I'm using CakePHP 2.3.8 and having some trouble getting searches to work in the shell. I have a table 'companies' that has a hasmany relationship with a 'subscriptions' table, and belongs to 'users' table. I'm trying to restrict my search based on the type of user, but I get an error when I include it in the search (see below)
So when I search $this->Company->find('all');
something like this is returned
(
[0] => Array
(
[Company] => Array
(
[id] => 1
[first_name] => Test
[last_name] => Person
[company_name] => Test Company
[address] => 123 Test
[city] => New York
[state] => Ny
[zip] => 12345
[phone] => 1234567
)
[User] => Array
(
[email] => test@example.com
[created] => 2014-01-09
[id] => 1
[role] => user
[username] => TestUser
)
[Subscription] => Array
(
[0] => Array
(
[id] => 1
[user_id] => 1
[start_date] => 2014-01-09
[expire_date] => 2014-07-09
[subscription_plan] => standard
[amount_paid] => 0.00
[status] => valid
)
)
)
I'm searching through subscriptions and I want to restrict based on the role. If it's a trial account or admin, skip over it. However, cake doesn't seem to let me search through these joined tables
When I try the following query
$companies = $this->Company->find('all',array(
'conditions' => array(
'User.role !=' => array('admin','trial'), //don't select any admins or trial members
)
));
I get the following error SQLSTATE[42S22]: Column not found: 1054 Unknown column 'User.role' in 'where clause'
Can I not conduct "joined" searches like this in the shell? When I search for all companies and output the result, it shows the User table selected properly, so why can't I restrict my search to certain types of users?
Edit - I've switched to using a containable and it partially works, but maybe I'm just misunderstanding the functionality of containables. Here is the new search
$this->Company->contain(array(
'User' => array(
'conditions' => array(
'User.role !=' => array('owner','site_owner','trial','admin')
)
)
));
$this->companies = $this->Company->find('all');
It selects every company, but only selects a User (for that company) if it meets the criterion. Here's an example result
[0] => Array
(
[Company] => Array
(
[id] => 1
[first_name] => Test
[last_name] => Person
[company_name] => Test Company
[address] => 123 Test
[city] => New York
[state] => Ny
[zip] => 12345
[phone] => 1234567
)
[User] => Array
(
[email] => test@example.com
[created] => 2014-01-09
[id] => 1
[role] => user
[username] => TestUser
)
[1] => Array
(
[Company] => Array
(
[id] => 2
[first_name] => Test2
[last_name] => Person2
[company_name] => Test Company2
[address] => 123 Test
[city] => New York
[state] => Ny
[zip] => 12345
[phone] => 1234567
)
[User] => Array //this user's role is admin so it was not selected
(
)
I only want it to select a company if the user criterion is met. Is this even possible?