I would recommend taking advantage of the automatic binding by using
Model::$hasMany instead of binding on-the-fly.

Basically, the conditions you have generated are not Location
specific. Why bind them to the model? Instead, try something like the
following:

$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'
                )
            )
        ));
    $companies = $this->Company->find('all', compact('conditions'));

On Jul 8, 3:11 pm, 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
-~----------~----~----~----~------~----~------~--~---

Reply via email to