Yii 关系失败,索引 > 5
我有一个非常简单的关系定义如下(aId
和 bId
是每个表的主键)。
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'),
);
}
}
只要bId <= 5
,我就可以通过$bModel->a
访问A,没有任何问题。奇怪的是 bId >; 5
,$bModel->a
为空。我已经检查了 $bModel->aId
中的 bId >; 5
并且外键是正确的。我什至可以使用 $aModel = A::model()->findByPk($bModel->aId);
访问 A。我还可以手动编辑数据库表中的 bId
,这会产生相同的结果。
我不知道是什么导致主键大于五的关系失败。对于故障排除有什么建议吗?我不知所措。
已编辑
事实证明我没有正确使用该关系。我应该使用 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 导致 B 使用 bId
来索引 A。因为我的数据库中有五个适用于 bID
bID
的 A 实例。 5
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 bId
s 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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在应用程序配置中启用查询日志记录以查看到底发生了什么。
手动运行这些查询时您得到任何结果吗?
(我会将其作为评论而不是答案发布,但似乎我不能)
Enable query logging in your application config to see what exactly is happening.
Do you get any results when manually running those queries?
(I'd post this as a comment rather than an answer, but it seems I can't)
我推荐你使用这个 -> Yii 调试工具栏(它是我在乌克兰的朋友创建的)。
你能提供mysql结构+一些示例数据吗?谢谢。
I recommend you to use this -> Yii Debug Toolbar (it is created by my friend here in Ukraine).
Can you provide mysql structure + some example data. Thanks.