Hi, I am new to Yii. After 2 days trying, I am still stuck in the listData. Hope anyone can help out of the problem.
I want to display a dropdown list with the value selected from db. And I am building my db with DAO.
In short, this is my select statement:
class dbAdmin
{ public static function selectDept(){
$conn = Yii::app()->db;
$sql = "select deptid, deptname from eg_dept order by deptid";
return $conn->createCommand($sql)->queryColumn();
}
}
when I var_dump() the above returned value, i can see the result:
all I saw in the discussions are using ActiveRecord, but in my case, I can only use the DAO method. Could it be the reason why the listData can’t generate the correct output?
You will see that listData works only with ActiveRecords as for the first parameter it expects an array of models…
and what listData does is that it generates an array of values that you already have by using DAO and that you saw with var_dump()… .so you don’t need to call listData at all
Correct me if I get it wrongly, I thought listData can help to re-organize the array I get from the DB to something like array(‘Finanace’=>‘Finance’, ‘HR’=>‘HR’), instead of array(‘0’=>‘Finance’, ‘1’=>‘HR’).
Now I use a "manual" way to achieve this:
$depts = dbAdmin::selectDept();
foreach($depts as $value){
$this->dept_list[$value] = $value;
}
and need to correct a mistake of the sql statement I post in the #1 reply:
$sql = "select deptid from eg_dept order by deptid";