私は 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 フレームワークを使用していましたが、その違いは本当に大きいです。ご協力いただきありがとうございます