Skip to content

Instantly share code, notes, and snippets.

@michellesanver
Created March 20, 2015 09:43
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save michellesanver/42f23fc77b6b9348435e to your computer and use it in GitHub Desktop.
Save michellesanver/42f23fc77b6b9348435e to your computer and use it in GitHub Desktop.
Doctrine
//So this is how it looks like in composer, so I do believe I have the latest version:
"doctrine/orm": "~2.5@dev",
"doctrine/common": "~2.5@dev",
"doctrine/dbal": "dev-retry-logic as 2.5",
"doctrine/doctrine-bundle": "~1.2",
"doctrine/migrations": "~1.0@dev",
"doctrine/doctrine-migrations-bundle": "~2.1@dev",
//My issue is with the following code that generates the error Fatal error:
Call to undefined method Doctrine\ORM\QueryBuilder::insert()
<?php
$queryBuilder = $em->createQueryBuilder()
->insert('product_browse_view')
->values(
array(
'productId' => '?',
)
)
->setParameter(0, $id);
// Ideas what I am doing wrong is very helpful! Thank you.
@fabpico
Copy link

fabpico commented Aug 7, 2015

I came with the same problem. After investigation i figured out that the 2.5 Patch of doctrine/dbal recieved the insert() method, but not the 2.5 Patch of doctrine/orm (they have both own QueryBuilder classes). So the method is still not avaible through your code above.

But instead to use the insert method of doctrine/dbal, make use of "Persisting entities": http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/working-with-objects.html#persisting-entities

  1. You need a ORM mapped DB
  2. Create getters & setters of your needed tables
  3. Then code:
// The entity to insert
$user = new User(); 
// Properties / db cols
$user->setName('Mr.Right'); 
$user->setGender('m');
// Register the query
$em->persist($user);
// Execute all registered queries
$em->flush();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment