Skip to content
Snippets Groups Projects
Select Git revision
  • aa72efb204287848a56a74e90dd30553277c2abf
  • master default
  • shib-cas
3 results

Table.php

Blame
  • Table.php 5.84 KiB
    <?php
    
    require_once('Zend.php');
    
    Zend::loadInterface('Nmc_SingletonInterface');
    Zend::loadClass('Zend_Db_Table');
    
    abstract class Nmc_Db_Table
             extends Zend_Db_Table
             implements Nmc_SingletonInterface
    {
        public function _setup()
        {
            $selfInflection = new ReflectionObject($this);
            $docBlock = Nmc_DocBlock::createFromString($selfInflection->getDocComment());
    
            if (! $this->_name) {
                if($docBlock->table) {
                    $this->_name = $docBlock->table;
                } else {
                    $this->_name = DATABASE_TABLE_PREFIX . self::$_inflector->underscore(get_class($this));
                }
            }
    
            if ($this->_primary == 'id') {
                if($docBlock->primary) {
                    $this->_primary = $docBlock->primary;
                }
            }
    
            parent::_setup();
        }
    
        /**
         * find() - returns proper type of row
         *
         * @param mixed $val
         * @return Nmc_Db_Table_Row
         */
        public function find($val)
        {
            $row = parent::find($val);
            if(!$row) {
                return null;
            }
    
            $returnType = $this->getRowReturnType();
    
            $config = array('db'    => $this->getAdapter(),
                            'table' => $this,
                            'data'  => $row->toArray());
            return Nmc_Db_RowManager::getInstance()->getRow($returnType, $config);
        }
    
        /**
         * findAll() returns rows with primary keys listed in the array
         *
         * @param array $valArray
         * @return Nmc_Db_Table_Rowset
         */
        public function findAll($valArray) {
            if(!is_array($valArray) || count($valArray) == 0) {
                return $this->fetchNewRowset();
            }
            $where = $this->_db->quoteInto($this->_primary . ' IN (?)', $valArray);
            return $this->fetchAll($where);
        }
    
        /**
         * fetchNew() - returns proper type of row
         *
         * @return Nmc_Db_Table_Row
         */
        public function fetchNew()
        {
            $row = parent::fetchNew();
            $returnType = $this->getRowReturnType();
    
            $config = array('db'    => $this->getAdapter(),
                            'table' => $this,
                            'data'  => $row->toArray());
            return new $returnType($config);
        }
    
        /**
         * fetchRow() - returns proper type of row
         *
         * @param string $where
         * @param string $order
         * @return Nmc_Db_Table_Row
         */
        public function fetchRow($where = null, $order = null)
        {
            $row = parent::fetchRow($where, $order);
            $rowData = $row->toArray();
            $rowEmpty = true;
            foreach($rowData as $value) {
                if($value !== null) {
                    $rowEmpty = false;
                    break;
                }
            }
            if($rowEmpty) {
                return null;
            }
    
            $returnType = $this->getRowReturnType();
    
            $config = array('db'    => $this->getAdapter(),
                            'table' => $this,
                            'data'  => $rowData);
            return Nmc_Db_RowManager::getInstance()->getRow($returnType, $config);
        }
    
        /**
         * fetchAll() - returns proper type of rowset
         *
         * @param string $where
         * @param string $order
         * @param int $count
         * @param int $offset
         * @return Nmc_Db_Table_Rowset
         */
        public function fetchAll($where = null, $order = null, $count = null, $offset = null)
        {
            $rowset = parent::fetchAll($where, $order, $count, $offset);
            $returnType = $this->getRowsetReturnType();
    
            $config = array('db'    => $this->getAdapter(),
                            'table' => $this,
                            'data'  => $rowset->toArray());
            $newRowset = new $returnType($config);
    
            return $newRowset;
        }
    
    
        public function fetchNewRowset()
        {
    	    $config = array();
    	    $config['db'] = $this->_db;
    	    $config['table'] = $this;
    	    $config['data'] = array();
    	    $rowsetClass = $this->getRowsetReturnType();
    	    return new $rowsetClass($config);
        }
    
        /**
         * Returns the name of the class this table uses to store rows
         *
         * @return string
         */
        public function getRowReturnType()
        {
            if(is_string($this->_rowClass)) {
                $classParents = class_parents($this->_rowClass);
                if(is_array($classParents) && in_array('Nmc_Db_Table_Row', $classParents)) {
                    return $this->_rowClass;
                }
            }
    
            $selfInflection = new ReflectionObject($this);
            $docBlock = Nmc_DocBlock::createFromString($selfInflection->getDocComment());
            if($docBlock->rowClass) {
                return $docBlock->rowClass;
            }
    
            return 'Nmc_Db_Table_Row';
        }
    
        /**
         * Returns the name of the class this table uses to store rowsets
         *
         * @return unknown
         */
        public function getRowsetReturnType()
        {
            if(!is_string($this->rowsetClass)) {
                return 'Nmc_Db_Table_Rowset';
            }
            $classParents = class_parents($this->rowsetClass);
            if(!is_array($classParents) || !in_array('Nmc_Db_Table_Rowset', $classParents)) {
                return 'Nmc_Db_Table_Rowset';
            }
            return $this->rowsetClass;
        }
    
        /**
         * Returns the name of the this table
         *
         * @return string
         */
        public function getTableName()
        {
            return $this->_name;
        }
    
        /**
         * Returns the name of the primary key for this table
         *
         * @return string
         */
        public function getPrimaryKeyName($camelized = false)
        {
            $primaryKeyName = $this->_primary;
            if ($camelized) {
                $primaryKeyName = Nmc_Db_Inflector::getInstance()->camelize($primaryKeyName);
            }
            return $primaryKeyName;
        }
    
        static public function serialize(Nmc_Db_Table $table)
        {
            return get_class($table);
        }
    
        static public function unserialize($serializedTable)
        {
            return call_user_func(array($serializedTable, 'getInstance'));
        }
    }
    
    ?>