0

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?

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
RGM
  • 1
  • 1

1 Answers1

0

Finally i found a solution for those who want to know. I didn't use hydrator, but i have the result i want.

  1. Add on my class Bc (Bc\Model\Bc.php) a protected variable $description and set setter and getter.

  2. 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));
  1. On my service (Bc\Service\BcService.php)
public function findBcsContent($id)
    {
        return $this->bcMapper->findContent($id);
    }
  1. And on my controller
public function indexAction()
        {
             return new ViewModel(array(          
                'bcs'=> $this->bcService->findAllBcsContent()         
             ));  
        }
  1. On my view, i could use

$this->escapeHtml($bc->getDesignation());

That's all

RGM
  • 1
  • 1