i once wrote a app_helper method for this:
/** * checks a record, if it is unique - depending on other fields in this table (transfered as array) * example in model: 'rule' => array ('uniqueRecord',array('belongs_to_table_id','some_id','user_id')), * if all keys (of the array transferred) match a record, return false, otherwise true * @param ARRAY other fields * TODO: add possibity of deep nested validation (User -> Comment -> CommentCategory: UNIQUE comment_id, Comment.user_id) * 2010-01-30 ms */ function validateUnique($arguments, $fields = array(), $options = null) { $id = (!empty($this->data[$this->alias]['id'])?$this->data[$this- >alias]['id'] : 0); foreach ($arguments as $key => $value) { $fieldName = $key; $fieldValue = $value; // equals: $this->data[$this->alias] [$fieldName] } if (empty($fieldName) || empty($fieldValue)) { // return true, if nothing is transfered (check on that first) return true; } $conditions = array($this->alias.'.'.$fieldName => $fieldValue, // Model.field => $this->data['Model']['field'] $this->alias.'.id !=' => $id, ); foreach ((array )$fields as $dependingField) { if (!empty($this->data[$this->alias][$dependingField])) { // add ONLY if some content is transfered (check on that first!) $conditions[$this->alias.'.'.$dependingField] = $this->data[$this- >alias][$dependingField]; } elseif (!empty($this->data['Validation'][$dependingField])) { // add ONLY if some content is transfered (check on that first! $conditions[$this->alias.'.'.$dependingField] = $this- >data['Validation'][$dependingField]; } elseif (!empty($id)) { # manual query! (only possible on edit) $res = $this->find('first', array('fields' => array($this- >alias.'.'.$dependingField), 'conditions' => array($this->alias.'.id' => $this->data[$this->alias]['id']))); if (!empty($res)) { $conditions[$this->alias.'.'.$dependingField] = $res[$this->alias] [$dependingField]; } } } $this->recursive = -1; if (count($conditions) > 2) { $this->recursive = 0; } $res = $this->find('first', array('fields' => array($this- >alias.'.id'), 'conditions' => $conditions)); if (!empty($res)) { return false; } return true; } On 22 Apr., 16:00, John Andersen <j.andersen...@gmail.com> wrote: > What exactly is this validation supposed to validate? > 1) That among the submitted data are no duplicate records (field1 + > field2)? > 2) That among the submitted data and the existing records in the table > are no duplicate records? > > From what I see, the validation handles only no. 1). Is that correct? > Enjoy, > John > > On Apr 21, 9:08 pm, Andy Dirnberger <andy.dirnber...@gmail.com> wrote: > > > > > Here's a stripped down example of how I do it: > > > app_model.php > > class AppModel extends Model { > > > ... > > > function isUniqueMulti($data, $fields) { > > if (!is_array($fields)) { > > $fields = array($fields); > > } > > > foreach ($fields as $key) { > > $tmp[$key] = $this->data[$this->name][$key]; > > } > > > return $this->isUnique($tmp, FALSE); > > } > > > } > > > some_model.php > > class SomeModel extends AppModel { > > var $validate = array( > > 'field1' => array( > > 'rule' => array('isUniqueMulti', array('field1', 'field2')), > > 'required' => TRUE, > > 'message' => 'Error Message', > > ), > > 'field2' => array( > > 'rule' => 'notEmpty', > > 'required' => TRUE, > > 'message' => 'Error Message', > > ), > > ); > > > } > > > On Apr 21, 8:40 am, Swanny <stuarts...@gmail.com> wrote: > > > > Is there a way in a model to verify that a record does not exist > > > already that matches a certain criteria. In the database table are > > > three columns: id, list_id and product_id. I would like to be able to > > > tell if there already exists a record with the same list_id AND > > > product_id as the one being added using the form which would then > > > produce an error as this would be a duplicate. > > > > Example: I try to add a product with product_id 23 and list_id 12 but > > > the table already contains a record with this information. > > > > Thanks > > > > Check out the new CakePHP Questions sitehttp://cakeqs.organdhelpothers > > > with their CakePHP related questions. > > > > You received this message because you are subscribed to the Google Groups > > > "CakePHP" group. > > > To post to this group, send email to cake-php@googlegroups.com > > > To unsubscribe from this group, send email to > > > cake-php+unsubscr...@googlegroups.com For more options, visit this group > > > athttp://groups.google.com/group/cake-php?hl=en > > > Check out the new CakePHP Questions sitehttp://cakeqs.organdhelp others > > with their CakePHP related questions. > > > You received this message because you are subscribed to the Google Groups > > "CakePHP" group. > > To post to this group, send email to cake-php@googlegroups.com > > To unsubscribe from this group, send email to > > cake-php+unsubscr...@googlegroups.com For more options, visit this group > > athttp://groups.google.com/group/cake-php?hl=en > > Check out the new CakePHP Questions sitehttp://cakeqs.organd help others with > their CakePHP related questions. > > You received this message because you are subscribed to the Google Groups > "CakePHP" group. > To post to this group, send email to cake-php@googlegroups.com > To unsubscribe from this group, send email to > cake-php+unsubscr...@googlegroups.com For more options, visit this group > athttp://groups.google.com/group/cake-php?hl=en Check out the new CakePHP Questions site http://cakeqs.org and help others with their CakePHP related questions. You received this message because you are subscribed to the Google Groups "CakePHP" group. To post to this group, send email to cake-php@googlegroups.com To unsubscribe from this group, send email to cake-php+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php?hl=en