Monday, April 20, 2015

find in a controller using “like” inside an array of possible values

The main idea of this post is that I need to combine search inside an array via "like" without doing a fetch.

In the following code I extract the zones (part of postal code) depending on session ID:

 $sess_id = $this->Session->read('Auth.User');   $this->loadModel('Zone', 2);   $MyZones = $this->Zone->find('list', array('recursive' => -1, 'conditions' => array('Zone.user_id' => $sess_id['id']), 'fields' => array('Zone.zone')));   $this->set('MyZones', $MyZones);     print_r($MyZones);    //output : $MyZone = Array ( [1] => AB [2] => AL [3] => B1 )

Now that I have my array I want to look for persons who live in this zone (example his postalcode can be AB526PQ so he lives in the administrator zone)

 $this->loadModel('Person', 2);   $num_persons = $this->Person->find('count', array('recursive' => -1, 'conditions' => array('Person.pc LIKE' => '%' . $MyZones)));       $this->set('num_persons', $num_persons);

I got the following error :

Notice (8): Array to string conversion [APP\Controller\ZonesController.php]

This is the generated sql query:

SELECT COUNT(*) AS `count` FROM `tn`.`personnes` AS `Personne` WHERE `Personne`.`pc` LIKE '%Array'

--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP

---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cake-php+unsubscribe@googlegroups.com.
To post to this group, send email to cake-php@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/d/optout.

No comments: