Need help, PHP & ZF please!

Associate
Joined
2 Oct 2003
Posts
2,121
Location
Chester
Moving from CakePHP to Zend Framework. I've noticed ZF being quite excessive compared to CakePHP but i'm keeping an open mind.

I can't see why i'm getting these errors. And i've been following the tutorial on the ZF website, just changed a few variable names!

Thanks for looking :)

Code:
[COLOR="DimGray"]Warning: Missing argument 1 for Application_Model_Staff::setFirstName(), called in C:\Users\Laura\CMS\cms\application\models\Staff.php on line 33 and defined in C:\Users\Laura\CMS\cms\application\models\Staff.php on line 70[/COLOR]

[B]Notice: Undefined variable: firstname in C:\Users\Laura\CMS\cms\application\models\Staff.php on line 72[/B]

[COLOR="DimGray"]Warning: htmlspecialchars() expects parameter 1 to be string, object given in C:\Program Files\Zend\ZendServer\share\ZendFramework\library\Zend\View\Abstract.php on line 897

    Warning: Missing argument 1 for Application_Model_Staff::setLastName(), called in C:\Users\Laura\CMS\cms\application\models\Staff.php on line 33 and defined in C:\Users\Laura\CMS\cms\application\models\Staff.php on line 82[/COLOR]

    [B]Notice: Undefined variable: lastname in C:\Users\Laura\CMS\cms\application\models\Staff.php on line 84[/B]

    [COLOR="DimGray"]Warning: htmlspecialchars() expects parameter 1 to be string, object given in C:\Program Files\Zend\ZendServer\share\ZendFramework\library\Zend\View\Abstract.php on line 897[/COLOR]

PHP:
// index.phtml
<dl>
    <?php foreach ($this->entries as $entry): ?>
    <dt><?php echo $this->escape($entry->firstname) ?></dt>
    <dd><?php echo $this->escape($entry->lastname) ?></dd>
    <?php endforeach ?>
</dl>

PHP:
// Staff.php
<?php
class Application_Model_Staff
{
	protected $_id;
	protected $_username;
	protected $_firstname;
	protected $_lastname;
	protected $_password;
	protected $_created;
	
	public function __construct(array $options = null)
	{
	    if (is_array($options)) {
			$this->setOptions($options);
    	}
	}
	
	public function __set($name,$value)
	{
		$method = 'set' . $name;
		if (('mapper' == $name) || !method_exists($this, $method)) {
			throw new Exception('Invalid staff property');
		} 
		$this->$method($value);
	}
	
	public function __get($name)
	{
		$method = 'set' . $name;
		if (('mapper' == $name) || !method_exists($this, $method)) {
			throw new Exception('Invalid staff property');
		} 
		return $this->$method();
	}
	
	public function setOptions(array $options)
	{
	    $methods = get_class_methods($this);
	    foreach ($options as $key => $value) {
	        $method = 'set' . ucfirst($key);
	        if (in_array($method, $methods)) {
	            $this->$method($value);
	        }
	    }
    	return $this;
	}
	
	public function setId($id)
	{
		$this->_id = (int) $id;
		return $this;
	}
	
	public function getId()
	{
		return $this->_id;
	}
	
	public function setUsername($username)
	{
		$this->_username = (string) $username;
		return $this;
	}
	
	public function getUsername()
	{
		return $this->_username;
	}
	
	public function setFirstName($firstname)
	{
		$this->_firstname = (string) $firstname;
		return $this;
	}
	
	public function getFirstName()	
	{
		return $this->_firstname;
	}

	
	public function setLastName($lastname)
	{
		$this->_lastname = (string) $lastname;
		return $this;
	}
	
	public function getLastName()
	{
		return $this->_lastname;
	}
	
	public function setPassword($password)
	{
		$this->_password = (string) $password;
		return $this;
	}
	
	public function getPassword()
	{
		return $this->_password;
	}
	
	public function setCreated($created)
	{
		$this->_created = $created;
		return $this;
	}
	
	public function getCreated()
	{
		return $this->_created;
	}
}
?>

PHP:
// StaffMapper.php
<?php

class Application_Model_StaffMapper
{
	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_Staff');
	    }

    	return $this->_dbTable;
    }
	
	public function save(Application_Model_Staff $staff)
	{
		$data = array(
			'username'	=> $staff->getUsername(),
			'firstname'	=> $staff->getFirstName(),
			'lastname'	=> $staff->getLastName(),
			'password'	=> $staff->getPassword(),
			'created'	=> date('Y-m-d H:i:s')
		);
		
		if (null === ($id = $staff->getId())) {
			unset($data['id']);
			$this->getDbTable()->insert($data);
		} else {
			$this->getDbTable()->update($data, array('id = ?' => $id));
		}
	}
	
	public function find($id)
	{
		$result = $this->getDbTable()->find($id);
		if (0 == count($result)) {
			return;
		}
		$row = $result->current();
		$staff->setId($row->id)
			  ->setUsername($row->username)
			  ->setFirstName($row->firstname)
			  ->setLastName($row->lastname)
			  ->setPassword($row->password)
			  ->setCreated($row->created);
	}
	
	public function fetchAll()
	{
		$resultSet = $this->getDbTable()->fetchAll();
		$entries = array();
		foreach($resultSet as $row) {
			$entry = new Application_Model_Staff();
			$entry->setId($row->id)
			  	  ->setUsername($row->username)
			  	  ->setFirstName($row->firstname)
			  	  ->setLastName($row->lastname)
			  	  ->setPassword($row->password)
			  	  ->setCreated($row->created);
			$entries[] = $entry;
		}
		return $entries;
	}
}
 
How come you are not extending the Zend_Db_Table class for your models? That would make life a lot easier.
 
fez said:
How come you are not extending the Zend_Db_Table class for your models? That would make life a lot easier.

I don't know, i'm not that educated yet. I'm just following a tutorial pretty much exactly to the word, except for a few variable names :confused:

Dj_Jestar said:
change setFirstName() to setFirstname().

That didn't work but I wasn't really expecting it too either. Unless I did something wrong?
 
PHP:
    public function __get($name)
    {
        $method = 'set' . $name;
        if (('mapper' == $name) || !method_exists($this, $method)) {
            throw new Exception('Invalid staff property');
        } 
        return $this->$method();
    }
The mistake is in there.. __get($name) and yet you have
PHP:
$method = 'set' . $name;
That 'set' should be 'get'. :)
 
Looks fine to me, only I'd use public properties instead of magic __get/__set. But tbh, because you are throwing custom exceptions for missing parameters, it's the only way to do so. :)
 
Back
Top Bottom