Property "Manager.namesurname" is not defined.[/b][/color]
What should I do to display concatenated strings on this dropdown?
Is there possibility to concatenate 2 text columns directly in the view? Or can I only write function in model to get fullname and then execute thin funtion in view (thanks Antonio…).
Yeah it works in that way, 1) define function in model to concat name and surname, 2) call this function in view, but I was hoping that I can concat name and surname somehow just there in view - it seems to be so simple task.
Unfortunately there is no other way. CHtml::listData() is not that smart to understand strings like “name surname” Usually I generate such data for dropdowns manually.
Well I believe that one of the arguments passed to the CHtml::listData() is (beside models and ids) string $textField (database column/model property) which will be displayed on dropdown for corresponding id value. Concat is one of the simplest operations so maybe yii 2.0 will have this feature. On the other hand being unable to concat this in view, pushed me to be follow MVC and don’t put any logic (as concat) in view
You can’t pass ‘name’.’ surname’ to listData method, because it will try to find namesurname model’s property. You can try to extend this method so that it will take an array of strings and a separator, or a string like “@name@surname” and then parse it…
Well now I know difference between passing and rendering. So you mean I can try to extend CHtml::listData() function in yii framework source files, so it accepts more than property (for example concatenation of properties…)?
I mean extend CHtml class by adding your own method “listDataEx” and put the class into protected/components directory. listDataEx() should accept an array of attributes’ names as a 3rd parameter. Let’s look at the listData() code, it contants following strings:
This is exactly as writing a property named this way:
public function getNameSurname(){
return $this->name . ’ ’ . $this->surname;
}
That will work.
andy_s’s right and there is NO WAY JOSE that Yii could even imagine that the value passed is actually two column names concatenated… How does it know? Is there any parameters or settings that says to look at that?
Could be cool but if there is no way to tell the object that two concatenated columns are passed by, then the assumption to guess that the object will ‘intelligently know’ that there are in fact two columns is wrong.
Maybe it is a cool feature, you are right but that should be in the way of:
Could be nice to pass a separator too (’ ’ in our case). But anyway, this method won’t be universal, because one day some one will want to add some text to each dropdownlist row or to number them
For those still looking for a solution for this, it’s rather simple (I keep getting this post as one of the first replies in google so adding this solution hopefully helps some people)
in the model you’re using to fill the listData, add the following code
public function __get($key){
$action = 'get'.str_replace(' ','',ucwords(str_replace('_',' ',$key)));
if(method_exists($this, $action)) return $this->$action();
return parent::__get($key);
}
public function getFullName(){
return $this->name.' '.$this->first_name;
}
its a magic function that allows you to call a method as an attribute