Exemple #1
0
 /**
  * create a join in one to one
  *
  * @access public
  * @param string $sPrimaryKeyName
  * @param string $sEntityJoinName
  * @param string $sForeignKeyName
  * @param string $sNamespaceEntity
  * @param array $aOptions
  * @return object
  */
 public function hasOne($sPrimaryKeyName, $sEntityJoinName, $sForeignKeyName, $sNamespaceEntity, array $aOptions = array())
 {
     $this->_aJoins[$sEntityJoinName] = function ($mParameters = null) use($sPrimaryKeyName, $sEntityJoinName, $sForeignKeyName, $sNamespaceEntity) {
         if (!isset($this->{$sEntityJoinName})) {
             $oOrm = new Orm();
             $oOrm->select(array('*'))->from($sEntityJoinName);
             if ($mParameters) {
                 $aWhere[$sForeignKeyName] = $mParameters;
             } else {
                 $sMethodName = 'get_' . $sPrimaryKeyName;
                 $aWhere[$sForeignKeyName] = $this->{$sMethodName}();
             }
             $this->{$sEntityJoinName} = $oOrm->where($aWhere)->load(false, $sNamespaceEntity . '\\');
         }
         return $this->{$sEntityJoinName}[0];
     };
     if (isset($aOptions['foreignKey']) && !isset($this->_aForeignKey[$sEntityJoinName])) {
         $this->_aForeignKey[$sEntityJoinName] = array('foreign_key' => $sPrimaryKeyName, 'entity_join_name' => $sEntityJoinName, 'primary_key_name' => $sForeignKeyName, 'foreign_key_options' => $aOptions['foreignKey'], 'has_one' => 1);
     }
 }
Exemple #2
0
 /**
  * create a join in many to many
  *
  * @access public
  * @param string $sPrimaryKeyName
  * @param string $sEntityJoinName
  * @param string $sForeignKeyName
  * @param string $sNamespaceEntity
  * @param unknown $sManyToManyKeyName
  * @param unknown $sManyToManyTableName
  * @param array $aOptions
  * @return object
  */
 public function hasManyToMany($sPrimaryKeyName, $sEntityJoinName, $sForeignKeyName, $sNamespaceEntity, $sManyToManyKeyName, $sManyToManyTableName, array $aOptions = array())
 {
     $this->_ahasMany[$sEntityJoinName] = function ($mParameters = null) use($sPrimaryKeyName, $sEntityJoinName, $sForeignKeyName, $sManyToManyKeyName, $sManyToManyTableName, $sNamespaceEntity) {
         if (!isset($this->{$sEntityJoinName})) {
             $oOrm = new Orm();
             $oOrm->select(array('*'))->from($sEntityJoinName);
             if ($mParameters) {
                 $aWhere[$sForeignKeyName] = $mParameters;
             } else {
                 $sMethodName = 'get_' . $sPrimaryKeyName;
                 $aWhere[$sForeignKeyName] = $this->{$sMethodName}();
             }
             $this->{$sEntityJoinName} = $oOrm->where($aWhere)->load(false, $sNamespaceEntity . '\\');
         }
         $aResults = array();
         foreach ($this->{$sEntityJoinName} as $iKey => $oOne) {
             $oOrm = new Orm();
             $oOrm->select(array('*'))->from($sManyToManyTableName);
             if ($mParameters) {
                 $aWhere[$sManyToManyKeyName] = $mParameters;
             } else {
                 $sMethodName = 'get_' . $sManyToManyKeyName;
                 $aWhere[$sManyToManyKeyName] = $this->{$sMethodName}();
             }
             $aResults[] = $oOrm->where($aWhere)->load(false, $sNamespaceEntity . '\\');
         }
         return $aResults;
     };
 }