2013-09-21 54 views
1

我是zend框架中的新成员,并且我有一个问题可以从具有关系的书中获取作者: 致命错误:调用成员函数findParentRow()在E:\ XAMPP \ htdocs中\快速启动\程序\ \控制器上BookController.php线路14zend中的findParentRow()1.12

我把我的课,但我们的目标是做一个应用程序有一个1,N作者和书之间的关系

<?php 

class Application_Model_BookMapper 
{ 
    protected $_dbTable; 

    public function setDbTable($dbTable) 
    { 
     if (is_string($dbTable)) { 
      $dbTable = new $dbTable(); 
     } 
     if (!$dbTable instanceof Zend_Db_Table_Abstract) { 
      throw new Exception('Invalid table data gateway provided'); 
     } 
     $this->_dbTable = $dbTable; 
     return $this; 
    } 

    public function getDbTable() 
    { 
     if (null === $this->_dbTable) { 
      $this->setDbTable('Application_Model_DbTable_Book'); 
     } 
     return $this->_dbTable; 
    } 

    public function find($id, Application_Model_Book $book) 
    { 
     $result = $this->getDbTable()->find($id); 
     if (0 == count($result)) { 
      return; 
     } 
     $row = $result->current(); 
     $book->setId($row->id) 
        ->setTitle($row->title) 
        ->setAuteur($row->auteur_id); 
    } 

    public function fetchAll() 
    { 
     $resultSet = $this->getDbTable()->fetchAll(); 
     $entries = array(); 
     foreach ($resultSet as $row) { 
      $entry = new Application_Model_book(); 
      $entry->setId($row->id) 
        ->setTitle($row->title) 
        ->setAuteur($row->auteur_id); 
      $entries[] = $entry; 
     } 
     return $entries; 
    } 

    public function findAuteur($id, Application_Model_Book $book) 
    { 
     $result = $this->getDbTable()->find($id); 
     if (0 == count($result)) { 
      return; 
     } 
     $row = $result->current(); 

     //hydratation de l'objet book 
     $book->setId($row->id) 
     ->setTitle($row->title); 

     //Utilisation d'une methode pour récupérer l'auteur u livre 
     $rowAuteur=$row->findParentRow('Auteur');  
     $auteur= new Application_Model_Auteur(); 
     //hydratation de l'objet Auteur 
     $auteur->setId($rowAuteur->id) 
     ->setName($rowAuteur->name); 

     //Attacher l'auteur à l'article 
     $book->setAuteur($auteur); 
     return $book; 
    } 

} 

<?php 

class Application_Model_AuteurMapper 
{ 
    protected $_dbTable; 

    public function setDbTable($dbTable) 
    { 
     if (is_string($dbTable)) { 
      $dbTable = new $dbTable(); 
     } 
     if (!$dbTable instanceof Zend_Db_Table_Abstract) { 
      throw new Exception('Invalid table data gateway provided'); 
     } 
     $this->_dbTable = $dbTable; 
     return $this; 
    } 

    public function getDbTable() 
    { 
     if (null === $this->_dbTable) { 
      $this->setDbTable('Application_Model_DbTable_Auteur'); 
     } 
     return $this->_dbTable; 
    } 

    public function find($id, Application_Model_Auteur $auteur) 
    { 
     $result = $this->getDbTable()->find($id); 
     if (0 == count($result)) { 
      return; 
     } 
     $row = $result->current(); 
     $auteur->setId($row->id) 
        ->setName($row->name); 
    } 

    public function fetchAll() 
    { 
     $resultSet = $this->getDbTable()->fetchAll(); 
     $entries = array(); 
     foreach ($resultSet as $row) { 
      $entry = new Application_Model_Auteur(); 
      $entry->setId($row->id) 
        ->setName($row->name); 
      $entries[] = $entry; 
     } 
     return $entries; 
    } 

} 

<?php 

class Application_Model_DbTable_Book extends Zend_Db_Table_Abstract 
{ 

    protected $_name = 'book'; 
    protected $_referenceMap = array(
     'Auteur' => array(
      'columns'   => 'auteur_id', 
      'refTableClass'  => 'Application_Model_DbTable_Auteur', 
      'refColumns'  => 'id' 
     ) 
    ); 

} 


<?php 

class Application_Model_DbTable_Auteur extends Zend_Db_Table_Abstract 
{ 

