cakephp - Dynamically Build a SQL Where Clause with the IN Keyword -
i'm using cakephp 2.3.0. summary - in controller, have query returns 1 or more id values. then, want use id values in next query, sql in keyword. know conceptually want accomplish. think i'm doing correct way in cakephp, wrong. i'm not sure on coding piece dynamically build in clause.
here code snippet controller:
$this->set('userids', $this->activity->activitiesofinterest->find('all', array ( 'conditions' => array ('activitiesofinterest.activities_id' => $id), 'fields' => array ('activitiesofinterest.user_id'))));
the above code fine value or values expect. in code below, have hard-coded piece, array(3, 4), equivalent sql in keyword. values 3, 4 stored in userids array. but, i'm not sure how dynamically build values of 3 , 4, array in above code. of course, 3 , 4 may not values because depends on in database. think need loop through array , build list of id's. i'm still new cakephp , i'm guessing easy answer experienced folks.
and, yes want perform deleteall operation, based on use case.
$this->activity->activitiesofinterest->deleteall( array('activitiesofinterest.user_id' => array(3, 4)), false);
thank you, appreciated.
you don't need use $this->set()
, unless need user-ids inside view; hash::extract()
may helpfull here.
// find user-ids $result = $this->activity->activitiesofinterest->find('all', array ( 'conditions' => array ( 'activitiesofinterest.activities_id' => $id ), 'fields' => array ( 'activitiesofinterest.user_id' ) )); if ($result) { // 'extract' user-ids results $userids = hash::extract($result, '{n}.activitiesofinterest.user_id'); $this->activity->activitiesofinterest->deleteall( array( 'activitiesofinterest.user_id' => $userids ), false ); }
Comments
Post a Comment