yii2覆盖数据库类的batchinsert

时间:2017-01-28 11:18:06

标签: php mysql yii2

我想覆盖batchinsert,因为我想添加 @section('SampleSection') @component( 'mylayouts.partials.contentheader' ) @slot('title') Sample Title @endslot @slot('indexes') Pass array here // example [ 'a', 'b', 'c' ] @endslot @endcomponent @endsection

ON DUPLICATE KEY UPDATE

这是有效但现在我需要覆盖yii \ db \ QueryBuilder 如果找到了这个:$result = Yii::$app->db->createCommand()->batchInsert('product', ['asin', 'title', 'image', 'url', 'price'], $results)->execute(); public function batchInsert($table, $columns, $rows) { if (empty($rows)) { return ''; } $schema = $this->db->getSchema(); if (($tableSchema = $schema->getTableSchema($table)) !== null) { $columnSchemas = $tableSchema->columns; } else { $columnSchemas = []; } $values = []; foreach ($rows as $row) { $vs = []; foreach ($row as $i => $value) { if (isset($columns[$i], $columnSchemas[$columns[$i]]) && !is_array($value)) { $value = $columnSchemas[$columns[$i]]->dbTypecast($value); } if (is_string($value)) { $value = $schema->quoteValue($value); } elseif ($value === false) { $value = 0; } elseif ($value === null) { $value = 'NULL'; } $vs[] = $value; } $values[] = '(' . implode(', ', $vs) . ')'; } $query = 'INSERT INTO ' . $schema->quoteTableName($table); $duplicate = ' ON DUPLICATE KEY UPDATE '; $last = end($columns); reset($columns); foreach ($columns as $i => $name) { $columns[$i] = $schema->quoteColumnName($name); $duplicate .= $schema->quoteColumnName($name) . ' = VALUES(' . $schema->quoteColumnName($name) . ')'; if ($name <> $last) { $duplicate .= ', '; } } $query .= ' (' . implode(', ', $columns) . ') '; $query .= ' VALUES ' . implode(', ', $values); $query .= $duplicate; return $query; }

我可以覆盖命令类,但是yii \ db \ Schema中的代码也应该更新

Yii::$app->db->commandClass = new common\models\Command();

或者我需要做一些完全不同的事情,我该如何解决这个问题?

2 个答案:

答案 0 :(得分:1)

根据开发人员的C# - Populating Gridview,您可以执行以下操作(未经测试):

registration.showNotification('title', {body: 'message'})

或者你可以这样做:

class MyQueryBuilder extends yii\db\mysql\QueryBuilder
{
    public function batchInsert($table, $columns, $rows)
    {
        $sql = parent::batchInsert($table, $columns, $rows);
        $sql .= 'ON DUPLICATE KEY UPDATE';
        return $sql;
    }
}

答案 1 :(得分:0)

<?php

namespace common\models;

use yii\db\mysql\QueryBuilder as baseQueryBuilder;

class QueryBuilder extends baseQueryBuilder
{
    public function batchInsert($table, $columns, $rows)
    {
        $sql = parent::batchInsert($table, $columns, $rows);
        $sql .= ' ON DUPLICATE KEY UPDATE ';

        $schema = $this->db->getSchema();
        $last = end($columns);
        reset($columns);

        foreach ($columns as $i => $column)
        {
            $columns[$i] = $schema->quoteColumnName($column);
            $sql .= $schema->quoteColumnName($column) . ' = VALUES(' . $schema->quoteColumnName($column) . ')';

            if ($column <> $last) {
                $sql .= ', ';
            }
        }

        return $sql;
    }
}


$db = Yii::$app->db;
$queryBuilder = new \common\models\QueryBuilder(Yii::$app->db);
$query = $queryBuilder->batchInsert('product', ['asin', 'title', 'image', 'url', 'price'], $results);
$db->createCommand($query)->execute();