Php 篝火编码点火器链接模型方法
我只是篝火/ci的新手。我试图通过链接bf/ci模型方法来重新创建以下sql语句:Php 篝火编码点火器链接模型方法,php,codeigniter,Php,Codeigniter,我只是篝火/ci的新手。我试图通过链接bf/ci模型方法来重新创建以下sql语句: SELECT prod_cat.cat_id, prod_cat.prod_id, prod.enabled, prod.name, prod.prod_id, FROM prod_cat, prod WHERE prod.
SELECT prod_cat.cat_id,
prod_cat.prod_id,
prod.enabled,
prod.name,
prod.prod_id,
FROM prod_cat, prod
WHERE prod.is_enabled = 1
AND prod_cat.cat_id =15
AND prod_cat.prod_id = prod.prod_id
ORDER BY prod.name DESC
我有一个bf/ci产品模型和一个产品类别模型
我知道我可以执行以下操作以获得与cat_id 15匹配的所有产品,例如:
$this->load->model('cat/prod_cat_model');
$records = $this->prod_cat_model->find_all_by('cat_id' => $cid);
类似地,我计划使用product表上的find_all_by来限制活动/启用的记录,如下所示:
$this->prod_model->find_all_by('enabled', 1);
public function where_in($field=NULL, $value=NULL)
{
if (!empty($field))
{
if (is_string($field))
{
$this->db->where_in($field, $value);
}
else if (is_array($field))
{
$this->db->where($field);
}
}
return $this;
}//end where()
首先,作为一个测试,我想通过向prod_模型传递一个数组,尝试将这两个代码结合起来,如下所示:
public function productsincategory($cid)
{
$this->load->model('cat/prod_cat_model');
$criteria = array(
'enabled' => 1,
'prod_id' => $this->prod_cat_model->find_all_by('cat_id', $cid)
);
$records = $this->prod_model->find_all_by($criteria) ;
}
我收到一条错误消息:
“where子句”中的未知列“Array”
从(prod
)中选择*,其中已启用
=1和prod\u id
=Array
这是有道理的。。。但我不知道如何修复它,使其类似于“select*from prod where enabled=1和prod_id in(prod_cat表中的prod id))
我也尝试过使用“where_in()”,但篝火似乎无法识别。
谢谢。我在产品模型中创建了自己的where_in()方法,如下所示:
$this->prod_model->find_all_by('enabled', 1);
public function where_in($field=NULL, $value=NULL)
{
if (!empty($field))
{
if (is_string($field))
{
$this->db->where_in($field, $value);
}
else if (is_array($field))
{
$this->db->where($field);
}
}
return $this;
}//end where()
在我的控制器中,我做了如下操作:
public function productsincategory($cid)
{
$this->load->model('cat/prod_cat_model');
$this->prod_cat_model->where('cat_id', $cid);
$records = $this->prod_cat_model->find_all();
$prod_ids = array();
foreach($records as $record)
{
array_push($prod_ids, $record->product_id);
}
$this->prod_model->where_in('prod_id', $prod_ids) ; //call our own where_in method
$records = $this->prod_model->find_all();
}
您是否尝试过使用php activerecord sparks?您不需要对每个模型进行查询。对于产品和类别,您只需要Category::find(1)->include('products')。当然,您必须在模型上设置关系。