Select Git revision
Extend.php 3.36 KiB
<?php
/**
* Extend.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
*/
/**
* Nmc_Db_Table_Relation_Extend - A one to one relation where one table extends another
*
* @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_Table_Relation_Extend extends Nmc_Db_Table_Relation_Abstract {
protected $_parentTable = null;
protected $_parentRow = null;
protected $_foreignKeyName = null;
protected $_row = null;
protected $_attributeName = null;
protected $_updateParent = true;
/**
* Sets up the relation
*
* @param Nmc_Db_Table $parentTable - table this row belongs to
* @param Nmc_Db_Row $row - the row
* @param string $foreignKeyName - name of the foreign key column
* @param string[optional] - name to access the parentRow with via the child
* @param bool[optional] - whether or not to allow changes to the parent
*/
public function __construct(Nmc_Db_Table $parentTable,
Nmc_Db_Table_Row $row,
$foreignKeyName,
$attributeName = null,
$updateParent = true)
{
$this->_parentTable = $parentTable;
$this->_foreignKeyName = $foreignKeyName;
$this->_row = $row;
$this->_attributeName = $attributeName;
$this->_updateParent = $updateParent;
}
public function setAttributeName($attributeName)
{
$this->_attributeName = $attributeName;
}
public function setUpdateParent($updateParent = false)
{
$this->_updateParent = $updateParent;
}
public function register()
{
$this->_parentRow = $this->_parentTable->findOne($this->_row->{$this->_foreignKeyName});
if(!$this->_parentRow) {
$this->_parentRow = $this->_parentTable->fetchNew();
}
}
public function onPostGet($name)
{
if($this->_attributeName && $name == $this->_attributeName) {
return $this->_parentRow;
}
return $this->_parentRow->__get($name);
}
public function onPostSet($name, $value)
{
if($this->_updateParent) {
if($this->_attributeName && $name == $this->_attributeName) {
$this->_parentRow = $value;
}
return $this->_parentRow->__set($name, $value);
}
return null;
}
public function onPreSave()
{
if($this->_updateParent) {
$this->_parentRow->save();
}
$this->_row->{$this->_foreignKeyName} = $this->_parentRow->getPrimaryKey();
}
public function onPostDelete()
{
$this->_parentRow->delete();
}
public function onPostClone(Nmc_Db_Table_Row $newParentRow)
{
$this->_row = $newParentRow;
if($this->_updateParent) {
$this->_parentRow = clone $this->_parentRow;
}
}
public function __sleep()
{
$this->_parentTable = Nmc_Db_Table::serialize($this->_parentTable);
return array_keys(get_object_vars($this));
}
public function __wakeup()
{
$this->_parentTable = Nmc_Db_Table::unserialize($this->_parentTable);
}
}
?>