Php Can';t使用数据映射器/网关设计模式将数据传递到视图中:(
我无法获取视图上显示的数据。我不知道为什么Php Can';t使用数据映射器/网关设计模式将数据传递到视图中:(,php,zend-framework,Php,Zend Framework,我无法获取视图上显示的数据。我不知道为什么 我有一个网关定义如下: //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_Mod
我有一个网关定义如下:
//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/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/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/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/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>
当然,如果我有:
<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>
我得到:
致命错误:无法访问受保护的
财产
应用程序\型号\联系人::$\名称
这是有道理的,因为它是受保护的
我试着遵循Zend Quick Guide教程的结构,但我无法理解这一点。
如何在视图上正确显示这些元素?我缺少什么?
更新:
正如OZ_uu所指出的,我确实可以使用getter,但困扰我的是,在Zend Quick Start教程中,我基于我的结构,他们没有在视图上使用getter,所以可能我做错了什么
非常感谢您在这里花费时间。使用getter获取属性:
$this->escape($entry->\u name)
替换为
$this->escape($entry->getname())
和其他属性的相同方式。
受保护字段和专用字段只能通过getter和setter进行访问
这对于以下内容很有用:使用getter获取属性:
$this->escape($entry->\u name)
替换为
$this->escape($entry->getname())
和其他属性的相同方式。
受保护字段和专用字段只能通过getter和setter进行访问
这可能对阅读有用:请查看我的更新,只是为了确保我做的一切都是正确的。:请查看我的更新,只是为了确保我做的一切都是正确的。:请阅读教程,无一例外:)我没有在您的模型中看到u get()和u set(),尽管教程中有描述。但是,我建议您使用getter和setter,因为这是一种更“真实”的OOP方式(对象可以在接口中描述)__get()和_uset()这是一种“魔法”,在短时间内很方便,但更脏。事实上,我没有使用它们,因为我不太理解它们。所以,因为他们使用了神奇的getter和setter,所以他们可以设置和获取,而不需要调用getName和getAddress?因为,这是我在那里看到的唯一区别。好吧,我不会在不理解它们的情况下使用魔法的舒适方法。我已经阅读了关于它们的php手册,它们对我来说仍然不清楚。我相信魔术师也意味着“对解释缺乏澄清”,所以一定不仅仅是我…:))总之,简单地说,你是在说:用你正在使用的方式,你做得很好,这就是它应该工作的方式?直接和干净的使用总是比魔术更好。所以,是的,使用getter和setter。谢谢你OZ_uu在这里的时间,真的,有时候我们会被困几个小时,而我昨晚被困了几个小时,试图理解为什么会这样。我记得我也复制了魔法方法,但出于某种原因,这也不起作用。不管怎么说,困难的部分是,当我们有一个问题,为了解决它,我们必须展示很多线。所以,再次感谢您在这里的时间。:)然后毫无例外地阅读教程:)我没有在您的模型中看到_get()和_set(),尽管在教程中有描述。但是,我建议您使用getter和setter,因为这是一种更“真实”的OOP方式(对象可以在接口中描述)__get()和_uset()这是一种“魔法”,在短时间内很方便,但更脏。事实上,我没有使用它们,因为我不太理解它们。所以,因为他们使用了神奇的getter和setter,所以他们可以设置和获取,而不需要调用getName和getAddress?因为,这是我在那里看到的唯一区别。好吧,我不会在不理解它们的情况下使用魔法的舒适方法。我已经阅读了关于它们的php手册,它们对我来说仍然不清楚。我相信魔术师也意味着“对解释缺乏澄清”,所以一定不仅仅是我…:))总之,简单地说,你是在说:用你正在使用的方式,你做得很好,这就是它应该工作的方式?直接和干净的使用总是比魔术更好。所以,是的,使用getter和setter。谢谢你OZ_uu在这里的时间,真的,有时候我们会被困几个小时,而我昨晚被困了几个小时,试图理解为什么会这样。我记得我也复制了魔法方法,但出于某种原因,这也不起作用。不管怎么说,困难的部分是,当我们有一个问题,为了解决它,我们必须展示很多线。所以,再次感谢您在这里的时间。:)