Skip to content
Snippets Groups Projects
Select Git revision
  • aa72efb204287848a56a74e90dd30553277c2abf
  • master default
2 results

RowManager.php

Blame
  • user avatar
    Tim Steiner authored
    Creation of framework level controller, model, and view directories.  Moved existing library files to /library.
    
    aa72efb2
    History
    RowManager.php 3.10 KiB
    <?php
    /**
     * RowManager.php
     *
     * LICENSE: Some license information
     *
     * @copyright  2006 New Media Center - University of Nebraska-Lincoln
     * @license    http://www.gnu.org/licenses/gpl.txt GPL
     * @version    0.0.1
     * @link       http://nmc.unl.edu/
     * @since      File available since Release 0.0.1
    */
    
    require_once('Zend.php');
    Zend::loadInterface('Nmc_SingletonInterface');
    
    /**
     * Nmc_Db_RowManager - Keeps track of Nmc_Db_Table_Row instances
     *
     * @copyright  2006 New Media Center - University of Nebraska Lincoln
     * @license    http://www.gnu.org/gpl.txt GPL
     * @version    Release: 0.0.1
     * @link       http://nmc.unl.edu/
     * @since      Class available since Release 0.0.1
     */
    
    class Nmc_Db_RowManager implements Nmc_SingletonInterface
    {
    
        static protected $_instance = null;
        protected $_rows = null;
    
    	protected function __construct()
    	{
    	    $this->_rows = array();
    	}
    
    	/**
    	 * Return the one true instance of the class
    	 *
    	 * @return Nmc_Db_RowManager
    	 */
    	static public function getInstance()
    	{
    	    if(!self::$_instance) {
    	        self::$_instance = new Nmc_Db_RowManager();
    	    }
    	    return self::$_instance;
    	}
    
    	/**
    	 * getRow - returns the one true instance of the row you want
    	 *
    	 * @param string $rowClass
    	 * @param array $config
    	 * @return Nmc_Db_Table_Row
    	 */
        public function getRow($rowClass, $config)
        {
            $tableInfo = $config['table']->info();
    
            $tableName = $tableInfo['name'];
            $primaryKeyName = $tableInfo['primary'];
    
            $key = $this->_generateKey($tableName, $config['data'][$primaryKeyName]);
    
            if(!array_key_exists($key, $this->_rows)) {
                $this->addRow(new $rowClass($config), $key);
                //echo '$this->addRow(new ' . $rowClass . '($config), ' . $key . ');' . "<br />\n";
            }
    
            $row = $this->_rows[$key];
            return $row;
        }
    
        /**
         * addRow - Add a row to the manager
         *          This should only be used after saving a NEW row.
         *
         * @param Nmc_Db_Table_Row $row - row to be added
         * @param string[optional] $key - the key for the row (internal use ONLY)
         * @return void
         * @throws Zend_Db_Exception
         */
        public function addRow(Nmc_Db_Table_Row $row, $key = null)
        {
    
            if(!$key) {
                $tableInfo = $row->getTable()->info();
                $tableName = $tableInfo['name'];
                if($row->getPrimaryKey() == null) {
                    // row not in db, doesn't need (and can't use) management
                    return;
                }
                $key = $this->_generateKey($tableName, $row->getPrimaryKey());
            }
    
            // echo "Generating new record for: $key\n";
    
            if(array_key_exists($key, $this->_rows) && $this->_rows[$key] !== $row) {
                //throw new Zend_Db_Exception('Row is already being managed, one copy is stale');
                //trigger_error('Row is already being managed, one copy may be stale.', E_USER_NOTICE);
            }
            $this->_rows[$key] = $row;
    
            //echo "Done: $key (" . $this->_rows[$key] . ")\n";
        }
    
        protected function _generateKey($tableName, $primaryKey)
        {
            return $tableName . '::' . $primaryKey;
        }
    }
    
    ?>