2

I have several models that all share one fields (say name) but differ in other fields. Is there any wan to find all of ModelX ModelY and ModelZ that have name 'foo'. Thanks for any suggestions.

1
  • 2
    If you have a field that is a replication of data from another table that is not a simple ID, you're probably doing it wrong.
    – R2-Bacca
    Mar 26, 2012 at 1:27

2 Answers 2

3

Something like this ?

$models = array('Post', 'Comment', 'Article');
$results = array();

foreach($models as $model) {
    $this->loadModel($model);
    $results[$model] = $this->{$model}->find('all', array(
        'conditions' => array(
            $model'.name' => $name
        )
    ));
}
2

If the they are related you should be able to specify the Model followed by '.' within the find.

    $this->find('all', array(
        'conditions' => array(
             'OR' => array(
                  'Article.field' => 'foo',
                  'Comment.field' => 'foo',
                  'Post.field' => 'foo',
              )
         ),
        'fields' => array('Article.field', 'Comment.field', 'Post.field')
    ));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.