/** * save the entity * * @access public * @param bool $bOnDuplicateKeyUpdate (to do insert on duplicate key) * @return object */ public function save($bOnDuplicateKeyUpdate = false) { /** * Trigger on an entity to initialize it before the save */ if (method_exists(get_called_class(), 'beforeSave')) { static::beforeSave(); } $mPrimaryKeyName = $this->_mPrimaryKeyName; if ($bOnDuplicateKeyUpdate === false) { $bInsertMode = false; } else { $bInsertMode = true; } if ($mPrimaryKeyName === false) { throw new Exception('[' . __FILE__ . ' (l.' . __LINE__ . '] no primary key on this table!'); } else { if (is_string($mPrimaryKeyName)) { $sMethodPrimaryKey = 'get_' . $this->_mPrimaryKeyNameWithoutMapping; $aPrimaryKey = array($mPrimaryKeyName => $this->{$sMethodPrimaryKey}()); if ($this->{$sMethodPrimaryKey}() < 1) { $bInsertMode = true; } } else { $aPrimaryKey = array(); $oOrm = new Orm(); $iResults = $oOrm->select(array('*'))->from(preg_replace('/^.*\\\\([a-zA-Z0-9_]+)$/', '$1', get_called_class())); $oWhere = new Where(); foreach ($mPrimaryKeyName as $sKey => $sPrimaryKey) { $sMethodPrimaryKey = 'get_' . $this->_mPrimaryKeyNameWithoutMapping[$sKey]; $aPrimaryKey[$sPrimaryKey] = $this->{$sMethodPrimaryKey}(); $oWhere->andWhereEqual($sPrimaryKey, $aPrimaryKey[$sPrimaryKey]); } $aResults = $oOrm->where($oWhere)->load(); if (count($aResults) < 1) { $bInsertMode = true; } } } $aEntityTmp = get_object_vars(LibEntity::getRealEntity($this)); $aEntity = array(); foreach ($aEntityTmp as $sKey => $mField) { if ($mField !== null) { $aEntity[$sKey] = $mField; } } /** * check if the virtual foreign key in this model is respected */ if (count($this->_aForeignKey) > 0) { foreach ($this->_aForeignKey as $sName => $aForeignKey) { if ($aForeignKey['has_one'] == 1) { $sMethodPrimaryKey = 'get_' . $aForeignKey['foreign_key']; $mFIeld = $this->{$sMethodPrimaryKey}(); if ($mFIeld) { $oOrm = new Orm(); $iResults = $oOrm->select(array('*'))->from($aForeignKey['entity_join_name']); $oWhere = new Where(); $oWhere->whereEqual($aForeignKey['primary_key_name'], $mFIeld); $aResults = $oOrm->where($oWhere)->load(); if (count($aResults) < 1) { if ($aForeignKey['foreign_key_options']['message']) { throw new \Exception($aForeignKey['foreign_key_options']['message']); } else { throw new \Exception('Foreign Key not respected!'); } } } } } } /** * check if the virtual foreign key in the others models are respected */ $oReflectionClass = new \ReflectionClass(get_called_class()); $oReflectionProperties = $oReflectionClass->getProperties(); foreach ($oReflectionProperties as $mKey => $aOne) { $sCommentPhpDoc = $aOne->getDocComment(); if (strstr($sCommentPhpDoc, '@join')) { $sClassName = $aOne->class; $oClass = new $sClassName(); if (count($oClass->getForeignKey()) > 0) { foreach ($oClass->getForeignKey() as $sName => $aForeignKey) { if ($aForeignKey['has_many'] == 1) { $sMethodPrimaryKey = 'get_' . $aForeignKey['foreign_key']; $mFIeld = $this->{$sMethodPrimaryKey}(); if ($mFIeld) { $oOrm = new Orm(); $iResults = $oOrm->select(array('*'))->from($aForeignKey['entity_join_name']); $oWhere = new Where(); $oWhere->whereEqual($aForeignKey['primary_key_name'], $mFIeld); $aResults = $oOrm->where($oWhere)->load(); if (count($aResults) < 1) { if ($aForeignKey['foreign_key_options']['message']) { throw new \Exception($aForeignKey['foreign_key_options']['message']); } else { throw new \Exception('Foreign Key not respected!'); } } } } } } } } $oOrm = new Orm(); if ($bInsertMode === true) { $oOrm->insert(preg_replace('/^.*\\\\([a-zA-Z0-9_]+)$/', '$1', get_called_class()))->values($aEntity); if ($bOnDuplicateKeyUpdate === true) { $oOrm->onDuplicateKeyUpdate($aEntity); } $iResults = $oOrm->save(); } else { $iResults = $oOrm->update(preg_replace('/^.*\\\\([a-zA-Z0-9_]+)$/', '$1', get_called_class()))->set($aEntity)->where($aPrimaryKey)->save(); } return $iResults; }
/** * load * * @access public * @param bool $sDebug * @param string $sOtherPortail change the default portal to create the entity * @return array */ public function load($bDebug = false, $sOtherPortail = null) { $sQuery = $this->_prepare(); if ($bDebug === true) { echo $sQuery; } $aResults = self::connect()->query($sQuery)->fetchAll(\PDO::FETCH_ASSOC); $aReturn = array(); $i = 0; foreach ($aResults as $mKey => $aOneResult) { if (isset($aOneResult['.FOUND_ROWS()'])) { return $aOneResult['.FOUND_ROWS()']; } else { if ($this->_sFromAs) { $sPrefix = $this->_sFromAs . '.'; } else { $sPrefix = ''; } $aReturn[$i] = Entity::autoLoadEntity($this->_sFrom, $aOneResult, $sPrefix, true, $sOtherPortail); foreach ($this->_aJoin as $aJoin) { if (count($aJoin) > 0) { if ($aJoin['as']) { $sPrefixJoin = $aJoin['as'] . '.'; } else { $sPrefixJoin = ''; } $sPropertieName = 'get_' . $aJoin['table']; if (method_exists($aReturn[$i], $sPropertieName)) { $sPropertieName = $aJoin['table'] . '2'; $aReturn[$i]->{$sPropertieName} = Entity::autoLoadEntity($aJoin['table'], $aOneResult, $sPrefixJoin, true, $sOtherPortail); } else { $aReturn[$i]->{$aJoin}['table'] = Entity::autoLoadEntity($aJoin['table'], $aOneResult, $sPrefixJoin, true, $sOtherPortail); } } } $i++; } } $this->flush(); if ($this->_mWhere instanceof Where) { $this->_mWhere->flush(); } /** * Trigger on a model to initialize it every time you load each entity */ if (isset($aReturn[0]) && method_exists($aReturn[0], 'afterFetch')) { foreach ($aReturn as $iKey => $oOne) { $aReturn[$iKey]::afterFetch(); } } return $aReturn; }
/** * handle the request to do many actions on it * * @access public * @param array $aRequest request like $_POST * @return bool */ public function handleRequest(array $aRequest) : bool { if (!count($_POST)) { return true; } // Validation foreach ($this->_oForm->getElement() as $sKey => $sValue) { if (!$sValue instanceof self && !$this->_validate($sValue)) { return false; } } // Save if ($this->_oForm->getIdEntity() > 0 && $this->_oForm->getSynchronizeEntity() !== null && count($aRequest) > 0) { $sModelName = str_replace('Entity', 'Model', $this->_oForm->getSynchronizeEntity()); $oModel = new $sModelName(); $oEntity = new $this->_oForm->getSynchronizeEntity(); $sPrimaryKey = LibEntity::getPrimaryKeyNameWithoutMapping($oEntity); $sMethodName = 'set_' . $sPrimaryKey; call_user_func_array(array(&$oEntity, $sMethodName), array($this->_oForm->getIdEntity())); foreach ($this->_oForm->getElement() as $sKey => $sValue) { $sMethodName = 'set_' . $sValue->getName() . ''; call_user_func_array(array(&$oEntity, $sMethodName), array($aRequest[$sValue->getName()])); } $oEntity->save(); } else { if ($this->_oForm->getSynchronizeEntity() !== null && isset($aRequest) && count($aRequest) > 0) { $oEntity = new $this->_oForm->_sSynchronizeEntity(); foreach ($this->_oForm->getElement() as $sKey => $sValue) { $sMethodName = 'set_' . $sValue->getName() . ''; call_user_func_array(array(&$oEntity, $sMethodName), array($aRequest[$sValue->getName()])); } $this->_oForm->setIdEntityCreated($oEntity->save()); } } $this->_bHandleRequestActivate = true; $this->_aRequest = $aRequest; return true; }
/** * get global object form * * @access public * @return \stdClass */ public function getFormInObject() { $sExKey = null; if ($this->_iIdEntity > 0 && $this->_sSynchronizeEntity !== null && count($_POST) < 1) { $sModelName = str_replace('Entity', 'Model', $this->_sSynchronizeEntity); $oModel = new $sModelName(); $oEntity = new $this->_sSynchronizeEntity(); $sPrimaryKey = LibEntity::getPrimaryKeyNameWithoutMapping($oEntity); $sMethodName = 'findOneBy' . $sPrimaryKey; $oCompleteEntity = call_user_func_array(array(&$oModel, $sMethodName), array($this->_iIdEntity)); if (is_object($oCompleteEntity)) { foreach ($this->_aElement as $sKey => $sValue) { if ($sValue instanceof \Venus\lib\Form\Radio) { $sExKey = $sKey; $sKey = substr($sKey, 0, -6); } if ($sValue instanceof Form) { } else { $sMethodNameInEntity = 'get_' . $sKey; $mValue = $oCompleteEntity->{$sMethodNameInEntity}(); if ($sValue instanceof \Venus\lib\Form\Radio && method_exists($this->_aElement[$sExKey], 'setValueChecked')) { $this->_aElement[$sExKey]->setValueChecked($mValue); } else { if (isset($mValue) && method_exists($this->_aElement[$sKey], 'setValue')) { $this->_aElement[$sKey]->setValue($mValue); } } } } } } $oForm = new \StdClass(); $oForm->start = '<form name="form' . $this->_iFormNumber . '" method="post"><input type="hidden" value="1" name="validform' . $this->_iFormNumber . '">'; $oForm->form = array(); foreach ($this->_aElement as $sKey => $sValue) { if ($sValue instanceof Container) { $oForm->form[$sKey] = $sValue; } else { $oForm->form[$sKey] = $sValue->fetch(); } } $oForm->end = '</form>'; return $oForm; }
/** * classic method to delete one entities * * @access public * @param object $oEntityCriteria * @return object */ public function delete($oEntityCriteria) { $this->_checkEntity($oEntityCriteria); $aEntity = LibEntity::getAllEntity($oEntityCriteria, true); $this->orm->delete($this->_sTableName)->where($aEntity)->save(); return $this; }