Find a model in an array of models (typically the result of a findAll)

Lets say I have a super simple table of items:

Table: Items

  • item_id

  • item_name

Then I want to find a specific item (ex: item_id=58234) in the array of models that is the result of this:




$items=Items::model()->findAll($criteria);



Is there a convenient way (like a "yii way") to this? …I mean, except for the obvious:




foreach ($items as $i => $item) {

  if ($item->item_id == 58234) {

    break;

  }

}

// $items[$i] holds the model I want



I might also ask another question like: Is there a way to make findAll use an attribute for indexing in the array it returns? (then my task would be super easy)

…hm, come to think of it, I don’t think that last question is gonna have yes for an answer, but wouldn’t that be a cool feature? Qiang? :)

No comments? Surely someone have been in this situation? :)

There’s no better way yet. We’ve discussed a possible solution here:

http://www.yiiframework.com/forum/index.php?/topic/5000-primary-key-as-index-when-using-findall/page__hl__index__fromsearch__1

But i’m not sure, if this would break anything. Otherwhise we could add a ticket for it.

Thanks mate, I will add my comment to that thread, hopefully it will be dealt with.