CakePHP Concatenation

I had to concatenate two fields to search them as a single string. Another programmer created a table of phone numbers with the columns area_code (INT 3) and phone_number (INT 7) respectively. But people were searching for all ten digits as a single unit, so I needed to search both the columns as a single field.

This blog post on virtual fields and this doc about same made me think that  $virtual_fields was the answer, but it didn’t work out for me. The following solution worked well enough.

$conditions = array(
'CONCAT(NoCallList.area_code, NoCallList.phone_number) LIKE' => '%' . $this->data['NoCallList']['search_term'] . '%'
);
$this->set('items' , $this->{$this->modelClass}->find('all'
, array(
'order'=>array($this->modelClass . '.phone_number')
, 'limit'=>500
, 'conditions'=>$conditions
,
)
));

Would you like to receive updates by email?

Published by

Shannon Graham

Shannon has been writing brief essays and occasional how-to articles at Rocketships, Unaffiliated (.ca) whenever inspiration strikes, since 2012.

She is interested in your opinion.

One thought on “CakePHP Concatenation”

  1. I have noticed you don’t monetize your website, don’t
    waste your traffic, you can earn additional bucks every
    month because you’ve got hi quality content. If you want to know how to make extra $$$, search for: Mrdalekjd methods for
    $$$

Leave a Reply

Your email address will not be published. Required fields are marked *