2009-05-10 10:50:39 +03:00
|
|
|
|
<?php
|
2008-09-21 09:36:57 +03:00
|
|
|
|
/*-------------------------------------------------------
|
|
|
|
|
*
|
|
|
|
|
* LiveStreet Engine Social Networking
|
|
|
|
|
* Copyright © 2008 Mzhelskiy Maxim
|
|
|
|
|
*
|
|
|
|
|
*--------------------------------------------------------
|
|
|
|
|
*
|
|
|
|
|
* Official site: www.livestreet.ru
|
|
|
|
|
* Contact e-mail: rus.engine@gmail.com
|
|
|
|
|
*
|
|
|
|
|
* GNU General Public License, version 2:
|
|
|
|
|
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
|
|
|
|
|
*
|
|
|
|
|
---------------------------------------------------------
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Абстрактный класс сущности
|
|
|
|
|
*
|
|
|
|
|
*/
|
2009-04-27 20:45:36 +03:00
|
|
|
|
abstract class Entity extends Object {
|
2008-09-21 09:36:57 +03:00
|
|
|
|
protected $_aData=array();
|
2011-05-03 14:55:17 +03:00
|
|
|
|
protected $sPrimaryKey = null;
|
2008-09-21 09:36:57 +03:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Если передать в конструктор ассоциативный массив свойств и их значений, то они автоматом загрузятся в сущность
|
|
|
|
|
*
|
2010-02-07 20:50:45 +02:00
|
|
|
|
* @param array|null $aParam
|
2008-09-21 09:36:57 +03:00
|
|
|
|
*/
|
2010-02-07 20:50:45 +02:00
|
|
|
|
public function __construct($aParam = false) {
|
|
|
|
|
$this->_setData($aParam);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function _setData($aData) {
|
|
|
|
|
if(is_array($aData)) {
|
|
|
|
|
foreach ($aData as $sKey => $val) {
|
2008-09-21 09:36:57 +03:00
|
|
|
|
$this->_aData[$sKey] = $val;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2010-02-07 20:50:45 +02:00
|
|
|
|
|
|
|
|
|
public function _getData($aKeys=array()) {
|
|
|
|
|
if(!is_array($aKeys) or !count($aKeys)) return $this->_aData;
|
|
|
|
|
|
|
|
|
|
$aReturn=array();
|
|
|
|
|
foreach ($aKeys as $key) {
|
2010-08-13 09:12:19 +03:00
|
|
|
|
if(array_key_exists($key,$this->_aData)) {
|
2010-02-07 20:50:45 +02:00
|
|
|
|
$aReturn[$key] = $this->_aData[$key];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return $aReturn;
|
|
|
|
|
}
|
|
|
|
|
|
2010-08-13 09:12:19 +03:00
|
|
|
|
public function _getDataOne($sKey) {
|
|
|
|
|
if(array_key_exists($sKey,$this->_aData)) {
|
|
|
|
|
return $this->_aData[$sKey];
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2011-08-19 11:24:36 +03:00
|
|
|
|
/**
|
|
|
|
|
* Рекурсивное преобразование объекта и вложенных объектов в массив
|
|
|
|
|
*/
|
|
|
|
|
public function _getDataArray()
|
|
|
|
|
{
|
|
|
|
|
$aResult = array();
|
|
|
|
|
foreach ($this->_aData as $sKey => $sValue) {
|
|
|
|
|
if (is_object($sValue) && $sValue instanceOf Entity) {
|
|
|
|
|
$aResult[$sKey] = $sValue->_getDataArray();
|
|
|
|
|
} else {
|
|
|
|
|
$aResult[$sKey] = $sValue;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return $aResult;
|
|
|
|
|
}
|
|
|
|
|
|
2009-04-23 10:40:25 +03:00
|
|
|
|
/**
|
|
|
|
|
* Ставим хук на вызов неизвестного метода и считаем что хотели вызвать метод какого либо модуля
|
|
|
|
|
*
|
|
|
|
|
* @param string $sName
|
|
|
|
|
* @param array $aArgs
|
|
|
|
|
* @return unknown
|
|
|
|
|
*/
|
|
|
|
|
public function __call($sName,$aArgs) {
|
2010-02-07 17:01:27 +02:00
|
|
|
|
$sType=strtolower(substr($sName,0,3));
|
2010-08-03 23:13:44 +03:00
|
|
|
|
if (!strpos($sName,'_') and in_array($sType,array('get','set'))) {
|
2010-08-13 18:40:21 +03:00
|
|
|
|
$sKey=func_underscore(substr($sName,3));
|
2010-02-07 17:01:27 +02:00
|
|
|
|
if ($sType=='get') {
|
2010-08-03 23:13:44 +03:00
|
|
|
|
if (isset($this->_aData[$sKey])) {
|
2010-02-07 17:01:27 +02:00
|
|
|
|
return $this->_aData[$sKey];
|
2010-08-03 23:13:44 +03:00
|
|
|
|
} else {
|
|
|
|
|
if (preg_match('/Entity([^_]+)/',get_class($this),$sModulePrefix)) {
|
2010-08-13 18:40:21 +03:00
|
|
|
|
$sModulePrefix=func_underscore($sModulePrefix[1]).'_';
|
2010-08-03 23:13:44 +03:00
|
|
|
|
if (isset($this->_aData[$sModulePrefix.$sKey])) {
|
|
|
|
|
return $this->_aData[$sModulePrefix.$sKey];
|
|
|
|
|
}
|
|
|
|
|
}
|
2010-02-07 17:01:27 +02:00
|
|
|
|
}
|
|
|
|
|
return null;
|
2010-08-13 09:12:19 +03:00
|
|
|
|
} elseif ($sType=='set' and array_key_exists(0,$aArgs)) {
|
2010-02-07 17:01:27 +02:00
|
|
|
|
$this->_aData[$sKey]=$aArgs[0];
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
return Engine::getInstance()->_CallModule($sName,$aArgs);
|
|
|
|
|
}
|
2009-04-23 10:40:25 +03:00
|
|
|
|
}
|
2011-05-03 14:55:17 +03:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Получение первичного ключа сущности (ключ, а не значение!)
|
|
|
|
|
*/
|
|
|
|
|
public function _getPrimaryKey()
|
|
|
|
|
{
|
|
|
|
|
if (!$this->sPrimaryKey) {
|
|
|
|
|
if (isset($this->_aData['id'])) {
|
|
|
|
|
$this->sPrimaryKey = 'id';
|
|
|
|
|
} else {
|
|
|
|
|
// Получение primary_key из схемы бд (пока отсутствует)
|
|
|
|
|
$this->sPrimaryKey = 'id';
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $this->sPrimaryKey;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function _getPrimaryKeyValue() {
|
|
|
|
|
return $this->_getDataOne($this->_getPrimaryKey());
|
|
|
|
|
}
|
2008-09-21 09:36:57 +03:00
|
|
|
|
}
|
|
|
|
|
?>
|