Hello, please help me out:
I can’t get the data displayed on my view. And I’m not getting why.
I have a gateway defined like this:
//application/models/Dbtable/Contact.php
class Application_Model_DbTable_Contact extends Zend_Db_Table_Abstract
{
protected $_name = 'Contact';
}
I have a Mapper like this:
//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;
}
}
Then, I have a Domain Object like this:
//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;
}
}
Then, I have a controller like this:
//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();
}
}
Finally I have a view like this:
//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>
I’m getting:
Notice: Undefined property: Application_Model_Contact::$name
And it’s right, because, if I dump
$this->entries
I get no property with this name.
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"
}
}
If of course, I do:
<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>
I get:
Fatal error: Cannot access protected property Application_Model_Contact::$_name
And it makes sense, because, it is protected!
I’ve tried to follow Zend Quick Guide Tutorial Structure here:
http://framework.zend.com/manual/en/learning.quickstart.create-model.html
but I can’t figure this out.
How can we properly display those elements on the view ? What am I missing ?
Thanks a lot in advance.