It depends on what the status of your related objects is.
When the 'many' relation only contains fetched objects, the key in the array will always be the PK of the related object. If the object uses a compound key, it will be a string representation of that compound key.
If you add new objects, before you have saved them they will have the next available numeric index as array key, which has no relation at all to the PK of the object.
So if your array only contains fetched or saved items, this works fine.
I usually don't use ORM calls to produce dropdown lists, because it comes with a bit of overhead. We have a generic method in our ORM base model:
public static function dropdown() { // so we can cache the results static $dropdown = array();
// get the data if needed if (empty($dropdown)) { $dropdown = \DB::select(static::$keyfield, static::$datafield)->from(static::table())->execute()->as_array(static::$keyfield, static::$datafield); empty($dropdown) and $dropdown = array(); }
return $dropdown; }
where $keyfield is defined in the model as the column you want to use as array key, and $datafield the column you want to use as array value.
So it sounds like I am doing the correct thing to get the selected vals but then populating the entire list is done by:
In my Model_Location I would have:
$keyfield = 'id'; $datafield = 'name';
And then when I called and let the global for the totla list of locations, I would use: Set_global('list_locations', $location::dropdown()); (syntax is probably wrong, but just trying to get idea. )
Correct? Thanks for the replies. I was looking through and didn't see any info in the Arr class.
"If the method is in your Model_Location, you would call"
Not sure what you mean by this. In Model_List_Location (this is the actual correct name, List_Location, not just Location as I had posted earlier) I added:
Ah... I see. When you said "We have a generic method in our ORM base model:" ... I thought you meant 'We" as in "fuel code users... built into the base model." Got it.
I added a method in a 'Model_Custom' class that I am making all the models extend.
Here's where it gets weird... I had this problem earlier, but then thought I had something coded wrong.
I created a Model_Custom class that sits in the /model di and extends the '\Model\ORM' In there I have a method: public static function getListVals($active=NULL) { $sql = DB::select(static::$keyfield, static::$datafield) ->from(static::$table); if ($active) { $sql->where('active', 1); } return $sql->execute()->as_array(static::$keyfield, static::$datafield); } . My object classes extend this Model_Custom class in Model_List_Location, Model_List_Color, Model_Item
In items Controller I have a list of these calls: $this->template->content->set_global('list_locations', Model_List_Location::getListVals(true)); $this->template->content->set_global('list_colors', Model_List_Color::getListVals(true));
I'm getting an error that I cannot redeclare Model_List_Location OR Nothing even loads into the page. No error or content at all.
Seems like create/list are fine, but when Edit/Create post, it fails.
Any suggestions as to what I might have done. Everything seems fine.