MY_Model jamierumbelow

MY_Model jamierumbelow

伙计们

我一直在我的 project.But 中使用这个模型,现在我面临着由于 with 方法的重复查询导致的性能噪音,当然还需要在相关对象上制作和内部加入 with Where 子句以及如何我可以那样做吗?

我有 Profile ,Media,Product table 相关。 当我想获得所有产品的配置文件、媒体和详细信息时,我在 code.And 下面使用,它的工作除了 with createsrepeated select statements.

$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 方式或稍加修改就可以做到这一点。

丹妮拉德,

有许多可用的缓存技术。在我的项目中,我们也使用 Redis, Memcached。还为 Javascripts、CSS、图像等使用客户端(网站)缓存。

Redis/Memcached 是非常强大的工具。 Facebook 正在使用 Memcached 实现非常快速的输出和低负载的数据库服务器。

希望对您有所帮助!!

如果您遇到性能问题,请不要使用 with()!这是一种粗略的关系管理方式,仅用于原型设计或简单关系。

我建议手动进行连接:

$products = $this->select('products.*')
    ->join('profile', 'p.id = products.profile_id', 'inner')
    ->where('profile.is_verified', true)
    ->get_all();

我也使用@Jamie Rumbelow 的基础模型,几乎没有修改(一两个)。在加入时,它有一个很棒的触发方法,您可以使用:before_get观察者。在你的情况下,只需添加一个观察者,你就可以从需要的表中加入,如下所示:

在您的模型构造函数中:

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(),所有表都是联合的。