Concatenate atttributes in CHtml::listData

Hi,I want to concatenate attributes in a CHtml::listData


$recs = Socio::model()->findAll(array('order'=>'id')); //el array genera un CDBCriteria()

$data = CHtml::listData($recs,'id','apellidoynombre');



I’m using this to populate a DropDown List

Work fine, but I want to see the internar ID along with the name.

I try:

$data = CHtml::listData($recs,‘id’,‘apellidoynombre.id’);

$data = CHtml::listData($recs,‘id’,‘apellidoynombre+id’);

But nothing works

Any idea ? any tip ??

I want to show a DropDown list with somethin like

  • Customer John doe (1)

  • Customer Phillip Morris (2)

  • etc…

Where (1) and (2) is an internal id.

Best Regards.

Try




$data = CHtml::listData($recs,'id','apellidoynombre'.id);

// or

$data = CHtml::listData($recs,'id','apellidoynombre'.' '.id);



No luck, kokomo …

eventhough, I can’t figure out how the string is concatenated in Value function:


public static function value($model,$attribute,$defaultValue=null)

{

    foreach(explode('.',$attribute) as $name)

    {

        if(is_object($model))

            $model=$model->$name;

        else if(is_array($model) && isset($model[$name]))

            $model=$model[$name];

        else

            return $defaultValue;

    }

    return $model;

}

Maybe I should use for this case, some SQL statement directly.

Regards

Hmmm, I was pretty sure that one of my above solutions work

Another aproach would be to alter the $data variable which is only an array in a specific form I think.

Do a


CVarDumper::dump($data);

to see the array structure and then manipulate that array values to your needs. Write a function for this task so you can reuse it.

The SQL approach you mentioned should also work.

Hi,

What I am usually do and work is like this




$data = CHtml::listData($recs,'id','idApellidoynombre');



and then on your model add this method




public function getIdApellidoynombre() {

    return 'Customer ' . $this->apellidoynombre . ' (' . $this->id . ')'; // or whatever format you want to display

}



Hi Daniel,

I’m also looking for string/column concatenation but in all the forums it’s suggested to create the function inside the Model which can’t satisfy me.

For example: You have 4 columns id, first_name, last_name, father_name & you need to concatenate first_name with the last_name and in another point you need last_name with father_name. So you’ll be keep creating different functions to concatenate the columns, I hope you follow me?

Thanks,

Adil