在一条记录上加入一个表的多行(Codeigniter 3)

时间:2015-12-28 11:28:13

标签: php mysql codeigniter

我想建立一个查询,它返回的产品包含所有图像,而不仅仅是一个图像。
这是我获取产品的模型

function get_latest($id){
    $this->db->select('*');
    $this->db->from('products');
    $this->db->join('products_pictures', 'products_pictures.id_product = products.id_product', 'left');
    $this->db->join('thumbs', 'thumbs.id_thumb = products_pictures.id_thumb', 'left');
    $this->db->join('images', 'images.id_image = thumbs.id_image', 'left');
    $this->db->order_by('date_c', 'DESC');
    $this->db->limit($id);
    $query = $this->db->get();
    return $query->result();
    }

这是输出

  0 => 
    object(stdClass)[35]
      public 'id_product' => string '1000' (length=4)
      public 'name' => string 'Graham Corners' (length=14)
      public 'description' => string 'Et ea quaerat totam temporibus quis. Ut fugiat blanditiis vel corporis minus veniam. Consequatur corporis sint sed assumenda itaque ratione ipsum distinctio. Voluptate autem quo sunt rerum.' (length=189)
      public 'date_c' => string '2015-12-28 03:12:59' (length=19)
      public 'date_u' => null
      public 'id_category' => string '7' (length=1)
      public 'id_user' => string '173' (length=3)
      public 'alias' => string 'graham-corners' (length=14)
      public 'active' => string '1' (length=1)
      public 'id_product_thumb' => string '4996' (length=4)
      public 'id_thumb' => string '4996' (length=4)
      public 'thumb_path' => string 'http://lorempixel.com/250/150/?22290' (length=36)
      public 'id_image' => string '4996' (length=4)
      public 'image_path' => string 'http://lorempixel.com/1024/768/?77489' (length=37)
  1 => 
    object(stdClass)[36]
      public 'id_product' => string '1000' (length=4)
      public 'name' => string 'Graham Corners' (length=14)
      public 'description' => string 'Et ea quaerat totam temporibus quis. Ut fugiat blanditiis vel corporis minus veniam. Consequatur corporis sint sed assumenda itaque ratione ipsum distinctio. Voluptate autem quo sunt rerum.' (length=189)
      public 'date_c' => string '2015-12-28 03:12:59' (length=19)
      public 'date_u' => null
      public 'id_category' => string '7' (length=1)
      public 'id_user' => string '173' (length=3)
      public 'alias' => string 'graham-corners' (length=14)
      public 'active' => string '1' (length=1)
      public 'id_product_thumb' => string '4997' (length=4)
      public 'id_thumb' => string '4997' (length=4)
      public 'thumb_path' => string 'http://lorempixel.com/250/150/?87585' (length=36)
      public 'id_image' => string '4997' (length=4)
      public 'image_path' => string 'http://lorempixel.com/1024/768/?76756' (length=37)

因为我不想获取所需产品的所有图像我需要构建一个查询,该查询将输出存储在某些数组或其他内容中的对象产品下的所有产品图像 期望的输出对象应该与此类似

  0 => 
    object(stdClass)[35]
      public 'id_product' => string '1000' (length=4)
      public 'name' => string 'Graham Corners' (length=14)
      public 'description' => string 'Et ea quaerat totam temporibus quis. Ut fugiat blanditiis vel corporis minus veniam. Consequatur corporis sint sed assumenda itaque ratione ipsum distinctio. Voluptate autem quo sunt rerum.' (length=189)
      public 'date_c' => string '2015-12-28 03:12:59' (length=19)
      public 'date_u' => null
      public 'id_category' => string '7' (length=1)
      public 'id_user' => string '173' (length=3)
      public 'alias' => string 'graham-corners' (length=14)
      public 'active' => string '1' (length=1)
      public 'images' => array (
             [0] => stdClass Object
                 public 'id_thumb' => string '4998' (length=4)
                 public 'thumb_path'=> string 'http://lorempixel.com/250/150/?87585' (length=36)
                 public 'id_image' => string '4998' (length=4)
                 public 'image_path' => string 'http://lorempixel.com/1024/768/?76756' (length=37)
             [1] => stdClass Object
                 public 'id_thumb' => string '4997' (length=4)
                 public 'thumb_path'=> string 'http://lorempixel.com/250/150/?87585' (length=36)
                 public 'id_image' => string '4997' (length=4)
                 public 'image_path' => string 'http://lorempixel.com/1024/768/?76756' (length=37)
                 ........................

提前谢谢

1 个答案:

答案 0 :(得分:2)

对我来说,您应该只将id_thumb组合为id_thumbs,而不是将所有图片数据合并到images列中。例如:

function get_latest($id)
{
    $this->db->select('products.*,product_pictures.*');
    $this->db->select('GROUP_CONCAT(product_pictures.id_thumb) as id_thumbs',FALSE);
    $this->db->from('products');
    $this->db->join('products_pictures', 'products_pictures.id_product = products.id_product', 'left');
    $this->db->order_by('date_c', 'DESC');
    $this->db->group_by('id_product');
    $this->db->limit($id);
    $query = $this->db->get();
    return $query->result();
}

在你的模型中创建一个名为get_image_data_by_id_thumb的函数,如下所示:

function get_image_data_by_id_thumb($id_thumb='')
{
    if($id_thumb=='')
    {
        return false;
    }
    $this->db->where('id_thumb', $id_thumb);
    $this->db->join('images', 'images.id_image = thumbs.id_image', 'left');
    $sql = $this->db->get('thumb');
    $obj = $sql->row();
    if($obj)
    {
        return $obj;
    }
}

输出将是:

0 => 
    object(stdClass)[35]
      public 'id_product' => string '1000' (length=4)
      public 'name' => string 'Graham Corners' (length=14)
      public 'description' => string 'Et ea quaerat totam temporibus quis. Ut fugiat blanditiis vel corporis minus veniam. Consequatur corporis sint sed assumenda itaque ratione ipsum distinctio. Voluptate autem quo sunt rerum.' (length=189)
      public 'date_c' => string '2015-12-28 03:12:59' (length=19)
      public 'date_u' => null
      public 'id_category' => string '7' (length=1)
      public 'id_user' => string '173' (length=3)
      public 'alias' => string 'graham-corners' (length=14)
      public 'active' => string '1' (length=1)
      public 'id_thumbs' => '4998,4997,...'

然后您可以使用explode方法按id_thumb获取图像数据来取回:

$arr_id_thumbs = explode(",",$id_thumbs);
foreach($arr_id_thumbs as $row)
{
    $obj_image_data = $this->model_name->get_image_data_by_id_thumb($id_thumb);
    $thumb_path = $obj_image_data->thumb_path;  
    $image_path = $obj_image_data->image_path;
    .......................
}

希望它有所帮助。