Exemple #1
0
 /**
  * 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;
 }
Exemple #2
0
 /**
  * update Entity and get it
  *
  * @access public
  * @param  object $oEntityCriteria
  * @return mixed
  */
 public function updateAndGet($oEntity)
 {
     $sEntityNamespace = preg_replace('/^(.*)Model\\\\.+$/', '$1Entity\\', get_called_class());
     $mResult = $this->update($oEntity);
     if ($result) {
         $aEntity = get_object_vars(LibEntity::getRealEntity($oEntity));
         $mPrimaryKey = LibEntity::getPrimaryKeyName($aEntity);
         $mResult = $this->orm->select(array('*'))->from($this->_sTableName)->where(array($mPrimaryKey => $aEntity[$mPrimaryKey]))->load(false, $sEntityNamespace);
         if ($this->_isFilter()) {
             foreach ($mResult as $iKey => $oValue) {
                 $mResult[$iKey] = $this->_applyFilter($oValue);
             }
         }
     }
     return $mResult;
 }