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

前端 未结 2 1453
小鲜肉
小鲜肉 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:12

    You should like this in big model for relation to small from big,

    'has_small'  => array(self::BELONGS_TO, 'small', 'smallid');
    
    0 讨论(0)
  • 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')
    
    0 讨论(0)
提交回复
热议问题