Skip to content

Instantly share code, notes, and snippets.

@Doopin
Created May 13, 2015 14:57
Show Gist options
  • Save Doopin/44a68b8c7c437be6d502 to your computer and use it in GitHub Desktop.
Save Doopin/44a68b8c7c437be6d502 to your computer and use it in GitHub Desktop.
How to reset HydratingResultSet for many iterations?
//In myMapper class, I have this method
public function loadAllPosts() {
$postObject = new Post();
$userObject = new User();
$select = new Select();
$select->from(['post' => $postObject->tableName()])
->join(['user' => $userObject->tableName()], 'post.user= user.id');
$stmt = $this->m_sql->prepareStatementForSqlObject($select);
$result = $stmt->execute();
if($result instanceof ResultInterface && $result->isQueryResult()){
$postResultSet = new HydratingResultSet(new ClassMethods(), $postObject);
$userResultSet = new HydratingResultSet(new ClassMethods(), $userObject);
return [
'status' => TRUE,
'posts' => $postResultSet->initialize($result),
'users' => $doopinResultSet->initialize($result)
];
}
return [
'status' => FALSE
];
}
//And I were expecting to have data in my service class like this
public function getPosts() {
$result = $this->m_loginMapper->loadAllPosts();
$model = [];
if($result['status']){
if(!empty($result['users']) && !empty($result['posts'])){
$users = $result['users'];
$usersArray = $users->toArray();
$posts = $result['posts'];
$postsArray = $posts->toArray();
foreach ($postsArray as $key => $post) {
$post = $postsArray[$key];
$postWithUser = $post->setUser($usersArray[$key]);
$model[] = $postWithUser;
}
} else
$result['doops'] = NULL;
}
return $model;
}
//I always have this exception
/*This result is a forward only result set, calling rewind() after moving forward is not supported
I tried buffer with rewind. but not working. How can I achieve this or is there another way?
Thanks */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment