Yii 分割textarea 批量添加

1、在\framework\db\CDbCommand.php新加insertSeveral() 方法
<span style="font-size:14px;">/**
         * @param string $table the table that new rows will be inserted into.
         * @param array $columns the column data (name=>value) to be inserted into the table.
         * @return integer number of rows affected by the execution.
         */
        
        public function insertSeveral($table, $array_columns)
        {
            $sql = '';
            $params = array();
            $i = 0;
            foreach ($array_columns as $columns) {
                $names = array();
                $placeholders = array();
                foreach ($columns as $name => $value) {
                    if (!$i) {
                        $names[] = $this->_connection->quoteColumnName($name);
                    }
                    if ($value instanceof CDbExpression) {
                        $placeholders[] = $value->expression;
                        foreach ($value->params as $n => $v)
                            $params[$n] = $v;
                    } else {
                        $placeholders[] = ':' . $name . $i;
                        $params[':' . $name . $i] = $value;
                    }
                }
                if (!$i) {
                    $sql = 'INSERT INTO ' . $this->_connection->quoteTableName($table)
                        . ' (' . implode(', ', $names) . ') VALUES ('
                        . implode(', ', $placeholders) . ')';
                } else {
                    $sql .= ',(' . implode(', ', $placeholders) . ')';
                }
                $i++;
            }
            return $this->setText($sql)->execute($params);
        }</span>

第二步:执行添加
<span style="font-size:14px;">$school = new Schoolattr(); 

$postList = $school->attributes = $_POST['Schoolattr']; 

$countries_type= $postList['countries_type']; 

$nameList = explode("\n", $postList['name']); 

foreach($nameList as $key => $value){ 
	$schooldata['name'] = $value; 

	$schooldata['pid'] = 0; 

	$schooldata['countries_type'] = $countries_type; 

	$schooldata['level'] =1; 
	
	$command = Yii::app()->db->createCommand(); 

	$command->insert('un_school_attr', $schooldata); 
} </span>



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值