How to join two database table in sql query for cakephp 3.6.7

398 views Asked by At

can anyone tell me to join two tables from database

my php query is :

SELECT * FROM `users`  INNER JOIN registration ON users.id=registration.id WHERE users.id='7'

can anyone convert to Cakaphp 3.6.7

thanks & regards, Darshan

1

There are 1 answers

0
Sehdev On

First of all you should use plural name for your model Registration. It should be Registrations.

You can use association for the same in Cakephp. First define your association in /src/Model/Table/UsersTable.php file.

class UsersTable extends Table
{
    public function initialize(array $config)
    {
        $this->hasOne('Registrations')->setJoinType('INNER');;
    }
}

Then query like this

$this->Users->find('all')->contain('Registrations')->where(['Users.id => 7]);

Reference: https://book.cakephp.org/3.0/en/orm/associations.html