How can i use the OR operator in the ON clause when creating a JOIN?
public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
'users' => array(self::HAS_MANY, 'User', 'accountId'),
'fields' => array(self::HAS_MANY, 'Field', 'accountId', 'on' => ' [b]OR[/b] accountId IS NULL'),
);
}
I want to fetch all fields with accountId related to this model instance PLUS those with ‘accountId IS NULL’
I took a look at the code and saw that the AND operator is hardcoded (CActiveFinder.php on line 1222). =/
public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
'users' => array(self::HAS_MANY, 'User', 'accountId'),
'labels' => array(self::HAS_MANY, 'Label', 'accountId'),
'fields' => array(self::HAS_MANY, 'Field', '', 'on' => 't.id = fields.accountId OR fields.accountId IS NULL'),
);
}
This code give me follow error:
CDbCommand failed to execute the SQL statement: SQLSTATE[42S22]: Column not found: 1054 Unknown column 't.id' in 'where clause'
If I just remove the third element of the relation definition, it throw me another error.
Active record "Account" has an invalid configuration for relation "fields". It must specify the relation type, the related active record class and the foreign key.
Which model defines the relationship we’re talking about?
Examples
$crit = new CDbCriteria;
$crit->with = 'fields';
$crit->together = true;
$dataprovider = CActiveDataProvider('Primary', $crit);
// or
$models = Primary::model()->with('fields')->findAll();
The SQL you posted is probably not the right one. It uses field as primary table (which is unexpected here), does have a WHERE clause (condition in Yii), but no JOIN with a field table (as specified in the fields relation we’re talking about).
First of all you should probably read this section of the Definitive Guide to Yii.
Edit: I may be you posted the correct SQL assuming lazy loading (I never examined the generated SQL from a lazy load operation). My examples above use eager loading.
Ok, sorry. Let me explain further, and apologize in advance for any mistakes in my english.
I have accounts and each account has fields. However there are some fields that are common to all accounts, so the column "accountid" is null for these fields.
So when I want to fetch the fields for an account, I need to fetch all the fields where the column "accountid"" equals the primary key of the account plus those that are null.
So I created a model called "account" and defined the following relation:
However, when I do "Account::model()->findByPk(1)->fields" the fields whose "accountid" are null are not returned.
So I tried to put some clauses using the ‘on’ and ‘condition’ in the definition of the relation, but without success, because I need an ‘OR’ operator, not an ‘AND’.
My first answer should work with eager loading. You didn’t show the source that generates the actual query, but at least I can advice you to add with and together, for the table alias to work.