Search code examples
cakephpcakedc

CakePHP: Search virtual fields from another model


I'm having an issue searching a virtual field from another model. I have 2 models: User/Order. Both are setup and working fine with the exception of the error below:

User Model (hasMany Order):

public $virtualFields = array(
    'name' => 'CONCAT(User.first, " ", User.last)'
);

public $filterArgs = array(
    'search' => array('type' => 'like', 'field'=>array('User.name', 'User.first', 'User.last', 'User.email')),
);

The above will search User.name (first last).

Order Model (belongsTo User):

public $filterArgs = array(
    'search' => array('type' => 'like', 'field'=>array('Order.id', 'User.email', 'User.first', 'User.last', 'User.name', 'Order.notes')),
);

I get a database error when trying to access User.name from the Order model. How can I search orders from "first last"?


Solution

  • This works for me.

    public $filterArgs = array('CONCAT(Contact.firstname , " ", Contact.lastname) LIKE' => "%$haystack%"));