consult with inner join in codeigniter

0

Good afternoon, I would like to request your help in something that I have broken my mind to solve and I can not fix it to work out

I have 4 tables

  • products -> id | slug | title | info1 | info2 | info3
  • prodnutri - > idNutricion | idProduct | Portion | info2 | info3
  • prodpreparo - > idPreparo | idProduct | info1 | info2 | info3
  • prodingredients - > idIngredients | idProduct | info1 | info2 | info3
  • The tables will have enough information and did not want to leave everything in one .. so I think it's better to divide the information to 4.

    I have tried several ways and I can not do it.

    I can list the products and call everything right this way domain.com/products/ver/productname

    NA MODEL / ProductModel - my basic query where I can bring all the data from the table products

     public function find($slug){      //
        $this->db->where('slug', $slug);
        return $this->db->get('productos')->row();
    }
    

    However,

    public function find($slug){      //
        $this->db->where('slug', $slug);
        $this->db->select("productos.slug");
        $this->db->from('productos');
        $this->db->join('prodnutri', 'prodnutri.idNutricion = productos.slug');
        $query = $this->db->get();
        return $this->db->get('productos')->row();
    }    
    

    NO CONTROLLER / products

    public function ver($slug)
    {   
        $this->load->view('incluir/cabecalho');
        $data['producto'] = $this->ProductModel->find($slug);
        $this->load->view('productos/producto', $data);
        $this->load->view('incluir/rodape');
    }
    

    The problem is that I'm not able to query the model (I think the controller is right! because all the data of the product table it brings right, but when I want to bring the data of the other tables it is giving error ..

    PHP Error was encountered Severity: Notice Message: Undefined property: stdClass :: $ portion

    in view to using vector so

    <?Php echo $producto->titulo;?> -- titulo da tb Productos
    <?Php echo $producto->porcion;?> -- titulo da tb prodnutri
    

    Someone can help, I will be very grateful in a light to solve the table with inner join ..

        
    asked by anonymous 10.04.2018 / 03:58

    2 answers

    0

    Try this solution in your MODEL query method:

    try {
        $query = $this->db->query("SELECT * FROM productos p ".
            "INNER JOIN prodnutri pn INNER JOIN prodpreparo pp ".
            "INNER JOIN prodingredientes pin ".
            "ON (p.id = pn.idProducto AND p.id = pp.idProducto AND p.id = pin.idProducto)");
    
        if ($query === false) {
            throw new Exception($this->db->error()['message'], $this->db->error()['code']);
        }
        return $query->result();
    
    } catch (Exception $e) {
        return null;
    }
    

    The return is in the form of an array, so you can scroll as desired.

        
    10.04.2018 / 20:58
    0

    First, you are calling the get method twice, for me it is wrong:

    $query = $this->db->get();
    return $this->db->get('productos')->row();
    

    You're not using this first line of $query for nothing.

    Second, try to give var_dump($data['producto']); after $data['producto'] on your controller, you see exactly what it is returning. If the search has really been successful.

    Remembering that $ this-> db-> get ('products') -> row () ; returns only one result and $ this-> db-> get ('products') -> result () ; returns an array.

    In any case, var_dump () will help you.

        
    17.04.2018 / 20:53