如何在使用Cdbcommandbuilder :: createMultipleInsertCommand()时运行/使用验证

时间:2013-12-14 10:01:39

标签: php yii

Yii 1.1.14

我使用CDbCommandBuilder::createMultipleInsertCommand()创建多个插入CDbCommand。但是如何在保存之前运行验证? 对于单个插入命令,验证是在CActiveRecord::save()方法内完成的(我认为)。

// Controller

if (isset($_POST['ProductBrand'])) {
    $model->attributes = $_POST['ProductBrand'];

    if ($model->saveRows())
        $this->redirect(array('indexBrand'));

}

// Model


public function saveRows()
{
    $builder = Yii::app()->db->schema->commandBuilder;
    $names = explode(', ', $this->attributes['name']);
    $type_id = $this->attributes['type_id'];
    $attributes = array();
    foreach ($names as $name) :
        $attributes[] = array('name' => $name, 'type_id' => $type_id);
    endforeach;
    $command = $builder->createMultipleInsertCommand('vwa_product_brand',
        $attributes);
    if ($command->execute()) :
        return TRUE;
    endif;
    return FALSE;
}

感谢任何帮助。

1 个答案:

答案 0 :(得分:2)

Yii提供在模型中保存之前和之后调用函数的选项。因此,您可以在beforesave函数中调用自定义验证函数。

在控制器中调用验证的另一种方法,请遵循以下语法

  

MODELNAME ::模型() - >验证();

有关详细信息,请参阅以下链接

http://www.yiiframework.com/doc/api/1.1/CActiveRecord#beforeSave-detail

http://www.yiiframework.com/forum/index.php/topic/26637-solved-cactiveform-validate-on-ajax-submit/