-->

Missing insert() method on Doctrine DBAL Query Bui

2019-02-25 04:17发布

问题:

I feel like I'm having a moment where I'm missing something small here; I've been having issues using the insert() method on the QueryBuilder component on Dotrine DBAL 2.2.x / 2.3.x.

I did some investigation and here's the snippet from the QueryBuilder page from the DBAL Documantation

The \Doctrine\DBAL\Query\QueryBuilder supports building SELECT, INSERT, UPDATE and DELETE queries. Which sort of query you are building depends on the methods you are using.

It goes on further to explain code examples, such that I can simply do:

$builder = $connection->createQueryBuilder();
$result = $builder
    ->insert('table_name')
    // ...

To use the query builder in Insert Mode. Except when I do I get a complaint here from PHP:

Fatal error: Call to undefined method Doctrine\DBAL\Query\QueryBuilder::insert()

On further inspection of The QueryBuilder.php Source Code

I see no reference to any method insert(...), no class to inherit this from, no traits added to the QueryBuilder that could expose the insert mechanism. In addition I see this right at the top:

/* The query types. */
const SELECT = 0;
const DELETE = 1;
const UPDATE = 2;

There's no insert query type; there is however this interesting method comment for execute():

/**
 * Execute this query using the bound parameters and their types.
 *
 * Uses {@see Connection::executeQuery} for select statements and {@see Connection::executeUpdate}
 * for insert, update and delete statements.
 *
 * @return mixed
 */

Bottom Line:

This is a massive project with 100's of maintainers, I'm more likely to find my interpretation suspect here than a screwup on something so fundamental over numerous versions, but I cannot for the life of me figure out what I'm missing. Please help me see the obvious.

回答1:

It depends on your version. Insert has been added since v2.5.0-BETA3.

Viz https://github.com/doctrine/dbal/blob/master/lib/Doctrine/DBAL/Query/QueryBuilder.php#L563 and commit

You can decide to update package version or check this alternative solution