Yii 2.嵌套M到N的关系

时间:2016-09-01 13:00:18

标签: php yii2 yii2-basic-app

问题

我有多对多的关系。是否可以通过多个联结表设置关系(到'标签'表格'医院')?

enter image description here

我现在有什么

我只能通过一个联结表将关系设置为'治疗'表。

医院模型

   /**
     * Getting treatments of the hospital record.
     *
     * @return ActiveQuery
     */
    public function getTreatments()
    {
        return $this->hasMany(Treatment::className(), ['id' => 'id_treatment'])
                    ->viaTable(HospitalTreatmentPrice::tableName(), ['id_hospital' => 'id']);
    }

2 个答案:

答案 0 :(得分:1)

我已经在类似场景中使用了混合方法,我为远程表建立了关系,然后在层次结构顶部的joinWith中使用关系,因为这更为明显。

我的示例使用了一个属性 - >许多选项(通过property_options) - >许多OptionGroups(通过option_group_link)

房产可能有像“加热池”这样的选项。或者'私人泳池'但是我想列出所有带有Pools的属性,所以我有一个option_group的Pools,这个组合在一起的游泳池'选项。

class Property extends \yii\db\ActiveRecord
{
    public function getPools()
    {
        return $this->hasMany(Option::className(), ['id' => 'option_id'])
                ->viaTable('property_options as pog',['property_id' => 'id'])
                ->joinWith('optionGroupPools')
                ->where('option_group_name is not null'); /** make sure the relation is NOT NULL **/
    }

...
}

class Option extends \yii\db\ActiveRecord
{
     // standard relation option -> option_group with via
    public function getOptionGroups()
    {
        return $this->hasMany(OptionGroup::className(), ['id' => 'option_group_id'])
                ->viaTable( 'option_group_link',['option_id' => 'id']);        
    }    

    // Specific relation for this condition
    public function getOptionGroupPools()
    {
        return $this->hasMany(OptionGroup::className(), ['id' => 'option_group_id'])
                ->viaTable( 'option_group_link',['option_id' => 'id'])
                ->onCondition((['option_group.id' => Option::OPTION_GROUP_POOL]));
    }
}

class PropertyController extends BackendController
{
    public function actionTest()
    {

        $props = \common\models\Property::find()
            ->select('properties.id,ref,')
            ->joinWith('pools')
            ->asArray()
            ->all();

        var_dump( $props );
    }

答案 1 :(得分:0)

现在我的 医院 模型看起来像这样

class Hospital extends ActiveRecord
{
       /**
         * Getting treatments of the hospital record.
         *
         * @return ActiveQuery
         */
        public function getTreatments()
        {
            return $this->hasMany(Treatment::className(), ['id' => 'id_treatment'])
                        ->viaTable(HospitalTreatmentPrice::tableName(), ['id_hospital' => 'id']);
        }


        /**
         * Getting tags of the treatment.
         *
         * @return ActiveQuery
         */
        public function getTags()
        {
            return $this->getTreatments()->joinWith(Tag::tableName());
        }
}

治疗 模型

class Treatment extends ActiveRecord
{
    public static function tableName()
    {
        return 'treatments';
    }


    public function getTags()
    {
        return $this->hasMany(Tag::className(), ['id' => 'id_tag'])
                    ->viaTable(TreatmentTag::tableName(), ['id_treatment' => 'id']);
    }
}