Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Is this correct: array_keys($item->locations) in SELECT'ed vals
  • Hi,

    I am trying to populate a select with the values already part of the object. Right now, I am doing this:

    Form::select('list_location_id[]',  array_keys($item->list_locations), $list_locations);

    Is using the keys the correct way to get the ids to push to the default/selected vals in a select element?

    Thanks.

  • 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

    Model_Location::dropdown()

    but yes. that is the idea.
  • "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:

    protected static $keyfield = 'id';
    protected static $datafield = 'name';

    and then in the Item controller
    I am trying to set a global with:
    $this->template->content->set_global('locations', Model_List_Location::dropdown());

    and getting a 'Method dropdown does not exist' error.

  • I created a method in my List_Location :

            public static function getActiveListVals()
            {
                return DB::select(static::$keyfield, static::$datafield)
                    ->from(static::table())
                    ->where('active', 1)
                    ->execute()->as_array(static::$keyfield, static::$datafield);
            }

    And it works great. I would like ot be able to use this in each model, like the built in 'dropdown()'
    BUT

    1. Can I add to the code to exclude records that are not 'active' = 1 using the built in dropdown()?

  • If you don't define the method you're going to call, it does not exist.

    I use a base model that all my models extend, and which defines this method, making it avaiable to all my models. So my setup looks like this:

    class Model_Base extends \Orm\Model
    {
        public static function dropdown()
        {
            // ...
        }
    }

    class Model_List_Location extends Model_Base
    {
        // which now inherits the dropdown method from the base model
    }

  • 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.

    Thanks for the follow ups!
  • one more thing.. :)

    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 these models, I have:

            protected static $keyfield     = 'id';
            protected static $datafield    = 'name';

    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.


  • IGNORE this last one... I messed up.. TYPO.. .sorry.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion