similarly I have 2 tables in database,accounts and banks,and One to Many Relation between them.
'banks' => array(self::BELONGS_TO, 'Banks', 'banks_id'),
in Accounts model.
I need to use Chtml::dropDownList
in account creation form,so I edit _form.php
to it:
<div class="row">
<?php echo $form->labelEx($model,'banks_id'); ?>
<?php echo CHtml::dropDownList($model, 'banks_id', CHtml::listData(Banks::getBanksList(), 'id', 'title','country')); ?>
<?php echo $form->error($model,'banks_id'); ?>
</div>
and also in Banks model develop `getBanksList by:
public static function getBanksList()
{
$userLanguage = Yii::app()->user->getState('lang');
$result = array();
$banks = self::model()->findAll();
foreach ($banks as $bank){
array_push($result, array(
'id' => $bank->id,
'title' => ($userLanguage == $bank->default_lang)?$bank->default_name:$bank->en_name,
'country' => Yii::t('countries',$bank->country),
));
}
return $result;
}
but it raise Object of class Accounts could not be converted to string
error ,and in trace it occured at
public static function getIdByName($name)
{
return str_replace(array('[]', '][', '[', ']', ' '), array('', '_', '_', '', '_'), $name);
}
in Chtml.php file.where is the problem?I searched a lot,but I couldn't solve the problem.
I understood where is my problem,I should use $form
instead of Chtml
.