伙计们
我一直在我的项目中使用这个模型。但是,现在我面临着由于 with 方法的重复查询导致的性能噪音,当然还需要在相关对象上使用 Where 子句进行内部连接,我怎么能这样做那个?
我有 Profile
、Media
、Product
表相关。
当我想获得所有具有配置文件、媒体和详细信息的产品时,我使用下面的代码。并且,它的工作除了 with 创建重复的选择语句。
$all_products = $this->with('medias')->with('profile')->get_all();
现在,我希望所有产品的资料都经过验证。
select * from products
inner join profile as p on p.id = product.profile_id
inner join media as m on m.product_id=p.product_id
where profile.is_verfied='true';
我怎样才能使用我的 MY_MODEL 方式或稍作修改来做到这一点。
最佳答案
我也使用@Jamie Rumbelow 的基础模型,几乎没有修改(一两个)。在加入时,它有一个很棒的触发方法,您可以使用:before_get
observer。在您的情况下,只需添加一个观察者,您就可以像这样从所需的表中加入:
在您的模型构造函数中:
public function __constrcutor()
{
array_unshift(
$this->before_get,
'join_from_profile_and_media'
);
// Then
parent::__construct();
}
观察者将是:
protected function join_from_profile_and_media()
{
$this->db->join('profile', 'profile.id = product.profile_id');
$this->db->join('media', 'media.product_id = profile.product_id');
}
现在无论何时调用 get()
、get_many()
、get_by()
或 get_many_by()
所有的 table 都是联合的。
关于php - MY_Model jamierumbelow,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30320540/