简体   繁体   中英

codeigniter join multiple rows where

I'm trying to code a search function for a site written on CodeIgniter but have trouble with the join statement in the model.

A controller passes an array of search criteria to the model function: get_sales($search_criteria);

function get_sales($search_criterie){

    $this->db->join('sales_prices', 'sales_prices.sale_id = sales.sale_id', 'left');
    $this->db->join('sales_sizes', 'sales_sizes.sale_id = sales.sale_id', 'left');
    $query = $this->db->get_where('sales', $search_criterie);
    $sale_data = $query->result_array();

}

I'm running into two problems:

1) I need a WHERE clause in the sales_sizes join. Right now it joins sales_sizes.sale_id ON sales.sale_id but I want to be able to filter by size via the $search_criteria array. How can I do this?

2) Every sales usually has multiple sizes and I want the output of the query formatted like:

Array
(
    [sale_id] => 1
    [sale_category] => shoes
    [sale_price] => 29.99
    [sale_sizes] => Array
        (
            [0] => 10
            [1] => 10.5
            [2] => 11

        )

)

I've tried some foreach loops to format the output but can't get it to work. Hope somebody can help.

Update:

How do I process the following query result into a format like the one above?

Array
(
    [0] => Array
        (
            [sale_id] => 1
            [sale_category] => shoes
            [sale_price] => 29.99
            [sale_size] => 10
        )

    [1] => Array
        (
            [sale_id] => 1
            [sale_category] => shoes
            [sale_price] => 29.99
            [sale_size] => 10.5
        )

    [2] => Array
        (
            [sale_id] => 1
            [sale_category] => shoes
            [sale_price] => 29.99
            [sale_size] => 11    
        )

) 

I am personally not a big fan of using the active record as given. I find that it often restricts the programmer from writing more complex queries. If you find that this is true as well you could rewrite your query as followed:

$this->db->query('SELECT * FROM `sales_prices`, `sales_sizes` WHERE `sales_prices`.`sale_id` =   `sales`.`sale_id` AND `sales_sizes`.`sale_id` = `sales`.`sale_id` AND `sales`  = ?', array($search_criterie));
$sale_data = $query->results();

This will generate the same result and will also parameterize your query. Note that the question mark corresponds in order to values in your array that will make up the second parameter of you $this->db->query() function.

Try this type of query

$this->db->select('places.*, category.*')
            ->from('places')
            ->join('category', 'places.category_id = category.category_id', 'left')
            ->join('places_reviews', 'places_reviews.place_id = places.id', 'left')
            ->where('places.category_id', $category_id)
            ->limit($limit, $offset)
            ->order_by($sort_by, $sort_order);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM