Difference between #1 and #2 of
Named scope - Get orphan AR without losing primary key

Revision #2 has been created by jonah on Jul 29, 2010, 7:43:54 PM with the memo:

fixed highlighting
« previous (#1) next (#3) »

Changes

Title unchanged

Named scope - Get orphan AR without loosing primary key

Category unchanged

Tutorials

Yii version unchanged

Tags unchanged

Content changed

Sometime, in a many-to-many relation, you need to retrieve AR that aren't linked. In practice, AR with null value in the join table. So you implements a criteria like this :
 
    
 
```php 
public function scopeUnused( $useAnd = true )
{
$join = " left outer join composed_by j on t.ID = j.ID ";
[...]
return $this;
}
``` You face the problem that your AR's pk are empty ?!! It come from the fact that the generated sql query returns by default all columns (select *). To overcome this, you need to explicitly specify the columns of your AR in the select property of the DBCriteria
 
    
 
```php 
public function scopeUnused( $useAnd = true )
{
$join = " left outer join composed_by j on t.ID = j.ID ";
[...]
return $this;
}

 
```
0 0
2 followers
Viewed: 9 383 times
Version: 1.1
Category: Tutorials
Tags:
Written by: cma
Last updated by: Steve Friedl
Created on: Jul 28, 2010
Last updated: 13 years ago
Update Article

Revisions

View all history