    protected $_name = 'auteur'; 
    protected $_dependentTables = array('Application_Model_DbTable_Book'); 

} 



<?php 

class Application_Model_Book extends Zend_Db_Table_Row_Abstract 
{ 
    protected $_id; 
    protected $_title; 
    protected $_auteur; 

    public function __construct(array $options = null) 
    { 
     if (is_array($options)) { 
      $this->setOptions($options); 
     } 
    } 

    public function __set($title, $value) 
    { 
     $method = 'set' . $title; 
     if (('mapper' == $title) || !method_exists($this, $method)) { 
      throw new Exception('Invalid guestbook property'); 
     } 
     $this->$method($value); 
    } 

    public function __get($title) 
    { 
     $method = 'get' . $title; 
     if (('mapper' == $title) || !method_exists($this, $method)) { 
      throw new Exception('Invalid guestbook property'); 
     } 
     return $this->$method(); 
    } 

    public function setOptions(array $options) 
    { 
     $methods = get_class_methods($this); 
     foreach ($options as $key => $value) { 
      $method = 'set' . ucfirst($key); 
      if (in_array($method, $methods)) { 
       $this->$method($value); 
      } 
     } 
     return $this; 
    } 

    public function settitle($title) 
    { 
     $this->_title = (string) $title; 
     return $this; 
    } 

    public function gettitle() 
    { 
     return $this->_title; 
    } 

    public function setId($id) 
    { 
     $this->_id = (int) $id; 
     return $this; 
    } 

    public function getId() 
    { 
     return $this->_id; 
    } 

    public function setAuteur($auteur) 
    { 
     $this->_auteur = (string) $auteur; 
     return $this; 
    } 

    public function getAuteur() 
    { 
     //if (!$this->_auteur) { 
      //$this->_auteur = $this->findParentRow('Application_Model_DbTable_Book'); 
     //} 
     return $this->_auteur; 
    } 
} 

<?php 

class Application_Model_Auteur extends Zend_Db_Table_Row_Abstract 
{ 
    protected $_id; 
    protected $_name; 

    public function __construct(array $options = null) 
    { 
     if (is_array($options)) { 
      $this->setOptions($options); 
     } 
    } 

    public function __set($name, $value) 
    { 
     $method = 'set' . $name; 
     if (('mapper' == $name) || !method_exists($this, $method)) { 
      throw new Exception('Invalid guestbook property'); 
     } 
     $this->$method($value); 
    } 

    public function __get($name) 
    { 
     $method = 'get' . $name; 
     if (('mapper' == $name) || !method_exists($this, $method)) { 
      throw new Exception('Invalid guestbook property'); 
     } 
     return $this->$method(); 
    } 

    public function setOptions(array $options) 
    { 
     $methods = get_class_methods($this); 
     foreach ($options as $key => $value) { 
      $method = 'set' . ucfirst($key); 
      if (in_array($method, $methods)) { 
       $this->$method($value); 
      } 
     } 
     return $this; 
    } 

    public function setName($name) 
    { 
     $this->_name = (string) $name; 
     return $this; 
    } 

    public function getName() 
    { 
     return $this->_name; 
    } 

    public function setId($id) 
    { 
     $this->_id = (int) $id; 
     return $this; 
    } 

    public function getId() 
    { 
     return $this->_id; 
    } 

    public function getBooks(){ 

    } 
} 

我以前使用yii框架,差别非常大。 感谢您的帮助

回答

0

如果您从您的bookcontroller放置代码,它会更容易。但是Zend说的是你没有Zend_Db_Table_Row_Abstract对象的实例(根本没有任何对象)。如果你想使用方法findParentRow(),你必须确定你有这个对象。所以首先检查你的变量的类型。

0

我测试它是否是一个正确的类型(Zend_Db_Table_Row_Abstract)并将其':

$book = new Application_Model_BookMapper(); 

    //$name = $t->findParentRow('Application_Model_DbTable_Book'); 
    $this->view->entries = $book->fetchAll();//findAuteur(1,new Application_Model_Book()); 
    foreach ($this->view->entries as $b){ 
     echo gettype($b)."&&class=".get_class($b); 
     if ($b instanceof Zend_Db_Table_Row_Abstract) { 
      echo 'okkk'; 
     } 
     //$name = $b->findParentRow('Application_Model_DbTable_Auteur');//findDependentRowset 
    }