Feature Request #2552

Add a display_val method/field

Added by Rowan Parker over 4 years ago. Updated over 4 years ago.

Status:ReviewStart date:02/08/2010
Priority:NormalDue date:
Assignee:-% Done:

0%

Category:-
Target version:2.0.1
Resolution: Points:

Description

This request is for a standalone method which determines what is return as primary value for an ORM model.

You can use this for customising the contents of a dropdown value. It also provides a single method for generating a user-friendly display.

Inside the __get() method add code to check for display_val:

elseif ($column === 'display_val')
{
     return $this->display_val();
}

Create a display_val() method to which returns the current primary value:

/**
 * Returns the value of the primary_val field.
 * Can be used to return a different value for
 * specific models.
 *
 * @return  string
 */
public function display_val()
{
    $primary_val = $this->primary_val;
    return $this->$primary_val;
}

Modify select_list() to read as:

if ($val === NULL)
{
    $val = $this->display_val();
}

As an example you could then overload the display_val() method in your model to return whatever value you like.

History

#1 Updated by Isaiah DeRose-Wilson over 4 years ago

  • Status changed from New to Review
  • Target version set to 2.0.1

Hmmm, I'm not totally sure I understand what you need, but doesn't $obj->primary_key_value already do what you want?

We could create a ORM::pk() function like 3.0 has, would that be better?

#2 Updated by Rowan Parker over 4 years ago

If I had a 2 column table (id and name), then $obj->primary_key_value would return the value of $obj->id (according to how I read the following code):

elseif ($column === 'primary_key_value')
{
    return $this->object[$this->primary_key];
}

Maybe there should be a $obj->primary_val_value - which would return $obj->name - like this:

elseif ($column === 'primary_val_value')
{
    return $this->object[$this->primary_val];
}

However that's not what I'm after. At the moment I have a heap of objects which use a common list view. For most of them I can usually just display the primary_val set in the model. However for some I usually need to concatenate 2 strings to display - for example a three column table with id, code and name.
I usually want to output the code, a space then the name.

I guess what I'd really like is for the $val argument to select_list() to be an array of column names. Then it passes the result to another function to interpret those results. Maybe something like this:

public function select_list($key = NULL, $val = NULL)
{
    if ($key === NULL)
    {
        $key = $this->primary_key;
    }

    if (!is_array()){
    {
        if ($val === NULL)
        {
            $val = $this->primary_val;
        } 

        return $this->select($key, $val)->find_all()->select_list($key, $val);
    }

    array_unshift($val, $key);
    return $this->format_select_list($this->select($val)->find_all()));
}

public function format_select_list($vals)
{
    // code to format the array back
    // into a single key => val array
}

#3 Updated by Ben Rogers over 4 years ago

i think what's really necessary is a drop in function you can drop in your model for creating the primary_key_value instead of just specifying a field. this way you can do any sort of random formatting on it you would prefer(spacing, dashes, what have you).

that would provide the most flexibility.

#4 Updated by Rowan Parker over 4 years ago

Actually I think the most concise solution is to allow a callback as a third parameter to select_list().

/**
 * Creates a key/value array from all of the objects available. Uses find_all
 * to find the objects.
 *
 * @param   string  key column
 * @param   string  value column
 * @return  array
 */
public function select_list($key = NULL, $val = NULL, $callback = NULL)
{
    if ($key === NULL)
    {
        $key = $this->primary_key;
    }

    if ($val === NULL)
    {
        $val = $this->primary_val;
    }

    // Get a select list from the results
    $list = $this->select($key, $val)->find_all()->select_list($key, $val);

    if ($callback === NULL)
    {
        return $list;
    }
    else
    {
        return call_user_func($callback, $list);
    }
}

Also available in: Atom PDF