CodeIgniter Forums
Join in controller method - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30)
+--- Thread: Join in controller method (/showthread.php?tid=79585)



Join in controller method - pippuccio76 - 07-05-2021

Hi how can i do a join query on controller's method :

Code:
$this->select('tratte_acquistate.*');
        $this->select('tratte_acquistate_extra.*');
        $this->select('tratte_acquistate_passeggeri.*');
        $this->from('tratte_acquistate');
        $this->join('tratte_acquistate_extra', 'tratte_acquistate.id = tratte_acquistate_extra.id_tratte_acquistate');
        $this->join('tratte_acquistate_passeggeri', 'tratte_acquistate_passeggeri.id = tratte_acquistate_passeggeri.id_tratte_acquistate');

        $this->orderBy('Table_3.id');
        $result = $this->findAll();

        echo $this->db->getLastQuery();

        return $result;


Call to undefined method App\Controllers\Admin_tratte_acquistate:: select()


RE: Join in controller method - craig - 07-05-2021

You don't.

You need to use Query Builder or Models.


RE: Join in controller method - paliz - 07-05-2021

PHP Code:
public function index()
    {

        $newsMediaModel = new NewsMediaModel();
  
        $result 
$newsMediaModel->select()
            ->join('news_post''news_post.id = news_media.post_id''left')
          
            
->where(['id'=>'2'])

            ->orderBy('id','desc')
            ->paginate(10'default',11);

        return $this->respond([
            'data' => $result,
            'pager' => $newsMediaModel->pager->getDetails()
        ], ResponseInterface::HTTP_OKlang('Shared.api.receive'));


    



RE: Join in controller method - ikesela - 07-05-2021

declare variable first

protected $db;

then in controller __construct()

$this->db = db_connect();

now your are ready to use.

$this->db->select('tratte_acquistate.*');


RE: Join in controller method - pippuccio76 - 07-05-2021

(07-05-2021, 12:22 PM)ikesela Wrote: declare variable first

protected $db;

then in controller __construct()

$this->db = db_connect();

now your are ready to use.

$this->db->select('tratte_acquistate.*');

Don't work  error : Call to undefined method CodeIgniter\Database\MySQLi\Connection:: select()

(07-05-2021, 12:13 PM)paliz Wrote:
PHP Code:
public function index()
    {

        $newsMediaModel = new NewsMediaModel();
  
        $result 
$newsMediaModel->select()
            ->join('news_post''news_post.id = news_media.post_id''left')
          
            
->where(['id'=>'2'])

            ->orderBy('id','desc')
            ->paginate(10'default',11);

        return $this->respond([
            'data' => $result,
            'pager' => $newsMediaModel->pager->getDetails()
        ], ResponseInterface::HTTP_OKlang('Shared.api.receive'));


    

this is my function :


Code:
public function lista_completa(){

        $tratte_acquistate_model = new Tratte_acquistateModel();

        $result =$tratte_acquistate_model->select('tratte_acquistate.*')
        ->select('tratte_acquistate_extra.*')
        ->select('tratte_acquistate_passeggeri.*')
        ->join('tratte_acquistate_extra', 'tratte_acquistate.id = tratte_acquistate_extra.id_tratte_acquistate')
        ->join('tratte_acquistate_passeggeri', 'tratte_acquistate_passeggeri.id = tratte_acquistate_passeggeri.id_tratte_acquistate')
        ->findAll();

        echo $tratte_acquistate_model->getLastQuery();

        return $result;

                  
}

mysqli_result::fetch_object(): Argument #1 ($class) must be a valid class name, App\Models\EntitiesTratte_acquistate given


RE: Join in controller method - ikesela - 07-05-2021

(07-05-2021, 12:50 PM)pippuccio76 Wrote:
(07-05-2021, 12:22 PM)ikesela Wrote: declare variable first

protected $db;

then in controller __construct()

$this->db = db_connect();

now your are ready to use.

$this->db->select('tratte_acquistate.*');

Don't work  error : Call to undefined method CodeIgniter\Database\MySQLi\Connection:: select()

(07-05-2021, 12:13 PM)paliz Wrote:
PHP Code:
public function index()
    {

        $newsMediaModel = new NewsMediaModel();
  
        $result 
$newsMediaModel->select()
            ->join('news_post''news_post.id = news_media.post_id''left')
          
            
->where(['id'=>'2'])

            ->orderBy('id','desc')
            ->paginate(10'default',11);

        return $this->respond([
            'data' => $result,
            'pager' => $newsMediaModel->pager->getDetails()
        ], ResponseInterface::HTTP_OKlang('Shared.api.receive'));


    

this is my function :


Code:
public function lista_completa(){

        $tratte_acquistate_model = new Tratte_acquistateModel();

        $result =$tratte_acquistate_model->select('tratte_acquistate.*')
        ->select('tratte_acquistate_extra.*')
        ->select('tratte_acquistate_passeggeri.*')
        ->join('tratte_acquistate_extra', 'tratte_acquistate.id = tratte_acquistate_extra.id_tratte_acquistate')
        ->join('tratte_acquistate_passeggeri', 'tratte_acquistate_passeggeri.id = tratte_acquistate_passeggeri.id_tratte_acquistate')
        ->findAll();

        echo $tratte_acquistate_model->getLastQuery();

        return $result;

                  
}

mysqli_result::fetch_object(): Argument #1 ($class) must be a valid class name, App\Models\EntitiesTratte_acquistate given

my bad, model usage is different than builder.

showing half code, hard to solve this.


RE: Join in controller method - manager - 07-05-2021

All queries to retrieve data from database should be in models, controllers just call suitable model's methods and that's it.
If your model extends codeigniter's Model than there always available automated database connection which you can use ($this-db is a reference to default database connection).
select() is a Query Builder method. So use it with query builder, like:
$builder = $this-db->table('my_table_name');
$builder->select('my_variable1, my_variable2');
$query = $builder->get();


RE: Join in controller method - pippuccio76 - 07-06-2021

(07-05-2021, 10:02 PM)ikesela Wrote:
(07-05-2021, 12:50 PM)pippuccio76 Wrote:
(07-05-2021, 12:22 PM)ikesela Wrote: declare variable first

protected $db;

then in controller __construct()

$this->db = db_connect();

now your are ready to use.

$this->db->select('tratte_acquistate.*');

Don't work  error : Call to undefined method CodeIgniter\Database\MySQLi\Connection:: select()

(07-05-2021, 12:13 PM)paliz Wrote:
PHP Code:
public function index()
    {

        $newsMediaModel = new NewsMediaModel();
  
        $result 
$newsMediaModel->select()
            ->join('news_post''news_post.id = news_media.post_id''left')
          
            
->where(['id'=>'2'])

            ->orderBy('id','desc')
            ->paginate(10'default',11);

        return $this->respond([
            'data' => $result,
            'pager' => $newsMediaModel->pager->getDetails()
        ], ResponseInterface::HTTP_OKlang('Shared.api.receive'));


    

this is my function :


Code:
public function lista_completa(){

        $tratte_acquistate_model = new Tratte_acquistateModel();

        $result =$tratte_acquistate_model->select('tratte_acquistate.*')
        ->select('tratte_acquistate_extra.*')
        ->select('tratte_acquistate_passeggeri.*')
        ->join('tratte_acquistate_extra', 'tratte_acquistate.id = tratte_acquistate_extra.id_tratte_acquistate')
        ->join('tratte_acquistate_passeggeri', 'tratte_acquistate_passeggeri.id = tratte_acquistate_passeggeri.id_tratte_acquistate')
        ->findAll();

        echo $tratte_acquistate_model->getLastQuery();

        return $result;

                  
}

mysqli_result::fetch_object(): Argument #1 ($class) must be a valid class name, App\Models\EntitiesTratte_acquistate given

my bad, model usage is different than builder.

showing half code, hard to solve this.
no is entire function (lista_completa) not half ...