New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Default Model Instance on Empty Relation #1886
Comments
I don't think it will fit framework well. You can always use a method like |
No I can't if i have long relation chains like |
In summary: <?php
// data definition, model
class User extends CActiveRecord
{
public $id;
public $first_name;
public function relations()
{
return array(
'favorites'=>array(self::HAS_MANY,'Favorite','user_id'),
// neat way of getting single favorite related record
'favorite'=>array(self::HAS_ONE,'Favorite','user_id'),
);
}
}
class Favorite extends CActiveRecord
{
public $user_id;
public $company_id;
public function relations()
{
return array(
'company'=>array(self::BELONGS_TO,'Company','company_id',
'condition'=>'company.deleted=0'),
);
}
}
class Company extends CActiveRecord
{
public $id;
public function relations()
{
return array(
'initiator'=>array(self::HAS_ONE,'Initiator','company_id'),
);
}
public function getInitiatorName()
{
if($this->initiator)
{
return $this->initiator->name;
}
}
}
class Initiator extends CActiveRecord
{
public $id;
public $company_id;
public $name;
}
// querying, controller
$criteria=new CDbCriteria();
$criteria->with=array(
'favorite'=>array('select'=>false),
'favorite.company'=>array('select'=>false),
'favorite.company.initiator'=>array('select'=>'name'),
);
$criteria->select=array('t.id','t.first_name');
$criteria->addColumnCondition(array('t.id'=>Yii::app()->user->id));
$user=User::model()->find($criteria); // passed to the view
$favorite=$user->favorite; // passed to the view
// displaying, view
echo $user->first_name;
if($favorite!==null && $favorite->company!==null):
echo $favorite->company->initiatorName;
endif; Any situation could be simplified as i described above. |
You didn't understand me, sorry. |
I did. You're using relations improperly. We can continue discussion in Skype or Jabber if you want. |
http://www.soberit.hut.fi/mmantyla/badcodesmellstaxonomy.htm
|
I want to set new param (e.g. 'default', 'defaultCriteria', etc.) to CActiveRecord.relations() array, which say to CActiveFinder (or CActiveRecord?) to load default model instance (or set of instances in some relation types) in case when nothing is found in CActiveRecord.getRelated().
Use case:
The text was updated successfully, but these errors were encountered: