2014-01-24 81 views
1

我是新來的Yii,我創造降下來的工作。我面對錯誤,請給我建議。這裏他我下拉代碼對象無法轉換爲字符串錯誤警予

<?php 
/* @var $this CreateproductController */ 
/* @var $model Createproduct */ 
/* @var $form CActiveForm */ 
?> 

<div class="form"> 

<?php $form=$this->beginWidget('CActiveForm', array(
    'id'=>'createproduct-form', 
    'method'=>'post', 
    // Please note: When you enable ajax validation, make sure the corresponding 
    // controller action is handling ajax validation correctly. 
    // There is a call to performAjaxValidation() commented in generated controller code. 
    // See class documentation of CActiveForm for details on this.E0E0E0 EFF0F2 
    'enableAjaxValidation'=>false, 
)); ?> 

    <?php echo $form->errorSummary($model); ?> 


    <div class="row" style="background-color: #F7F7F7; height:30px;"> 
     <?php echo $form->labelEx($model,'Product Type'); ?>&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp 
     <?php echo CHtml::dropDownList($model,'pr_name',$model->getTypeOptions()); ?> 
     <?php echo $form->error($model,'pr_type'); ?> 
    </div> 

    <div class="row buttons"> 
     <?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save',array('class'=>'button1')); ?> 
    </div> 

<?php $this->endWidget(); ?> 

</div> 

的getTypeOptions()方法已經下面和恆定使用的方法內,並宣佈

const CAT_EMPTY=0; 
const CAT_PHONE=1; 
const CAT_ROUTERS=2; 
const CAT_ACCESSORIES=3; 
const CAT_SERVICES=4; 

public function getTypeOptions() 
{ 
    return array(
    self::CAT_EMPTY=>'Select_Category', 
    self::CAT_PHONE=>'Phones', 
    self::CAT_ROUTERS=>'Routers', 
    self::CAT_ACCESSORIES=>'Accessories', 
    self::CAT_SERVICES=>'Services', 



    ); 
} 

幫我..

+0

什麼是你所得到的錯誤? –

+0

類Create Product的對象無法轉換爲字符串錯誤在yii..Createproduct我的模型類 – sonorita

+0

這是什麼? '自:: CAT_EMPTY'?它是字符串嗎? –

回答

1

問題在CHtml::dropDownList用法你應該使用這樣的:

<?php echo CHtml::dropDownList("model_name[pr_name]",'selected_value',$model->getTypeOptions()); ?> 

或者,你可以使用$form部件dropDownList功能

<?php echo $form->dropDownList($model, 'pr_name',$model->getTypeOptions()); ?> 
+0

我使用dropDownList ?>但我在上面的代碼中得到錯誤..如果我刪除代碼中的$模型它顯示下拉列表,但我不會得到在控制器方法中的下拉值..任何建議 – sonorita

+0

嘗試第一種方法.. –

+1

謝謝人終於我用上面的答案做了 – sonorita