zend 1.12中的findParentRow()

时间:2013-09-21 12:24:39

标签: zend-framework one-to-many

我是zend框架中的新手,我有一个问题要从一本关系书中获取作者: 致命错误:在第14行的E:\ xampp \ htdocs \ quickstart \ application \ controllers \ BookController.php中的非对象上调用成员函数findParentRow()

我把我的课程,但目标是做一个与作者和书籍之间的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框架,差异非常大。 谢谢你的帮助

2 个答案:

答案 0 :(得分:0)

如果你把bookcontroller中的代码放在这里会更容易。但Zend所说的是你没有Zend_Db_Table_Row_Abstract对象的实例(你根本就没有任何对象)。如果要使用方法findParentRow(),则必须确保拥有此对象。因此,首先检查变量的类型。

答案 1 :(得分: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
    }