If you would like you play around with the below class and interface and learn from it. This is the data container for my active record.
PHP Code:
interface IActiveRecordDataEntity {
public function getProperty($pName);
public function setProperty($pName,$pValue);
public function hasProperty($pName);
public function hasChanged($pName);
public function addRecord($pPropertyName,IActiveRecordDataEntity $pRecord,$pArrayByDefault=false);
public function getRecord($pPropertyName,$pPrimaryKey,$pField);
public function removeProperty($pPropertyName);
public function getData();
public function cast();
}
PHP Code:
class ActiveRecordDataEntity implements IActiveRecordDataEntity {
private $_data;
public function __construct() {
$this->_data = array();
}
public function setProperty($pName,$pValue) {
if($this->hasProperty($pName)===true) {
array_unshift($this->_data[$pName],$pValue);
} else {
$this->_data[$pName] = array($pValue);
}
}
public function getProperty($pName) {
if($this->hasProperty($pName)===true) {
return $this->_data[$pName][0];
}
}
public function hasProperty($pName) {
return array_key_exists($pName,$this->_data);
}
public function getRecord($pPropertyName,$pPrimaryKey,$pField) {
if(!array_key_exists($pPropertyName,$this->_data)) return false;
if(!($this->_data[$pPropertyName][0] instanceof ActiveRecord) && $this->_data[$pPropertyName][0] instanceof arrayaccess) {
foreach($this->_data[$pPropertyName][0] as $record) {
if($record->$pField == $pPrimaryKey) return $record;
}
} else {
if($this->_data[$pPropertyName][0]->$pField == $pPrimaryKey) {
return $this->_data[$pPropertyName][0];
}
}
return false;
}
public function addRecord($pPropertyName,IActiveRecordDataEntity $pRecord,$pArrayByDefault=false) {
if(array_key_exists($pPropertyName,$this->_data)===true) {
if($this->_data[$pPropertyName][0] instanceof arrayaccess) {
$this->_data[$pPropertyName][0][] = $pRecord;
} else {
//$this->_data[$pPropertyName][0] = array($this->_data[$pPropertyName][0]);
$this->_data[$pPropertyName][0] = new ActiveRecordCollection($this->_data[$pPropertyName][0]);
}
} else {
// for a hasMany relationship with only one item. Otehrwise if something
// only has one item in its result set but has a hasMany relationship
// a array would not exists which seems wrong.
if($pArrayByDefault === true) {
$this->_data[$pPropertyName] = array(new ActiveRecordCollection($pRecord));
} else {
$this->_data[$pPropertyName] = array($pRecord);
}
}
}
public function removeProperty($pPropertyName) {
if($this->hasProperty($pPropertyName)===true) {
unset($this->_data[$pPropertyName]);
return true;
} else {
return false;
}
}
public function getData() {
return $this;
}
public function hasChanged($pName) {
if($this->hasProperty($pName)===true) {
return count($this->_data[$pName])>1?true:false;
}
}
public function cast() {
foreach($this->_data as $key=>$data) {
$value = $this->_data[$key][0];
$this->_data[$key] = array($value);
}
}
}
practical usage
PHP Code:
$data = new ActiveRecordDataEntity();
$data->setProperty('name','oddz');
echo $data->getProperty('name'); // 'oddz'
There isn't a practical advantage of hard coding the properties into the class if your creating a generic system.
Bookmarks