Yii 分割textarea 批量添加

来源:互联网 发布:北京凯恩帝数控编程 编辑:程序博客网 时间:2024/06/16 09:25
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
原创粉丝点击