มีปัญหาเรื่องการใช้งาน dropDownlist ที่มีการดึงข้อมูลจากฐานข้อมูลครับ

wave
Sanya Namburi
2,245
สังคมนักพัฒนาระบบ (Community)
มีปัญหาเรื่องการใช้งาน dropDownlist ที่มีการดึงข้อมูลจากฐานข้อมูลครับ

<p>เรื่องมีอยู่ว่า ผมได้แก้โคด _form.php ของ research ซึ่งเดิมเป็น textfield จะเปลี่ยนให้เป็น dropDownlist ที่ดึงข้อมูลจาก ตาราง researcher </p><p>ซึ่งเมื่อแก้จาก textfield เป็น dropDownlist เกิด error ขึ้น**</p><p>โคดที่ผมใช้ครับ</p><p>&lt;div&gt;</p><p>&lt;?php echo $form-&gt;labelEx($model,'idresearcher'); ?&gt;</p><p>&lt;?php echo $form-&gt;textField($model,'idresearcher'); ?&gt;</p><p>&lt;?php echo $form-&gt;error($model,'idresearcher'); ?&gt;</p><p><span style="background-color: initial;">&lt;/div&gt;</span> </p><p>ไม่ทราบว่ากรณีอย่างนี้เกิดเพราะอะไรครับ</p><p>** ---------------------error --------------------</p><h1>CException</h1><p>Property "Research.researcher" is not defined.</p><p>/home/rdkkc/public_html/framework/db/ar/CActiveRecord.php(145)</p><pre>133 */ 134 public function __get($name) 135 { 136 if(isset($this-&gt;_attributes[$name])) 137 return $this-&gt;_attributes[$name]; 138 elseif(isset($this-&gt;getMetaData()-&gt;columns[$name])) 139 return null; 140 elseif(isset($this-&gt;_related[$name])) 141 return $this-&gt;_related[$name]; 142 elseif(isset($this-&gt;getMetaData()-&gt;relations[$name])) 143 return $this-&gt;getRelated($name); 144 else 145 return parent::__get($name); 146 } 147 148 /** 149 * PHP setter magic method. 150 * This method is overridden so that AR attributes can be accessed like properties. 151 * @param string $name property name 152 * @param mixed $value property value 153 */ 154 public function __set($name,$value) 155 { 156 if($this-&gt;setAttribute($name,$value)===false) 157 { </pre><h2>Stack Trace</h2><table><tbody><tr><td>#0</td><td><p>+ /home/rdkkc/public_html/framework/db/ar/CActiveRecord.php(145): <strong>CComponent</strong>-&gt;<strong>__get</strong>("researcher") </p></td></tr><tr> <td>#1</td> <td> <p>+</p> /home/rdkkc/public_html/framework/web/helpers/CHtml.php(2728): <strong>CActiveRecord</strong>-&gt;<strong>__get</strong>("researcher") </td></tr><tr> <td>#2</td> <td> <p>+</p> /home/rdkkc/public_html/framework/web/helpers/CHtml.php(1981): <strong>CHtml</strong>::<strong>resolveValue</strong>(Research, "researcher") </td></tr><tr> <td>#3</td> <td> <p>+</p> /home/rdkkc/public_html/framework/web/widgets/CActiveForm.php(914): <strong>CHtml</strong>::<strong>activeDropDownList</strong>(Research, "researcher", array("" =&gt; "พลนิกรณ์"), array("empty" =&gt; "เลือกการแสดงผล")) </td></tr><tr> <td>#4</td> <td> <p>–</p> /home/rdkkc/public_html/protected/views/research/_form.php(56): <strong>CActiveForm</strong>-&gt;<strong>dropDownList</strong>(Research, "researcher", array("" =&gt; "พลนิกรณ์"), array("empty" =&gt; "เลือกการแสดงผล"))<pre>51 52 &lt;div&gt; 53 &lt;?php echo $form-&gt;labelEx($model,'researcher'); ?&gt; 54 &lt;?php echo $form-&gt;dropDownList($model,'researcher', 55 CHtml::listData(Researcher::model()-&gt;findAll(),'id','name'), 56 array('empty' =&gt; 'เลือกการแสดงผล'));?&gt; 57 58 &lt;?php echo $form-&gt;error($model,'researcher'); ?&gt; 59 &lt;/div&gt; 60 61 &lt;div&gt; </pre> </td></tr><tr><td>#5</td><td><p>+ /home/rdkkc/public_html/framework/web/CBaseController.php(126): <strong>require</strong>("/home/rdkkc/public_html/protected/views/research/_form.php")</p></td></tr><tr><td>#6</td><td><p>+</p><p> /home/rdkkc/public_html/framework/web/CBaseController.php(95): <strong>CBaseController</strong>-&gt;<strong>renderInternal</strong>("/home/rdkkc/public_html/protected/views/research/_form.php", array("model" =&gt; Research), true) </p></td></tr><tr><td>#7</td><td><p>+</p><p> /home/rdkkc/public_html/framework/web/CController.php(869): <strong>CBaseController</strong>-&gt;<strong>renderFile</strong>("/home/rdkkc/public_html/protected/views/research/_form.php", array("model" =&gt; Research), true) </p></td></tr><tr><td>#8</td><td><p>–</p> /home/rdkkc/public_html/protected/views/research/create.php(18): <strong>CController</strong>-&gt;<strong>renderPartial</strong>("_form", array("model" =&gt; Research))<pre>13 ); 14 ?&gt; 15 16 &lt;h1&gt;Create Research&lt;/h1&gt; 17 18 &lt;?php $this-&gt;renderPartial('_form', array('model'=&gt;$model)); ?&gt;</pre> </td></tr><tr><td>#9</td><td><p>+ /home/rdkkc/public_html/framework/web/CBaseController.php(126): <strong>require</strong>("/home/rdkkc/public_html/protected/views/research/create.php")</p></td></tr><tr><td>#10</td><td><p>+</p><p> /home/rdkkc/public_html/framework/web/CBaseController.php(95): <strong>CBaseController</strong>-&gt;<strong>renderInternal</strong>("/home/rdkkc/public_html/protected/views/research/create.php", array("model" =&gt; Research), true) </p></td></tr><tr><td>#11</td><td><p>+</p><p> /home/rdkkc/public_html/framework/web/CController.php(869): <strong>CBaseController</strong>-&gt;<strong>renderFile</strong>("/home/rdkkc/public_html/protected/views/research/create.php", array("model" =&gt; Research), true) </p></td></tr><tr><td>#12</td><td><p>+</p><p> /home/rdkkc/public_html/framework/web/CController.php(782): <strong>CController</strong>-&gt;<strong>renderPartial</strong>("create", array("model" =&gt; Research), true) </p></td></tr><tr><td>#13</td><td><p>–</p> /home/rdkkc/public_html/protected/controllers/ResearchController.php(79): <strong>CController</strong>-&gt;<strong>render</strong>("create", array("model" =&gt; Research))<pre>74 $this-&gt;redirect(array('view','id'=&gt;$model-&gt;idResearch)); 75 } 76 77 $this-&gt;render('create',array( 78 'model'=&gt;$model, 79 )); 80 } 81 82 /** 83 * Updates a particular model. 84 * If update is successful, the browser will be redirected to the 'view' page. </pre> </td></tr><tr><td>#14</td><td><p>+ /home/rdkkc/public_html/framework/web/actions/CInlineAction.php(49): <strong>ResearchController</strong>-&gt;<strong>actionCreate</strong>()</p></td></tr><tr><td>#15</td><td><p>+</p><p> /home/rdkkc/public_html/framework/web/CController.php(308): <strong>CInlineAction</strong>-&gt;<strong>runWithParams</strong>(array("r" =&gt; "research/create")) </p></td></tr><tr><td>#16</td><td><p>+</p><p> /home/rdkkc/public_html/framework/web/filters/CFilterChain.php(133): <strong>CController</strong>-&gt;<strong>runAction</strong>(CInlineAction) </p></td></tr><tr><td>#17</td><td><p>+</p><p> /home/rdkkc/public_html/framework/web/filters/CFilter.php(40): <strong>CFilterChain</strong>-&gt;<strong>run</strong>() </p></td></tr><tr><td>#18</td><td><p>–</p> /home/rdkkc/public_html/framework/web/CController.php(1145): <strong>CFilter</strong>-&gt;<strong>filter</strong>(CFilterChain)<pre>1140 */ 1141 public function filterAccessControl($filterChain) 1142 { 1143 $filter=new CAccessControlFilter; 1144 $filter-&gt;setRules($this-&gt;accessRules()); 1145 $filter-&gt;filter($filterChain); 1146 } 1147 1148 /** 1149 * Returns a persistent page state value. 1150 * A page state is a variable that is persistent across POST requests of the same page. </pre> </td></tr><tr><td>#19</td><td><p>+ /home/rdkkc/public_html/framework/web/filters/CInlineFilter.php(58): <strong>CController</strong>-&gt;<strong>filterAccessControl</strong>(CFilterChain)</p></td></tr><tr><td>#20</td><td><p>+</p><p> /home/rdkkc/public_html/framework/web/filters/CFilterChain.php(130): <strong>CInlineFilter</strong>-&gt;<strong>filter</strong>(CFilterChain) </p></td></tr><tr><td>#21</td><td><p>+</p><p> /home/rdkkc/public_html/framework/web/CController.php(291): <strong>CFilterChain</strong>-&gt;<strong>run</strong>() </p></td></tr><tr><td>#22</td><td><p>+</p><p> /home/rdkkc/public_html/framework/web/CController.php(265): <strong>CController</strong>-&gt;<strong>runActionWithFilters</strong>(CInlineAction, array("accessControl", "postOnly + delete")) </p></td></tr><tr><td>#23</td><td><p>+</p><p> /home/rdkkc/public_html/framework/web/CWebApplication.php(282): <strong>CController</strong>-&gt;<strong>run</strong>("create") </p></td></tr><tr><td>#24</td><td><p>+</p><p> /home/rdkkc/public_html/framework/web/CWebApplication.php(141): <strong>CWebApplication</strong>-&gt;<strong>runController</strong>("research/create") </p></td></tr><tr><td>#25</td><td><p>+</p><p> /home/rdkkc/public_html/framework/base/CApplication.php(184): <strong>CWebApplication</strong>-&gt;<strong>processRequest</strong>() </p></td></tr><tr><td>#26</td><td><p>+</p><p> /home/rdkkc/public_html/index.php(13): <strong>CApplication</strong>-&gt;<strong>run</strong>() </p></td></tr></tbody></table><p>2015-08-07 21:52:26 Apache/2.4.10 (Debian) <a href="http://www.yiiframework.com/">Yii Framework</a>/1.1.16</p>

3 ความคิดเห็น

  • Sanya Namburi

  • มานพ กองอุ่น

เรื่องมีอยู่ว่า ผมได้แก้โคด _form.php ของ research ซึ่งเดิมเป็น textfield จะเปลี่ยนให้เป็น dropDownlist ที่ดึงข้อมูลจาก ตาราง researcher

ซึ่งเมื่อแก้จาก textfield เป็น dropDownlist เกิด error ขึ้น**

โคดที่ผมใช้ครับ

<div>

<?php echo $form->labelEx($model,'idresearcher'); ?>

<?php echo $form->textField($model,'idresearcher'); ?>

<?php echo $form->error($model,'idresearcher'); ?>

</div>

ไม่ทราบว่ากรณีอย่างนี้เกิดเพราะอะไรครับ

** ---------------------error --------------------

CException

Property "Research.researcher" is not defined.

/home/rdkkc/public_html/framework/db/ar/CActiveRecord.php(145)

133      */
134     public function __get($name)
135     {
136         if(isset($this->_attributes[$name]))
137             return $this->_attributes[$name];
138         elseif(isset($this->getMetaData()->columns[$name]))
139             return null;
140         elseif(isset($this->_related[$name]))
141             return $this->_related[$name];
142         elseif(isset($this->getMetaData()->relations[$name]))
143             return $this->getRelated($name);
144         else
145             return parent::__get($name);
146     }
147 
148     /**
149      * PHP setter magic method.
150      * This method is overridden so that AR attributes can be accessed like properties.
151      * @param string $name property name
152      * @param mixed $value property value
153      */
154     public function __set($name,$value)
155     {
156         if($this->setAttribute($name,$value)===false)
157         {

Stack Trace

#0

+ /home/rdkkc/public_html/framework/db/ar/CActiveRecord.php(145): CComponent->__get("researcher")

#1

+

/home/rdkkc/public_html/framework/web/helpers/CHtml.php(2728): CActiveRecord->__get("researcher")
#2

+

/home/rdkkc/public_html/framework/web/helpers/CHtml.php(1981): CHtml::resolveValue(Research, "researcher")
#3

+

/home/rdkkc/public_html/framework/web/widgets/CActiveForm.php(914): CHtml::activeDropDownList(Research, "researcher", array("" => "พลนิกรณ์"), array("empty" => "เลือกการแสดงผล"))
#4

/home/rdkkc/public_html/protected/views/research/_form.php(56): CActiveForm->dropDownList(Research, "researcher", array("" => "พลนิกรณ์"), array("empty" => "เลือกการแสดงผล"))
51 
52     <div>
53         <?php echo $form->labelEx($model,'researcher'); ?>
54         <?php echo $form->dropDownList($model,'researcher', 
55                 CHtml::listData(Researcher::model()->findAll(),'id','name'),
56                 array('empty' => 'เลือกการแสดงผล'));?>
57         
58         <?php echo $form->error($model,'researcher'); ?>
59     </div>
60 
61     <div>
#5

+ /home/rdkkc/public_html/framework/web/CBaseController.php(126): require("/home/rdkkc/public_html/protected/views/research/_form.php")

#6

+

/home/rdkkc/public_html/framework/web/CBaseController.php(95): CBaseController->renderInternal("/home/rdkkc/public_html/protected/views/research/_form.php", array("model" => Research), true)

#7

+

/home/rdkkc/public_html/framework/web/CController.php(869): CBaseController->renderFile("/home/rdkkc/public_html/protected/views/research/_form.php", array("model" => Research), true)

#8

/home/rdkkc/public_html/protected/views/research/create.php(18): CController->renderPartial("_form", array("model" => Research))
13 );
14 ?>
15 
16 <h1>Create Research</h1>
17 
18 <?php $this->renderPartial('_form', array('model'=>$model)); ?>
#9

+ /home/rdkkc/public_html/framework/web/CBaseController.php(126): require("/home/rdkkc/public_html/protected/views/research/create.php")

#10

+

/home/rdkkc/public_html/framework/web/CBaseController.php(95): CBaseController->renderInternal("/home/rdkkc/public_html/protected/views/research/create.php", array("model" => Research), true)

#11

+

/home/rdkkc/public_html/framework/web/CController.php(869): CBaseController->renderFile("/home/rdkkc/public_html/protected/views/research/create.php", array("model" => Research), true)

#12

+

/home/rdkkc/public_html/framework/web/CController.php(782): CController->renderPartial("create", array("model" => Research), true)

#13

/home/rdkkc/public_html/protected/controllers/ResearchController.php(79): CController->render("create", array("model" => Research))
74                 $this->redirect(array('view','id'=>$model->idResearch));
75         }
76 
77         $this->render('create',array(
78             'model'=>$model,
79         ));
80     }
81 
82     /**
83      * Updates a particular model.
84      * If update is successful, the browser will be redirected to the 'view' page.
#14

+ /home/rdkkc/public_html/framework/web/actions/CInlineAction.php(49): ResearchController->actionCreate()

#15

+

/home/rdkkc/public_html/framework/web/CController.php(308): CInlineAction->runWithParams(array("r" => "research/create"))

#16

+

/home/rdkkc/public_html/framework/web/filters/CFilterChain.php(133): CController->runAction(CInlineAction)

#17

+

/home/rdkkc/public_html/framework/web/filters/CFilter.php(40): CFilterChain->run()

#18

/home/rdkkc/public_html/framework/web/CController.php(1145): CFilter->filter(CFilterChain)
1140      */
1141     public function filterAccessControl($filterChain)
1142     {
1143         $filter=new CAccessControlFilter;
1144         $filter->setRules($this->accessRules());
1145         $filter->filter($filterChain);
1146     }
1147 
1148     /**
1149      * Returns a persistent page state value.
1150      * A page state is a variable that is persistent across POST requests of the same page.
#19

+ /home/rdkkc/public_html/framework/web/filters/CInlineFilter.php(58): CController->filterAccessControl(CFilterChain)

#20

+

/home/rdkkc/public_html/framework/web/filters/CFilterChain.php(130): CInlineFilter->filter(CFilterChain)

#21

+

/home/rdkkc/public_html/framework/web/CController.php(291): CFilterChain->run()

#22

+

/home/rdkkc/public_html/framework/web/CController.php(265): CController->runActionWithFilters(CInlineAction, array("accessControl", "postOnly + delete"))

#23

+

/home/rdkkc/public_html/framework/web/CWebApplication.php(282): CController->run("create")

#24

+

/home/rdkkc/public_html/framework/web/CWebApplication.php(141): CWebApplication->runController("research/create")

#25

+

/home/rdkkc/public_html/framework/base/CApplication.php(184): CWebApplication->processRequest()

#26

+

/home/rdkkc/public_html/index.php(13): CApplication->run()

2015-08-07 21:52:26 Apache/2.4.10 (Debian) Yii Framework/1.1.16

มันบอกว่าไม่มี attribute researcher ครับ ดูจาก code ข้างบนจะเห็นว่า attribute คือ idresearcher ครับ เปลี่ยนตรงนี้ครับ

dropDownList($model,'idresearcher',

ขอบคุณครับ