在我的模型属性我有属性 - 规格:传递数组从形式
class Category extends CActiveRecord
{
private $_specifications = array();
public function getSpecifications()
{
return $this->_specifications;
}
public function setSpecifications($specifications)
{
$this->_specifications = implode(', ', $specifications);
}
所以我想规范是一个数组。
我的视图文件:
<div id="specifications" class="row">
<?php echo $form->labelEx($model,'specifications'); ?>
<?php echo $form->textField($model,'specifications',array('rows'=>6, 'cols'=>50, 'name'=>'Category[specifications][0]', 'class' => 'clonedInput')); ?>
<?php echo $form->textField($model,'specifications',array('rows'=>6, 'cols'=>50, 'name'=>'Category[specifications][1]', 'class' => 'clonedInput')); ?>
<?php echo $form->error($model,'specifications'); ?>
</div>
当我发送的形式我得到一个错误:
htmlspecialchars() expects parameter 1 to be string, array given
...
public static function encode($text)
84 {
85 return htmlspecialchars($text,ENT_QUOTES,Yii::app()->charset);
86 }
我试图禁用编码:
<?php echo $form->textField($model,'specifications',array('encode'=>false, 'rows'=>6, 'cols'=>50, 'name'=>'Category[specifications][0]', 'class' => 'clonedInput')); ?>
<?php echo $form->textField($model,'specifications',array('encode'=>false, 'rows'=>6, 'cols'=>50, 'name'=>'Category[specifications][1]', 'class' => 'clonedInput')); ?>
但在这种情况下,这是另一个错误:
Array to string conversion
...
2216 $html .= ' ' . $name . '="' . ($raw ? $value : self::encode($value)) . '"';
有人可以给一个建议,我应该怎么做才能从窗体传递数组?谢谢。
那么我应该在哪里调用这个函数? – 2013-04-07 18:32:01
没有地方,因为我写了'视图可以保持原样'。)当你指定它们时,Yii会自动检查变量和虚拟属性(getXyz)。你也可以编写'$ model = new MyModel(); echo $ model-> specifications;' – Tim 2013-04-07 18:36:11
implode():传递的参数无效 – 2013-04-07 18:43:29