yii - using relation HAS_ONE to get data from the related table to display in list page

前端 未结 2 1454
小鲜肉
小鲜肉 2021-01-26 00:39

I have two tables

big (id, bigs_name, smallid),

small(id, smallguys_name)

therefore two models - big and small

i have used the following relatio

2条回答
  •  说谎
    说谎 (楼主)
    2021-01-26 01:26

    The problem is that with a "HAS_ONE" relationship, it is trying to query the SMALL table for SMALLID (has_small.smallid) instead of ID (has_small.id). If BIG really has a "HAS_ONE" relationship with SMALL, you need to put the foreign key for BIG in SMALL (flip flop them), like so:

    big (id, bigs_name)

    small(id, smallguys_name, bigid)

    'has_small'  => array(self::HAS_ONE, 'small', 'bigid')
    

    Otherwise, if you want to keep your DB structure the same, I would use the BELONGS_TO relation like you mentioned in your comment:

    'has_small'  => array(self::BELONGS_TO, 'small', 'smallid')
    

提交回复
热议问题