Example #1
0
 /**
  * You could remove this entity
  *
  * @access public
  * @return object
  */
 public function remove()
 {
     $mPrimaryKeyName = $this->_mPrimaryKeyName;
     $bInsertMode = false;
     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}());
         } else {
             $aPrimaryKey = array();
             foreach ($mPrimaryKeyName as $sKey => $sPrimaryKey) {
                 $sMethodPrimaryKey = 'get_' . $this->_mPrimaryKeyNameWithoutMapping[$sKey];
                 $aPrimaryKey[$sPrimaryKey] = $this->{$sMethodPrimaryKey}();
             }
         }
     }
     /**
      * 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 && isset($aForeignKey['foreign_key_options']['action']) && $aForeignKey['foreign_key_options']['action'] == self::CASCADE) {
                 $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) > 0) {
                         $oOrm = new Orm();
                         $oOrm->delete($aForeignKey['entity_join_name'])->where($oWhere)->save();
                     }
                 }
             }
         }
     }
     $oOrm = new Orm();
     $oOrm->delete(preg_replace('/^.*\\\\([a-zA-Z0-9_]+)$/', '$1', get_called_class()))->where($aPrimaryKey)->save();
     return $this;
 }