Zend Framework - With Data Mapper - Can't retrive data to the view

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. :nono:

How can we properly display those elements on the view ? What am I missing ?

Thanks a lot in advance.

Ok. We can use getters to solve this. Of course.

But on the Zend Quick Start Guide they DON’T use/call them on the view.

And I’m not getting how can we do this, that same way they do.

I notice that THEY use magic methods like __get and __set but, I’m not using them here, because I really don’t get them.

So perhaps, the fact that they access the properties directly is due to the fact that they are using magic methods ?

The only advantage I see here, may be the fact that, in order to access properties we don’t need to know what methods do we have for retrieving them.

But a clear name convention could clear this as well…

If I’m saying something silly, please, let me know, if not, then, I rest my case, and I will call $entry->getName() and $entry->getAddress() on my view.

:shifty: