For your find to work the way you expect it you need to use a join in
the query. hasMany doesn't do this by default.
You can do the query from the other side, since belongsTo is a joining
association.
$results = $this->Company->Location->find('all',...);
Or you you can specify a join. Check out this for more on that:
http://bakery.cakephp.org/articles/view/quick-tip-doing-ad-hoc-joins-in-model-find
On Jul 8, 9:11 am, Quang Yên <[email protected]> wrote:
> Hi all,
>
> I have a problem with the relationship hasMany when find with a
> type of condition :
>
> These are 2 models : Company and Location, a Company hasMany
> Location, a Location has a name of location, a phone number,
> locationID and companyID while a Company has companyID, companyname,
> companylegalname.
>
> - Company hasMany Location
> - Company : companyID, companyname, companylegalname.
> - Location : locationID, locationname, locationphone, companyID.
>
> I want to find companies and every company's locations. On the
> search form, I input a name and I want to find company has the
> companyname LIKE that name OR any of it's location has that name. My
> solution is :
>
> $conditions = array('OR'=>array(
> array('Company.companyname
> LIKE' => '%'.$name.'%'),
> array('Company.companylegalname
> LIKE' => '%'.$name.'%'),
> array('Location.locationname
> LIKE =>'%'.$name.'%')
> ));
> $this->Company->bindModel(array('hasMany'=>array('Location'=>array
> ('foreignKey'=>'companyID', 'conditions'=>$conditions))));
> $companies = $this->Company->find('all');
> This will cause an error that it cannot recognize the fiield
> Company.companyname or Company.companylegalname on the condition
> clause.
>
> So I tried to find with many ways but they seem to be helpless.
> When I put the $conditions at the condition clause on the find
> instruction, it cannot recognize the Location.locationname because
> this the the relationship hasMany.
> I need an advisement on this....
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"CakePHP" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---