Learning Zend Framework after Magento: Models Learning Zend Framework after Magento: Models php php

Learning Zend Framework after Magento: Models


The Zend team, as mentioned elsewhere, thinks differently about the Model layer than most other PHP Framework creators. Their current thoughts on "the best" way to use their raw tools to provide a Database backed Entity Model can be found in the quick start guide.

That said, most people's solution to Models in Zend Framework is bootstrapping Doctrine.


Here is how I, personally, implement models. I'll use a real life example: my User model.

Whenever I create a model, I use two files and two classes: the model itself (e.g. Application_Model_User) and a mapper object (e.g. Application_Model_UserMapper). The model itself obviously contains the data, methods for saving, deleting, modifying, etc. The mapper object contains methods for fetching model objects, finding objects, etc.

Here are the first few lines of the User model:

class Application_Model_User {    protected $_id;    protected $_name;    protected $_passHash;    protected $_role;    protected $_fullName;    protected $_email;    protected $_created;    protected $_salt;    // End protected properties

For each property, I have a getter and setter method. Example for id:

/* id */public function getId() {    return $this->_id;}public function setId($value) {    $this->_id = (int) $value;    return $this;}

I also use some standard "magic methods" for exposing public getters and setters (at the bottom of each model):

public function __set($name, $value) {    $method = 'set' . $name;    if (('mapper' == $name) || !method_exists($this, $method)) {        throw new Exception('Invalid user property');    }    $this->$method($value);}public function __get($name) {    $method = 'get' . $name;    if (('mapper' == $name) || !method_exists($this, $method)) {        throw new Exception('Invalid user 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;}

Example save method:

I validate inside the save() method, using exceptions when the information fails to validate.

public function save() {            // Validate username    if (preg_match("/^[a-zA-Z](\w{6,15})$/", $this->_name) === 0) {        throw new Application_Exception_UserInfoInvalid();    }    // etc.    $db = Zend_Registry::get("db");    // Below, I would check if $this->_id is null. If it is, then we need to "insert" the data into the database. If it isn't, we need to "update" the data. Use $db->insert() or $db->update(). If $this->_id is null, I might also initialize some fields like 'created' or 'salt'.}

For the mapper object, I have at least two methods: a method that returns a query object for selecting objects, and one that executes the query, initializes and returns objects. I use this so I can manipulate the query in my controller for sorting and filtering.

EDIT

Like I said in my comments, this post: http://weierophinney.net/matthew/archives/202-Model-Infrastructure.html was the inspiration for my current Model implementation.

More options

You can also use Zend_Form to do validation, instead of rolling your own: http://weierophinney.net/matthew/archives/200-Using-Zend_Form-in-Your-Models.html. I personally don't like this option since I think that Zend_Form is awkward to use and hard to precisely control.

When most people first learn Zend Framework, they learn to subclass Zend_Db related classes. Here is an article that demonstrates this: http://akrabat.com/zend-framework/on-models-in-a-zend-framework-application/

I mentioned that I don't like doing this. Here are a few reasons why:

  • It's difficult to create models that involve derived/calculated fields (i.e. data populated from other tables)
  • I found it impossible to incorporate access control (populated from my database)
  • I like having full control over my models

EDIT 2

For your second example: You can use Zend_Paginator for this. I mentioned that, in your wrapper, you create a method that returns a database query object for selecting objects. Here's my simplified but working user mapper:

class Application_Model_UserMapper {    public function generateSelect() {        $db = Zend_Registry::get("db");        $selectWhat = array(            "users_id",            "name",            "role",            "full_name",            "email",            "DATE_FORMAT(created, '%M %e, %Y at %l:%i:%s %p') as created",            "salt",            "passhash"        );        return $db->select()->from(array("u" => "users"), $selectWhat);    }    public function fetchFromSelect($select) {        $rows = $select->query()->fetchAll();        $results = array();        foreach ($rows as $row) {            $user = new Application_Model_User();            $user->setOptions(array(                "id" => $row["users_id"],                "name" => $row["name"],                "role" => $row["role"],                "fullName" => $row["full_name"],                "email" => $row["email"],                "created" => $row["created"],                "salt" => $row["salt"],                "passHash" => $row["passhash"]            ));            $results[] = $user;        }        return $results;    }}

To handle the paginator, I write a custom Paginator plugin and save it to library/Application/Paginator/Adapter/Users.php. Be sure you have your appnamespace and autoloaderNamespaces[] setup correctly in application.ini. Here is the plugin:

class Application_Paginator_Adapter_Users extends Zend_Paginator_Adapter_DbSelect {    public function getItems($offset, $itemCountPerPage) {        // Simply inject the limit clause and return the result set        $this->_select->limit($itemCountPerPage, $offset);        $userMapper = new Application_Model_UserMapper();        return $userMapper->fetchFromSelect($this->_select);    }}

In my controller:

// Get the base select statement$userMapper = new Application_Model_UserMapper();$select = $userMapper->generateSelect();// Create our custom paginator instance$paginator = new Zend_Paginator(new Application_Paginator_Adapter_Users($select));// Set the current page of results and per page count$paginator->setCurrentPageNumber($this->_request->getParam("page"));$paginator->setItemCountPerPage(25);$this->view->usersPaginator = $paginator;

Then render the paginator in your view script.


I do something similar to SimpleCode's way. My style derives from Pádraic Brady. He has multiple blog posts but the best and quickest resource of his is a online book he wrote: Survive the Deep End!. This link should take you straight to his chapter on Models, Data Mappers, and other cool goodies such as Lazy Loading. The idea is the following:

You have entities such as a User with The properties are defined in an array. All your entities extend an abstract class with magic getter/setters that get from or update this array.

class User extends Entity{    protected $_data = array(        'user_id' => 0,        'first_name' => null,        'last_name' => null    );}class Car extends Entity{    protected $_data = array(        'car_id' => 0,        'make' => null,        'model' => null    );}class Entity{    public function __construct($data)    {        if(is_array($data))        {            $this->setOptions($data);        }    }    public function __get($key)    {        if(array_key_exists($key, $this->_data)        {            return $this->_data[$key];        }        throw new Exception("Key {$key} not found.");    }    public function __set($key, $value)    {        if(array_key_exists($key, $this->_data))        {            $this->_data[$key] = $value;        }        throw new Exception("Key {$key} not found.");    }    public function setOptions($data)    {        if(is_array($data))        {               foreach($data as $key => $value)            {                $this->__set($key, $value);            }        }    }    public function toArray()    {        return $this->_data;    }}$user = new User();$user->first_name = 'Joey';$user->last_name = 'Rivera';echo $user->first_name; // Joey$car = new Car(array('make' => 'chevy', 'model' => 'corvette'));echo $car->model; // corvette

Data Mappers to me are separate from the Entities, their job is to do the CRUD (create, read, update, and delete) to the db. So, if we need to load an entity from the db, I call a mapper specific to that entity to load it. For example:

<?phpclass UserMapper{    $_db_table_name = 'UserTable';    $_model_name = 'User';    public function find($id)    {        // validate id first        $table = new $this->_db_table_name();        $rows = $table->find($id);        // make sure you get data        $row = $rows[0]; // pretty sure it returns a collection even if you search for one id        $user = new $this->_model_name($row); // this works if the naming convention matches the user and db table        //else        $user = new $this->_model_name();        foreach($row as $key => $value)        {            $user->$key = $value;        }        return $user;    }}$mapper = new UserMapper();$user = $mapper->find(1); // assuming the user in the previous example was id 1echo $user->first_name; // Joey

This code is to give an idea of how to architect the code in this way. I didn't test this so I may have created some typos/syntax errors as I wrote it. Like others have mentioned, Zend lets you do what you want with Models, there is no right and wrong it's really up to you. I usually create a table class for every table in the db that I want to work with. So if I have a user table, I usually have a User entity, User Mapper, and a User Table class. The UserTable would extend Zend_Db_Table_Abstract and depending on what I'm doing won't have any methods inside or sometimes I'll overwrite methods like insert or delete depending on my needs. I end up with lots of files but I believe the separation of code makes it much easier to quickly get to where I need to be to add more functionality or fix bug since I know where all the parts of the code would be.

Hope this helps.