I want to list on a view something like
Number Commands | Date of commands | Description (text from table article)
All I have is
Number Commands | Date of commands | Description (id from table Bc)
I have two tables: BC (as commands) which have columns: id_commands, id_article, id_user, number_commands, date_commands, ... and table Article which have columns : id_article, name_article, description_article, ...
According to this tutorial, How to extend the ZF2 skeleton application - entities with foreign keys
There are my codes:
On Bc\\Model\\Bc.php
class Bc implements BcInterface
{
...
public function setArticle(ArticleInterface $bc_article)
{
$this->article = $bc_article;
}
public function getArticle()
{
return $this->bc_article;
}
}
On Bc\\Mapper\\BcHydrator.php
namespace Bc\Mapper;
class BcHydrator // extends Zend\Stdlib\Hydrator\ClassMethods
{
public function extract($object)
{
return array(
'bc_id' => $object->getBc_id(),
'article_designation' => $object->getA_designation(),
'article_reference' => $object->getA_reference()
);
}
}
On Bc\\Mapper\\ZendDbSqlMapper.php
...
public function findAllWithArticle()
{
$sql = new Sql($this->dbAdapter);
$select = $sql -> select()
-> from( array('a'=>'s_article'))
-> join( array('bc' => 's_bc'),
'bc.bc_id_article = a.a_id');
$stmt = $sql->prepareStatementForSqlObject($select);
$result = $stmt->execute();
if ($result instanceof ResultInterface && $result->isQueryResult()) {
$resultSet = new HydratingResultSet($this->hydrator, $this->bcPrototype);
\Zend\Debug\Debug::dump($result);die();
//return $resultSet->initialize($result);
}
return array();
}
On my Controller
...
public function detailAction()
{
$id = $this->params()->fromRoute('id');
try {
//$bc = $this->bcService->findBc($id);
$bc = $this->bcService->findAllBcsWithArticle($id);
$articleDesignation = $bc->getArticle()->getA_designation();
} catch (\InvalidArgumentException $ex) {
return $this->redirect()->toRoute('bc');
}
return new ViewModel(array(
'bc' => $bc,
'article' => $articleDesignation
));
}
I have this error, when I have access to my view:
Fatal error: Call to undefined method Zend\\Db\\ResultSet\\HydratingResultSet::getArticle()
Did someone have an idea of what I did wrong?
Finally i found a solution for those who want to know. I didn't use hydrator, but i have the result i want.
Add on my class Bc (Bc\\Model\\Bc.php) a protected variable $description and set setter and getter.
On my Mapper (Bc\\Mapper\\ZendDbSqlMapper.php), i added a function findContent ($id), and use as sql request:
$sql = new Sql($this->dbAdapter); $select = $sql -> select() -> from( array('bc'=>'bc')) -> join( array('a' => 'article'), 'bc.bc_id_article = a.id', array( 'description' => 'description', )); $select->where(array('bc_id = ?' => $id));
public function findBcsContent($id) { return $this->bcMapper->findContent($id); }
public function indexAction() { return new ViewModel(array( 'bcs'=> $this->bcService->findAllBcsContent() )); }
$this->escapeHtml($bc->getDesignation());
That's all
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.