CodeIgniter Forums
db_join adds new record to left table - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: db_join adds new record to left table (/showthread.php?tid=842)



db_join adds new record to left table - alfred.laggner - 01-22-2015

here is my model function
Code:
class Tincture_model extends CI_Model {
    
    function get_records()
    {
        $this->db->select('tincture.*,ps_product_lang.name as ps_name,tincture.name as tincture_name ' );
        $this->db->join('ps_product_lang', 'tincture.ps_product_id = ps_product_lang.id_product ','left');
        $this->db->group_by('tincture.tincture_id');
        $query = $this->db->get('tincture');
        return  $query->result();
    }
Every time I run it it duplicates the last record of table tincture.


RE: db_join adds new record to left table - Avenirer - 01-22-2015

What do you mean by "it duplicates"? And what is the last record? can we se the query result?


RE: db_join adds new record to left table - alfred.laggner - 01-22-2015

Thank you!

This is the result of last_query(): (captured right before the return in the model function)
Code:
SELECT `tincture`.*, `ps_product_lang`.`name` as `ps_name`, `tincture`.`name` as `tincture_name` FROM `tincture` LEFT JOIN `ps_product_lang` ON `tincture`.`ps_product_id` = `ps_product_lang`.`id_product` GROUP BY `tincture`.`tincture_id`

This is the result of print_r($query->result()): (already inserted one record)
Code:
Array ( [0] => stdClass Object ( [tincture_id] => 1 [ps_product_id] => 13 [name] => Sibirian Ginseng [is_product] => 1 [ps_name] => Siberian Ginseng [Eleutherococcus senticosus] [tincture_name] => Sibirian Ginseng ) [1] => stdClass Object ( [tincture_id] => 2 [ps_product_id] => 10 [name] => Pau d'Arco [is_product] => 1 [ps_name] => Pau d'Arco [Tabebuia impetiginosa] [tincture_name] => Pau d'Arco ) [2] => stdClass Object ( [tincture_id] => 3 [ps_product_id] => 1 [name] => Cat's Claw [is_product] => 1 [ps_name] => Cat's Claw uña de gato [Uncaria tomentosa] [tincture_name] => Cat's Claw ) [3] => stdClass Object ( [tincture_id] => 44 [ps_product_id] => 1 [name] => Cat's Claw [is_product] => 1 [ps_name] => Cat's Claw uña de gato [Uncaria tomentosa] [tincture_name] => Cat's Claw ) [4] => stdClass Object ( [tincture_id] => 45 [ps_product_id] => 1 [name] => Cat's Claw [is_product] => 1 [ps_name] => Cat's Claw uña de gato [Uncaria tomentosa] [tincture_name] => Cat's Claw ) )

here is the controller:
Code:
function index()
    {
            $this->tincture_over_view();
            return(0);
    }
    
    function tincture_over_view()    
    {
        $data = array();
        
        if($query = $this->tincture_model->get_records())
        {
            $data['title'] = 'Tinctures';
            $data['admin_head'] = $this->load->view('templates/admin_head', $data, TRUE);
            $data['admin_top_bar'] = $this->load->view('templates/admin_top_bar', $data, TRUE);
            $data['admin_header'] = $this->load->view('templates/admin_header', $data, TRUE);
            $data['admin_footer'] = $this->load->view('templates/admin_footer', $data, TRUE);
            $data['title_action'] = 'Manage Tinctures';
            $data['top_note'] = 'List of your tinctures.';
            $data['records'] = $query;    
            
//            print_r($data);
            
            $this->load->view('tincture/tincture_over_view', $data);
        }
        else
        {

            $this->tincture_create();
        }
    }



RE: db_join adds new record to left table - Avenirer - 01-22-2015

I am sorry but I still don't see the duplicated record... There are 4 records, all with different ids.


RE: db_join adds new record to left table - CroNiX - 01-22-2015

I would guess that your first query in the IF() is failing, which makes it go to the ELSE, which is:
Code:
$this->tincture_create();

Your first query is a select, not an insert or update, so it's impossible for that to be adding anything whatsoever to the database.


RE: db_join adds new record to left table - alfred.laggner - 01-22-2015

(01-22-2015, 01:51 PM)Avenirer Wrote: I am sorry but I still don't see the duplicated record... There are 4 records, all with different ids.

I was not clear. Somehow the last record gets copied into a new record. Since it is autoincrement, a new id gets created. With every run a new record gets created.


RE: db_join adds new record to left table - alfred.laggner - 01-22-2015

(01-22-2015, 05:06 PM)CroNiX Wrote: I would guess that your first query in the IF() is failing, which makes it go to the ELSE, which is:

Code:
$this->tincture_create();

Your first query is a select, not an insert or update, so it's impossible for that to be adding anything whatsoever to the database.

I took the if else off and against my prediction it worked especially because
Code:
$this->tincture_create();
calls a function that opens the create record window - still far from creating a record. And it is still a mystery why it uses the contents of the last table record to create a new on.

So thank you all - it works - even have no explanation.