Even though it might not be the most elegant solution, i’ve used this approach here:
Add properties to your Category model, for values from the connecting table
Create a custom method in your target model to fetch the categories
So it could look like:
class Category extends CActiveRecord
{
public $position; // Usually empty, except for $post->getCategories();
...
In your Post class:
private $_categories;
public function getCategories()
{
if ($this->_categories===null)
$this->_categories=Category::model()->findAll(array(
'select'=>'t.*, pc.position AS position',
'join'=>"INNER JOIN tbl_post_category pc ON pc.category_id=t.id AND pc.post_id=$this->ID"
));
return $this->_categories;
}
You then fetch the Categories with $post->getCategories(); and can access the $category->position from the connecting table.
Also check out the new ‘through’ feature in ARR. You can build an alternative solution with it (namely by replacing MANY_MANY with 2 HAS_MANY relations. The second one uses ‘through’ on the first one).
Now what can I do with the "through" function? I want to get all Skills of the User which has a quality > 3, assuming quality is an attribute of the relationship.
I didn’t test this, but i think, you now can specify conditions for your skillRels relation, that will also affect, which skills are pulled in into your result. Something like this should probably work:
it says "Relation "user" is not defined in active record class "User"."
So I removed the “user” in the query. Now I get “Column not found: 1054 Unknown column ‘skills.price’ in ‘where clause’.” But even if I adjust the table name it won’t find it.
I should not post messages so late at night . Of course the relations in with() should match the name of your relations in your User model. I’ve updated the post above.