Database CodeIgniter中的一对多DB关系
我有一些产品和一些图片。有更多的图像,它们有一个p_id。我如何获得多个图像?这是画廊的。 我当前的查询:Database CodeIgniter中的一对多DB关系,database,codeigniter,relationship,Database,Codeigniter,Relationship,我有一些产品和一些图片。有更多的图像,它们有一个p_id。我如何获得多个图像?这是画廊的。 我当前的查询: $this->db->select('prod_id, prod_name, prod_link, prod_description, prod_status, prod_price, brand_link, link, cat_link, normal, thumbnail'); $this->db->from('product'); $
$this->db->select('prod_id, prod_name, prod_link, prod_description, prod_status, prod_price, brand_link, link, cat_link, normal, thumbnail');
$this->db->from('product');
$this->db->join('category', 'cat_id = prod_category');
$this->db->join('brands', 'brand_id = prod_brand');
$this->db->join('images', 'p_id = prod_id');
$query = $this->db->get();
return $query->row_array();
这只给了我第一张图片和其他信息。如果我将其更改为result_array(),它也会在另一个数组中给出第二个数组。(以及其他产品的结果,这是没有意义的)。正如我前面提到的,您可以再次点击数据库以获取该产品的图像数组,然后将这些结果添加回原始查询数组
$this->db->select('prod_id, prod_name, prod_link, prod_description, prod_status, prod_price, brand_link, link, cat_link, normal');
$this->db->join('category', 'cat_id = prod_category');
$this->db->join('brands', 'brand_id = prod_brand');
$query = $this->db->get('product')->result_array();
// Loop through the products array
foreach($query as $i=>$product) {
// Get an array of products images
// Assuming 'p_id' is the foreign_key in the images table
$this->db->where('p_id', $product['prod_id']);
$images_query = $this->db->get('images')->result_array();
// Add the images array to the array entry for this product
$query[$i]['images'] = images_query;
}
return $query;
据我所知,除了使用
$this->db->query(“SELECT…”)写出所需的SQL语句外,使用Codeigniters活动记录库无法实现这一点代码>。你只需第二次点击数据库就可以得到每种产品的一系列图片。是的,删除了我的答案。我没有正确阅读问题,我以为你是从图片表中选择的。没错。我真正想要的是在我的产品-阵列中包含图像的阵列。谢谢!!只需稍加修改,即可完美工作!这就是我要找的。德克萨斯州