irsyadulibad / ci4-datatables

Server Side Datatables Library for CodeIgniter 4 Framework
https://ci4-datatables.netlify.app
MIT License
72 stars 32 forks source link

Column 'field' in where clause is ambiguous #36

Open faustfizz opened 1 year ago

faustfizz commented 1 year ago
 DataTables::create(db_connect()->table('user u'))
            ->join("prestataire p", "p.id_prestataire = u.prestataire_id")
            ->select("u.*, p.nom as name")
            ->where('prestataire_id IS NOT NULL')
            .....................

I think the select function is ignored, i cann't do a custom select when fields are conflicting.

i have table user{ID, nom, prenom, prestataire_id} and prestataire{ID, nom}. I want to select with join but with no chance.

I need help !

cikaldev commented 1 year ago

Your code is almost correct, just another mistake to put a line of code, and get lost with your own Aliases of table.

Here i make the correct one, with inline comment and more verboses for readability.

DataTables::create(db_connect()
    // call table "users" with alias "u"
    ->table('user AS u'))
    // select whatever you want, and call another field as join
    // for better performance, better to call each collumn you want to fetch instead using wildcard
    ->select('u.*, p.nom AS name')
    // join another table "prestataire" alias "p", with condition "p.id_prestataire = u.prestataire_id"
    ->join('prestataire AS p', 'p.id_prestataire = u.prestataire_id')
    // put the where clause (don't forget to call it with prefix alias previously define)
    ->where('u.prestataire_id IS NOT NULL')
    // rest of your code here ...
    ->make()

Let us know if it's working or not,

@irsyadulibad (nimbrung ngab)

faustfizz commented 1 year ago

Hi, thank you for replying, unfortunately it didn't work.