ZF-439: Zend_Db_Table ORM implementation

Description

per http://framework.zend.com/wiki/display/…


<?php
/**
 * Zend Framework
 *
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://framework.zend.com/license/new-bsd
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@zend.com so we can send you a copy immediately.
 *
 * @category   Zend
 * @package    Zend_Db
 * @subpackage Table
 * @copyright  Copyright (c) 2006 Zend Technologies USA Inc. (http://www.zend.com)
 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 */ 

Zend::loadClass("Zend_Db_Table");

class Zend_Db_Table_Orm extends Zend_Db_Table {
    
    //per http://framework.zend.com/wiki/display/…
    protected $_rowClassName = 'Zend_Db_Table_Row';
    protected $_rowsetClassName = 'Zend_Db_Table_Rowset';
    protected $_dirs = null; // to support db_row classes in different directories
    
    /**
     * Fetches all rows.
     *
     * Honors the Zend_Db_Adapter fetch mode.
     *
     * @param string|array $where An SQL WHERE clause.
     * @param string|array $order An SQL ORDER clause.
     * @param int $count An SQL LIMIT count.
     * @param int $offset An SQL LIMIT offset.
     * @return mixed The row results per the Zend_Db_Adapter fetch mode.
     */
    public function fetchAll($where = null, $order = null, $count = null,
        $offset = null)
    {
        
        $className = $this->_rowsetClassName;
        Zend::loadClass($className, $this->_dirs);
        
        return new $className(array(
            'db'    => $this->_db,
            'table' => $this,
            'data'  => $this->_fetch('All', $where, $order, $count, $offset),
        ));
    }
    
    /**
     * Fetches one row.
     *
     * Honors the Zend_Db_Adapter fetch mode.
     *
     * @param string|array $where An SQL WHERE clause.
     * @param string|array $order An SQL ORDER clause.
     * @return mixed The row results per the Zend_Db_Adapter fetch mode.
     */
    public function fetchRow($where = null, $order = null)
    {
        $className = $this->_rowClassName;
        Zend::loadClass($className, $this->_dirs);
        
        return new $className(array(
            'db'    => $this->_db,
            'table' => $this,
            'data'  => $this->_fetch('Row', $where, $order, 1),
        ));
    }
    
    /**
     * Fetches a new blank row (not from the database).
     * 
     * @return _rowClassName class
     */
    public function fetchNew()
    {
        $keys = array_keys($this->_cols);
        $vals = array_fill(0, count($keys), null);
        
        $className = $this->_rowClassName;
        Zend::loadClass($className, $this->_dirs);
        
        return new $className(array(
            'db'    => $this->_db,
            'table' => $this,
            'data'  => array_combine($keys, $vals),

        ));
    }
    
}

?>

Comments

Super simple, super short, and very easy to use. However, we do not add classes or components to the ZF without a proposal. Please make a new proposal by clicking on the "Submit a new proposal" link in the blue box at the top of this page: http://framework.zend.com/wiki/display/ZFPROP/Home

I know many of us do not like filling out forms, but do not worry about the English wording. Just copy things from this issue, and make a few notes. The proposal is like code. Write a little code and then worry about adding more details later. Time permitting, I will help some also :)

If you write "Please help edit and improve this proposal" at the top of the new proposal, I am sure other community members will help with some of the details.

This issue should be closed when the review process completes for the proposal below:

http://framework.zend.com/wiki/x/bx0

Changing fix version to 0.8.0.

Recategorize as Zend_Db_Table component.

The requirements described in this issue duplicate those of ZF-38, even though the solution is slightly different.