Doctrine – A new entity was found through the relationship

I had the same problem and it was the same EntityManager. I wanted to insert an object related ManyToOne. And I don’t want a cascade persist. Example : $category = $em->find(“Category”, 10); $product = new Product(); $product->setCategory($category) $em->persist($product); $em->flush(); This throws the same exception for me. So the solution is : $category = $em->find(“Category”, 10); … Read more

Execute raw SQL using Doctrine 2

Here’s an example of a raw query in Doctrine 2 that I’m doing: public function getAuthoritativeSportsRecords() { $sql = ” SELECT name, event_type, sport_type, level FROM vnn_sport “; $em = $this->getDoctrine()->getManager(); $stmt = $em->getConnection()->prepare($sql); $stmt->execute(); return $stmt->fetchAll(); }

Doctrine – How to print out the real sql, not just the prepared statement?

Doctrine is not sending a “real SQL query” to the database server : it is actually using prepared statements, which means : Sending the statement, for it to be prepared (this is what is returned by $query->getSql()) And, then, sending the parameters (returned by $query->getParameters()) and executing the prepared statements This means there is never … Read more

Generating a single Entity from existing database using symfony2 and doctrine

I had the same problem, you’ve to do this way: php app/console doctrine:mapping:convert metadata_format \ ./src/App/MyBundle/Resources/config/doctrine \ –from-database \ –filter=”Yourtablename” Then php app/console doctrine:mapping:import AppMyBundle \ metadata_format –filter=”Yourtablename” Where metadata_format is the file ending you want to generate (e.g. xml, yml, annotation) And finally php app/console doctrine:generate:entities AppMyBundle –no-backup Like this doctrine will load only … Read more

PHPUnit Mock Objects and Static Methods

Sebastian Bergmann, the author of PHPUnit, recently had a blog post about Stubbing and Mocking Static Methods. With PHPUnit 3.5 and PHP 5.3 as well as consistent use of late static binding, you can do $class::staticExpects($this->any()) ->method(‘helper’) ->will($this->returnValue(‘bar’)); Update: staticExpects is deprecated as of PHPUnit 3.8 and will be removed completely with later versions.

Symfony: Clear doctrine cache

For Symfony 3+: php bin/console will list all commands, the following are relevant for cache: php bin/console doctrine:cache:clear-metadata php bin/console doctrine:cache:clear-query php bin/console doctrine:cache:clear-result Before Symfony 3: app/console will list how you can do it app/console doctrine:cache:clear-metadata app/console doctrine:cache:clear-query app/console doctrine:cache:clear-result

How to encode Doctrine entities to JSON in Symfony 2.0 AJAX application?

With php5.4 now you can do : use JsonSerializable; /** * @Entity(repositoryClass=”App\Entity\User”) * @Table(name=”user”) */ class MyUserEntity implements JsonSerializable { /** @Column(length=50) */ private $name; /** @Column(length=50) */ private $login; public function jsonSerialize() { return array( ‘name’ => $this->name, ‘login’=> $this->login, ); } } And then call json_encode(MyUserEntity);