Php Kohana ORM从多个类别获取帖子
我使用的是Kohana 3.3 ORM。我定义了以下模型:Php Kohana ORM从多个类别获取帖子,php,orm,kohana,kohana-orm,Php,Orm,Kohana,Kohana Orm,我使用的是Kohana 3.3 ORM。我定义了以下模型: class Model_Post extends ORM { protected $_primary_key = 'ObjID'; protected $_has_many = array( 'categories' => array( 'model' => 'Category', 'through' => 'posts2categ
class Model_Post extends ORM {
protected $_primary_key = 'ObjID';
protected $_has_many = array(
'categories' => array(
'model' => 'Category',
'through' => 'posts2categories',
'foreign_key' => 'post_id',
),
);
}
及
现在,获取属于一个类别的所有帖子非常容易:
$posts = $categoriesQuery->where('category_id','=',1)->find()->posts->find_all();
我想知道如何获取属于类别1或类别2的所有帖子。
我尝试了很多事情,但没有一件奏效。我怎样才能让它工作?有没有一种方法可以使用ORM模块而不是直接的SQL查询来实现这一点?您可以在模型文章中添加一个函数,该函数将返回属于多个(或一个)类别的所有文章
public function in_categories($categories)
{
return $this->join("posts2categories")->on("posts2categories.post_id", "=", "posts.id")
->join("categories")->on("category.id", "=", "posts2categories.category_id")
->where("categories.id", "IN", $categories);
}
这将返回第1、3和5类的所有员额
ORM::factory("Post")->in_categories(array(1, 3, 5))->find_all();
您可以向Model_帖子添加一个函数,该函数将返回属于多个(或一个)类别的所有帖子
public function in_categories($categories)
{
return $this->join("posts2categories")->on("posts2categories.post_id", "=", "posts.id")
->join("categories")->on("category.id", "=", "posts2categories.category_id")
->where("categories.id", "IN", $categories);
}
这将返回第1、3和5类的所有员额
ORM::factory("Post")->in_categories(array(1, 3, 5))->find_all();
如果我正确理解了你的问题,你只需要两个条件:
...
$data = DB::select('*')->from('table_name')
->where_open()
->where('category_id','=',1)
->or_where('category_id','=',2)
->where_close()
...
如果我正确理解了你的问题,你只需要两个条件:
...
$data = DB::select('*')->from('table_name')
->where_open()
->where('category_id','=',1)
->or_where('category_id','=',2)
->where_close()
...