CActiveFinder Error

I'm not sure if this is a bug, or if I'm just not quite doing something correctly.

Basically I have 3 tables: Product, Category, and ProductCategoryLink.

ProductCategoryLink table has the fields productId and categoryId.

I used the shell command to generate my models and it setup the relation:

'productCategoryLinks' => array(self::HAS_MANY, 'ProductCategoryLink', 'productId'),

Now when I do this in my controller:

$model = Product::model()->findByPk(1);

$links = $model->productCategoryLinks;

I get the error:

PHP Error

Description

Invalid argument supplied for foreach()

Source File

/data/web/vhosts/yii-1.0.6/framework/db/ar/CActiveFinder.php(586)

Now when I wrap the loop with: if(is_array($this->_pkAlias)) { }

I get back the relations that I was expecting and everything works.

Thanks for your help/insight.

Ok, I must be doing something wrong, I thought I was getting my relations back as I wanted, but its only giving me the first relating record back when there should be several.

Does your ProductCategoryLink table have primary keys?

Nope, each field just has an index.

Thanks, I added the PK on the fields and its working now.

I’m not used to code actually changing behavior based on my DB Schema.  :)