Confused New User: How do you use two tables in a model?


#1

I have the following Model:

class AdminUser extends AppModel {
public $name = ‘AdminUser’;
public $useDbConfig = ‘clientbackend_dev’;
public $useTable = ‘user’;

I need to join another table to do a query. How do I do that?

TIA


#2

read this first: https://book.cakephp.org/3.0/en/orm/associations.html


#3

Thanks I should have mentioned I am using 2.9 but your point is well taken.


#4

OK, I read this https://book.cakephp.org/2.0/en/models/associations-linking-models-together.html#

I now have this model:

class AdminUser extends AppModel {
public $name = ‘AdminUser’;
public $useDbConfig = ‘clientbackend_dev’;
public $useTable = ‘user’;
public $hasOne = ‘Employee’;
}

And this one:

class Employee extends AppModel {
public $belongsTo = array(
‘AdminUser’ => array(
‘className’ => ‘AdminUser’,
‘foreignKey’ => ‘user_id’
)
);
}

I am still get an error when trying to bring up a page that uses this.


#5

First one should be public $hasOne = [‘Employee’];

How are you trying to use it?


#6

dakota, that did no work. I am still trying to solve the problem and have gotten help and done more troubleshooting. The files are posted on gist,

As always. TIA.