我是 zend 框架的新手,我有一个问题要从一本有关系的书中获取作者:致命错误:调用 E:\xampp\htdocs\quickstart\ 中非对象上的成员函数 findParentRow()第 14 行的 application\controllers\BookController.php
我放了我的课,但目标是做一个在作者和书之间有 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框架,差别真的很大。感谢您的帮助