首页 > 解决方案 > 如何在 MySql 的单个名称列中添加多个表 id 并在 CakePHP 中使用 type_id 加入?

问题描述

我的问题出在我在CakePHP中的REST API中,用于最喜欢的 API。我有多个表,例如、和,以及一个表。我只想使用表中的单个字段和一个,例如 1 代表2 代表3 代表...quotesvideoaudiofavouritefav_idfavouritefav_typequotevideoaudio

如何fav_type在 CakePHP 中加入单个表?

标签: web-servicescakephp

解决方案


在 QuotesTable 的初始化函数中:

$this->hasMany('Favourites', [
    'foreignKey' => 'fav_id',
    'className' => 'Favourite',
    'dependent' => true,
    // Better to define a constant for this 1 somewhere
    'conditions' => ['fav_type' => 1],
]);

现在,当您get报价时,您可以->contain(['Favourites']).


推荐阅读