I am working on Cakephp 3.9 and I have 3 tables in my application.
CREATE TABLE `leads` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`first_name` varchar(100) NOT NULL,
`last_name` varchar(100) NOT NULL,
`created_dt` datetime NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=latin1
CREATE TABLE `clients` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`lead_id` int(10) NOT NULL,
`first_name` varchar(100) NOT NULL,
`last_name` varchar(100) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=latin1
CREATE TABLE `contacts` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`lead_id` int(10) NOT NULL,
`first_name` varchar(100) NOT NULL,
`last_name` varchar(100) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=latin1
There is a one-to-one relation between the Leads, Clients, and Contacts tables.
I also have a Users table:
CREATE TABLE `users` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`username` varchar(50) DEFAULT NULL,
`password` varchar(255) DEFAULT NULL,
`created` datetime DEFAULT NULL,
`modified` datetime DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=latin1
⌠and a leads_users table
CREATE TABLE `leads_users` (
`lead_id` int(10) NOT NULL,
`user_id` int(10) NOT NULL,
`user_type` enum('Opener','Closer') NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1
In my application, the users can be associated with the leads as Openers and Closers. There can be multiple openers and multiple closers for a lead. I have defined my models as below:
class LeadsTable extends Table
{
public function initialize(array $config)
{
$this->hasOne('Contacts');
$this->hasOne('Clients');
$this->belongsToMany('Users', [
'className' => 'Users'
])
->setConditions(['LeadsUsers.user_type' => 'Opener'])
->setProperty('openers');
$this->belongsToMany('Users', [
'className' => 'Users'
])
->setConditions(['LeadsUsers.user_type' => 'Closer'])
->setProperty('closers');
}
}
class ClientsTable extends Table
{
public function initialize(array $config)
{
$this->belongsTo('Leads');
}
}
class ContactsTable extends Table
{
public function initialize(array $config)
{
$this->belongsTo('Leads');
}
}
class UsersTable extends Table
{
public function initialize(array $config)
{
$this->belongsToMany('Leads');
}
}
Now in the Leads controller, I am trying to find all the leads, their contacts, clients, openers, and closers by using the below line of code.
$result = $this->Leads->find('all')->contain(['Contacts', 'Clients', 'Users']);
With this, I am getting the closers but not the openers. In the LeadsTable, if I comment out the closers association then I am getting the openers. So looks like the second association is overwriting the first one.
If I change the LeadsTable association to below:
class LeadsTable extends Table
{
public function initialize(array $config)
{
$this->hasOne('Contacts');
$this->hasOne('Clients');
$this->belongsToMany('Openers', [
'className' => 'Users'
])
->setConditions(['LeadsUsers.user_type' => 'Opener'])
->setProperty('openers');
$this->belongsToMany('Closers', [
'className' => 'Users'
])
->setConditions(['LeadsUsers.user_type' => 'Closer'])
->setProperty('closers');
}
}
I am getting the below error:
The Users association is not defined on Leads.
Please let me know what am I doing wrong. Thank you in advance.