I have forgotten more than I'll ever know
Limit results seen by users
Here are a couple of nifty functions I came across, to limit the results that are returned in a Cake app.
When using the find() function, by default it will return all of the results. These two methods if placed in the appModel will only return results that are associated with a given user.
<?php function findMy($type, $options=array()) { if($this->hasField('user_id') && !empty($_SESSION['Auth']['User']['id'])){ $options['conditions'][$this->alias.'.user_id'] = $_SESSION['Auth']['User']['id']; return parent::find($type, $options); } else{ return parent::find($type, $options); } } function deleteMy($id = null, $cascade = true){ if (!empty($id)) { $this->id = $id; } $id = $this->id; if($this->hasField('user_id') && !empty($_SESSION['Auth']['User']['id'])){ $opt = array( 'conditions' => array( $this->alias.'.user_id' => $_SESSION['Auth']['User']['id'], $this->alias.'.id' => $id, ), ); if($this->find('count', $opt) > 0){ return parent::delete($id, $cascade); } } return false } ?>
Print article | This entry was posted by Twitter on February 8, 2011 at 12:27 pm, and is filed under CakePHP. Follow any responses to this post through RSS 2.0. You can leave a response or trackback from your own site. |