我无法在我的视图中显示数据。我不明白为什么。
我有一个网关定义如下:
//application/models/Dbtable/Contact.php
class Application_Model_DbTable_Contact extends Zend_Db_Table_Abstract
{
protected $_name = 'Contact';
}
我有一个像这样的 Mapper :
//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 :: $名称
这是正确的,因为,如果我转储$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,所以也许,我是做错了。
非常感谢您在这里的时光。
答案 0 :(得分:1)
使用getters获取属性:
$this->escape($entry->_name)
替换为
$this->escape($entry->getname())
以及相同的其他属性
只能通过getter和setter访问受保护和私有字段。
这可用于阅读:http://www.php.net/manual/en/language.oop5.visibility.php