【问题标题】:Yii active record- how to relate a model to another third modelYii 活动记录 - 如何将一个模型与另一个第三个模型相关联
【发布时间】:2012-12-07 13:40:20
【问题描述】:

用真实的话.. 我有餐厅模型,餐厅模型有很多食物,每种食物都有一个类别,

所以我的问题是如何通过餐厅模型获取所有使用过的类别

非常感谢。

【问题讨论】:

    标签: php mysql activerecord yii


    【解决方案1】:

    http://www.yiiframework.com/doc/guide/1.1/en/database.arr#relational-query-with-through

    类似:

    public function relations()
    {
      return array(
        'foods' => array(self::HAS_MANY, 'Food', 'restaurant_id'),
        'categories' => array(self::HAS_MANY, 'Category', array('category_id'=>'id'), 
                           'through' => 'foods'),
      );
    }
    

    应该在您的Restaurant 模型中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-06-04
      • 2012-05-02
      • 1970-01-01
      • 1970-01-01
      • 2010-10-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多