我无法获取视图上显示的数据。我不明白为什么。
我有一个定义如下的网关:
//application/models/Dbtable/Contact.php
class Application_Model_DbTable_Contact extends Zend_Db_Table_Abstract
{
protected $_name = 'Contact';
}
我有一个这样的映射器:
//application/models/mappers/Contact.php
class Application_Model_Mapper_Contact
{
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_Contact');
}
return $this->_dbTable;
}
public function fetchAll()
{
$resultSet = $this->getDbTable()->fetchAll();
$entries = array();
foreach ($resultSet as $row)
{
$entry = new Application_Model_Contact();
$entry->setId($row->id);
$entry->setname($row->name);
$entry->setaddress($row->address);
$entries[] = $entry;
}
return $entries;
}
}
然后,我有一个这样的域对象:
//application/models/Contact.php
class Application_Model_Contact
{
protected $_id;
protected $_name;
protected $_address;
public function getId() {
return $this->_id;
}
public function setId($id) {
$this->_id = $id;
}
public function getname() {
return $this->_name;
}
public function setname($name) {
$this->_name = $name;
}
public function getaddress() {
return $this->_address;
}
public function setaddress($address) {
$this->_address = $address;
}
}
然后,我有一个这样的控制器:
//application/controllers/ContactController.php
class ContactController extends Zend_Controller_Action
{
public function init()
{
}
public function indexAction()
{
$Contact = new Application_Model_Mapper_Contact();
$this->view->entries = $Contact->fetchAll();
}
}
最后我得到了这样的视图:
//application/views/Contact/index.phml
<dl>
<?php foreach ($this->entries as $entry): ?>
<dt><?php echo $this->escape($entry->name) ?></dt>
<dd><?php echo $this->escape($entry->address) ?></dd>
<?php endforeach ?>
</dl>
我得到:
注意:未定义的属性: Application_Model_Contact::$name
这是对的,因为,如果我抛弃$this->entries
我不会得到这个名字的财产。
array(4) {
[0]=>
object(Application_Model_Contact)#50 (3) {
["_id":protected]=>
string(1) "1"
["_name":protected]=>
string(5) "NameA"
["_address":protected]=>
string(7) "AddressA"
}
[1]=>
object(Application_Model_Contact)#52 (3) {
["_id":protected]=>
string(1) "2"
["_name":protected]=>
string(5) "NameB"
["_address":protected]=>
string(7) "AddressB"
}
[2]=>
object(Application_Model_Contact)#54 (3) {
["_id":protected]=>
string(1) "3"
["_name":protected]=>
string(5) "NameC"
["_address":protected]=>
string(7) "AddressC"
}
[3]=>
object(Application_Model_Contact)#56 (3) {
["_id":protected]=>
string(1) "4"
["_name":protected]=>
string(5) "NameD"
["_address":protected]=>
string(7) "AddressD"
}
}
当然,如果这样做:
<dl>
<?php foreach ($this->entries as $entry): ?>
<dt><?php echo $this->escape($entry->_name) ?></dt>
<dd><?php echo $this->escape($entry->_address) ?></dd>
<?php endforeach ?>
</dl>
我得到:
致命错误:无法访问受保护 财产 Application_Model_Contact::$_name
这是有道理的,因为,它是受保护的。
我试图遵循Zend快速指南教程结构,但我无法弄清楚。http://framework.zend.com/manual/en/learning.quickstart.create-model.html
我们如何在视图上正确显示这些元素?我错过了什么?
更新:正如OZ_指出的那样,我确实可以使用getter,但是困扰我的是,在Zend快速入门教程中,我基于我的结构,他们不会在视图上使用getter,所以也许,我做错了什么。
非常感谢您在这里的时间。
使用 getter 获取属性:$this->escape($entry->_name)
替换为$this->escape($entry->getname())
和其他属性以相同的方式。
受保护的字段和私有字段只能通过 getter 和 setter 访问。
阅读以下内容可能很有用:http://www.php.net/manual/en/language.oop5.visibility.php