コード例 #1
0
ファイル: yiilite.php プロジェクト: smokeelow/faicore
 public function getRelated($name, $refresh = false, $params = array())
 {
     if (!$refresh && $params === array() && (isset($this->_related[$name]) || array_key_exists($name, $this->_related))) {
         return $this->_related[$name];
     }
     $md = $this->getMetaData();
     if (!isset($md->relations[$name])) {
         throw new CDbException(Yii::t('yii', '{class} does not have relation "{name}".', array('{class}' => get_class($this), '{name}' => $name)));
     }
     $relation = $md->relations[$name];
     if ($this->getIsNewRecord() && !$refresh && ($relation instanceof CHasOneRelation || $relation instanceof CHasManyRelation)) {
         return $relation instanceof CHasOneRelation ? null : array();
     }
     if ($params !== array()) {
         $exists = isset($this->_related[$name]) || array_key_exists($name, $this->_related);
         if ($exists) {
             $save = $this->_related[$name];
         }
         if ($params instanceof CDbCriteria) {
             $params = $params->toArray();
         }
         $r = array($name => $params);
     } else {
         $r = $name;
     }
     unset($this->_related[$name]);
     $finder = new CActiveFinder($this, $r);
     $finder->lazyFind($this);
     if (!isset($this->_related[$name])) {
         if ($relation instanceof CHasManyRelation) {
             $this->_related[$name] = array();
         } elseif ($relation instanceof CStatRelation) {
             $this->_related[$name] = $relation->defaultValue;
         } else {
             $this->_related[$name] = null;
         }
     }
     if ($params !== array()) {
         $results = $this->_related[$name];
         if ($exists) {
             $this->_related[$name] = $save;
         } else {
             unset($this->_related[$name]);
         }
         return $results;
     } else {
         return $this->_related[$name];
     }
 }
コード例 #2
0
	/**
	 * Returns the related record(s).
	 * This method will return the related record(s) of the current record.
	 * If the relation is HAS_ONE or BELONGS_TO, it will return a single object
	 * or null if the object does not exist.
	 * If the relation is HAS_MANY or MANY_MANY, it will return an array of objects
	 * or an empty array.
	 * @param string $name the relation name (see {@link relations})
	 * @param boolean $refresh whether to reload the related objects from database. Defaults to false.
	 * @param array $params additional parameters that customize the query conditions as specified in the relation declaration.
	 * This parameter has been available since version 1.0.5.
	 * @return mixed the related object(s).
	 * @throws CDbException if the relation is not specified in {@link relations}.
	 * @since 1.0.2
	 */
	public function getRelated($name,$refresh=false,$params=array())
	{
		if(!$refresh && $params===array() && (isset($this->_related[$name]) || array_key_exists($name,$this->_related)))
			return $this->_related[$name];

		$md=$this->getMetaData();
		if(!isset($md->relations[$name]))
			throw new CDbException(Yii::t('yii','{class} does not have relation "{name}".',
				array('{class}'=>get_class($this), '{name}'=>$name)));

		Yii::trace('lazy loading '.get_class($this).'.'.$name,'system.db.ar.CActiveRecord');
		$relation=$md->relations[$name];
		if($this->getIsNewRecord() && !$refresh && ($relation instanceof CHasOneRelation || $relation instanceof CHasManyRelation))
			return $relation instanceof CHasOneRelation ? null : array();

		if($params!==array()) // dynamic query
		{
			$exists=isset($this->_related[$name]) || array_key_exists($name,$this->_related);
			if($exists)
				$save=$this->_related[$name];
			$r=array($name=>$params);
		}
		else
			$r=$name;
		unset($this->_related[$name]);

		$finder=new CActiveFinder($this,$r);
		$finder->lazyFind($this);

		if(!isset($this->_related[$name]))
		{
			if($relation instanceof CHasManyRelation)
				$this->_related[$name]=array();
			else if($relation instanceof CStatRelation)
				$this->_related[$name]=$relation->defaultValue;
			else
				$this->_related[$name]=null;
		}

		if($params!==array())
		{
			$results=$this->_related[$name];
			if($exists)
				$this->_related[$name]=$save;
			else
				unset($this->_related[$name]);
			return $results;
		}
		else
			return $this->_related[$name];
	}