lima.kayo
(Lima Kayo)
October 16, 2013, 1:50pm
1
hi all,
I have to concate strings in CHtml::listData:
Example:
view:
$form->dropDownList($os, 'id', CHtml::listData(Service::model()->findAll(), 'id', 'concatenated'));
model:
public function getConcatenated()
{
return $this->id.'-'.$this->name;
}
this is working, BUT , I wish to make a custom function that return an array:
$form->dropDownList($os, 'id', CHtml::listData($this->customFunction(), 'id', 'concatenated'));
and that it’s not working, just work with the findAll() model function, what I’m doing wrong ??
thanks in advance
[color=#000088]public[/color][color=#000000] [/color][color=#000088]function[/color][color=#000000] getConcatened[/color]color=#666600 [/color][color=#000000]
[/color][color=#666600]{[/color][color=#000000]
[/color][color=#000088]return[/color][color=#000000] $this[/color][color=#666600]->[/color][color=#000000]name[/color][color=#666600].[/color][color=#008800]' '[/color][color=#666600].[/color][color=#000000]$this[/color][color=#666600]->[/color][color=#000000]surname[/color][color=#666600];[/color][color=#000000]
[/color][color="#666600"]}[/color][color=#666600][/color] [color=#000088][font=arial, verdana, tahoma, sans-serif]public[/font][/color][font=arial, verdana, tahoma, sans-serif] [/font][color=#000088][font=arial, verdana, tahoma, sans-serif]function[/font][/color][font=arial, verdana, tahoma, sans-serif] getManagers[/font][color=#666600][font=arial, verdana, tahoma, sans-serif]()[/font][/color][color=#000000] [/color][color=#666600]{[/color][color=#000000]
$criteria[/color][color=#666600]=[/color][color=#000088]new[/color][color=#000000] [/color][color=#660066]CDbCriteria[/color][color=#666600];[/color][color=#000000]
[/color][color=#666600]...[/color][color=#000000]
$models [/color][color=#666600]=[/color][color=#000000] [/color][color=#660066]Manager[/color][color=#666600]::[/color][color=#000000]model[/color][color=#666600]()->[/color][color=#000000]findAll[/color][color=#666600]([/color][color=#000000]$criteria[/color][color=#666600]);[/color][color=#000000]
$list [/color][color=#666600]=[/color][color=#000000] [/color][color=#660066]CHtml[/color][color=#666600]::[/color][color=#000000]listData[/color][color=#666600]([/color][color=#000000]$models[/color][color=#666600],[/color][color=#000000] [/color][color=#008800]'id'[/color][color=#666600],[/color][color=#000000] [/color][color=#008800]'concatened'[/color][color=#666600]);[/color][color=#000000]
[/color][color=#000088]return[/color][color=#000000] $list[/color][color=#666600];[/color][color=#000000]
[/color][color=#666600]}[/color][color=#666600]
[/color][color=#000000]echo [/color][color=#660066]CHtml[/color][color=#666600]::[/color][color=#000000]activeDropDownList[/color][color=#666600]([/color][color=#000000]$model[/color][color=#666600],[/color][color=#008800]‘id_manager’[/color][color=#666600],$model->[/color][color="#008800 "]managers[/color][color=#666600][font=arial, verdana, tahoma, sans-serif]));[/font][/color]
lima.kayo
(Lima Kayo)
October 16, 2013, 2:12pm
3
thanks chandran,
but how can I insert a param to the custom function in the view?
because my function in the model its like this:
public function getSomething($id) {
//do findAll with $id..
//save in an $array..
return CHtml::listData($array, 'id', 'concatened');
}
andy_s
(Arekandrei)
October 16, 2013, 2:36pm
4
Probably you don’t need CHtml::listData() at all. Just generate an array and pass it to dropDownList():
$form->dropDownList($os, 'id', array(1=>'foo', 2=>'bar', ...));
[font=Consolas,]
[/font][font=Consolas,]Hi,[/font][font=Consolas,]
[/font][font=Consolas,]I am not sure whether it is good way…but this is one way…[/font][font=Consolas,]
[/font][font=Consolas,]Use hidden field[/font][font=Consolas,]
[/font][font=Consolas,]echo CHtml::activeHiddenField($model,‘intime’,array(‘id’=>$model->id)); [/font][font=Consolas,]
[/font][font=Consolas,]Then in the model try to get _GET or _POST[/font][font=Consolas,]
[/font]
lima.kayo
(Lima Kayo)
October 16, 2013, 4:23pm
6
I think it’s because I’m returning an array() instead of a findAll() method :
model:
public function getAbertas()
{
$funcionario = Funcionario::model()->find('id_user=:id_user', array(':id_user'=>Yii::app()->user->id));
$areas = $funcionario->areas;
$array = array();
foreach($areas as $area):
$ordens = OrdemServico::model()->findAll('id_andamento=2 AND id_equipamento=:id_equipamento',array(':id_equipamento'=>$area->id));
$count_ordens = count($ordens);
if ($count_ordens > 0)
{
foreach($ordens as $ordem)
array_push($array, $ordem->attributes);
}
endforeach;
sort($array);
$list = CHtml::listData($array, 'id', 'concatenate');
return $list;
If I do that way, the dropDownList don’t show the concatenate otherwise if I return a OrdemServico::model()->findAll(), it works normally.
Tsunami
(Erik)
October 16, 2013, 8:17pm
7
Did you read the CHtml::listData documentation ? It only works with an array of model objects, you’re passing model attributes.
If your relations were set up correctly you would be able to do:
public function getAbertas()
{
$servico = OrdemServico::model()->with('funcionario')->findAll('t.id_andamento = 2 AND funcionario.id_user = :id_user', array(
':id_user' => Yii::app()->user->id,
));
$list = CHtml::listData($servico, 'id', 'concatenate');
return $list;
}
lima.kayo
(Lima Kayo)
October 17, 2013, 2:40pm
8
thanks Tsunami, but I want to know how can I do a foreach like this:
$employee = Employee::model()->find('id_user=:id_user', array(':id_user'=>Yii::app()->user->id));
foreach ($employee->areas as $area):
$service = Service::model()->findAll('id_equipment=:id_area', array(':id_area'=>$area->id));
endforeach;
return $list = CHtml::listData($service, 'id', 'concatenate');
I know that way is wrong…
I wish to get all areas of the Employee and compare to the attribute id_equipment of the table Service, than return an array of objects (Services) into a listData, but how can I do a foreach and return that array of objects?
my relations:
Service.php:
public function relations()
{
return array(
...
'employee' => array(self::BELONGS_TO, 'Employee', 'id_employee'),
...
)
}
Employee.php:
public function relations()
{
return array(
'areas' => array(self::MANY_MANY, 'Area', 'tbl_employee_area(id_area, id_employee)', 'index'=>'id'),
'employeeArea'=> array(self::HAS_MANY, 'EmployeeArea', 'id_employee', 'index'=>'id_area'),
);
}
Area.php:
public function relations()
{
return array(
'employees' => array(self::MANY_MANY, 'Employee', 'tbl_employee_area(id_area, id_employee)'),
);
}
Thanks!
Tsunami
(Erik)
October 20, 2013, 3:05pm
9
You shouldn’t do a foreach. If the employee has 20 areas, you’re executing 41 queries, which can be just 1.
Service.php:
public function relations()
{
return array(
...
'employee' => array(self::BELONGS_TO, 'Employee', 'id_employee'),
'area' => array(self::BELONGS_TO, 'Area', 'id_equipment'),
...
)
}
public function getAbertas()
{
$services = Service::model()
->with('area', 'area.employees')
->together()
->findAll('area_employees.id_user = :id_user', array(
':id_user' => Yii::app()->user->id,
));
return CHtml::listData($services, 'id', 'concatenate');
}
But if Service already has a relation with Employee, I’m not sure what Area is for.