I have a very simple relation defined as follows (aId and bId are the primary keys for each table).
class A extends CActiveRecord
{
// @var int32 $aId
}
class B extends CActiveRecord
{
// @var int32 $bId
// @var int32 $aId
public function relations()
{
return array(
'a'=>array(self::HAS_ONE, 'A', 'aId'),
);
}
}
As long as bId <= 5, I can access A through $bModel->a without any problems. What’s strange is for bId > 5, $bModel->a is null. I’ve checked $bModel->aId for bId > 5 and the foreign key is correct. I can even access A with $aModel = A::model()->findByPk($bModel->aId);. I can also manually edit my bIds in the database table, which produces the same result.
I have no idea what’s causing the relation to fail for primary key’s greater than five. Any suggestions for troubleshooting? I’m at a loss.
EDITED
It turns out I wasn’t using the relation properly. I should have used BELONGS_TO.
class B extends CActiveRecord
{
// @var int32 $bId
// @var int32 $aId
public function relations()
{
return array(
'a'=>array(self::HAS_ONE, 'A', 'aId'),
);
}
}
HAS_ONE was causing B to use bId to index A. Since I had five instances of A in my database that worked for bID < 5
Enable query logging in your application config to see what exactly is happening.
Do you get any results when manually running those queries?
'components' => array( 'db' => array( (..) 'enableParamLogging' => true, ), 'log' => array( 'class' => 'CLogRouter', 'routes' => array( // Show log messages on web pages array( 'class' => 'CWebLogRoute', 'categories' => 'system.db.CDbCommand', //queries 'levels' => 'error, warning, trace, info', //'showInFireBug' => true, ),(I’d post this as a comment rather than an answer, but it seems I can’t)