I extend Zend_Db_Table (version 1.7.1), now it’s can support join other table when u use fetchAll

it’s something like

$this->_model->setParentMap('user', 'id_user', 'id_user', array('name'))->fetchAll('id_user = "0"')

so it’s can read table “user”‘s “name” data when u get table “friend” data.

this is my code:

class X_Db_Table extends Zend_Db_Table
{
     protected $_parentMap = array();
    /**
     * set ParentMap value
     *
     * @param string $tablename
     * @param string $remote
     * @param string $local
     * @param array $fields
     * @return Zend_Db_Table
     */
 
    function setParentMap($tablename, $remote, $local, $fields)
    {
        $this->_parentMap[$tablename] =  array(
                                         'remote' => $remote,
                                          'local' => $local,
                                          'fields' => $fields + array('table_prefix' => Zend_Registry::get ('dbprefix')),
                                          );
        return $this;
    }
 
    /**
     * the parent mapping from self::_parentMap
     *
     * @param string|array $where
     * @param string|array $order
     * @param integer $count
     * @param integer $offset
     * @return array
     */
 
    public function fetchAll($where = null, $order = null, $count = null, $offset = null)
    {
        if(count($this->_parentMap) == 0)
            return parent::fetchAll($where, $order, $count, $offset);
        else{
            $select = $this->_db->select()
                    ->from($this->_name)
                    ->order($order)
                    ->limit($offset, $count);
 
            if (!is_null($where)) {
                $select->where($where);
            }
 
            foreach($this->_parentMap as $parentTable => $specs) {
                $fields = array();
                if (isset($specs['fields']['table_prefix'])) {
                    $parentTable = $specs['fields']['table_prefix'].$parentTable;
                    unset($specs['fields']['table_prefix']);
                }
                if (isset($specs['fields']['prefix'])) {
                    $prefix = $specs['fields']['prefix'];
                    unset($specs['fields']['prefix']);
                    foreach($specs['fields'] as $key => $field) {
                        if (is_int($key)) {
                            $key = $prefix.$field;
                        }
                        $fields[$key] = $field;
                    }
                } else {
                    $fields = $specs['fields'];
                }
                $select->joinLeft(
                    $parentTable,
                    sprintf('%s.%s = %s.%s',
                        $this->_db->quoteIdentifier($this->_name),
                        $this->_db->quoteIdentifier($specs['local']),
                        $this->_db->quoteIdentifier($parentTable),
                        $this->_db->quoteIdentifier($specs['remote'])
                    ),
                    $fields
                );
            }
 
            $this->_parentMap = array();
            return $this->_db->fetchAll($select);
        }
    }
}

u can use it in yr models

Share and Enjoy:
  • Digg
  • Twitter
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • RSS
  • HelloTxt
  • MyShare
  • MySpace
  • Add to favorites
  • PDF
  • Print