Doctrine 2 OneToMany Cascade SET NULL

You should add the option onDelete=”SET NULL” in the annotation of your entity Publication like this: class Publication { /** * @ORM\ManyToOne(targetEntity=”Teacher”, inversedBy=”publications”) * @ORM\JoinColumn(name=”teacher_id”, referencedColumnName=”id”, onDelete=”SET NULL”) */ protected $teacher; } This modification is registered in the table declaration itself. Hence, you need a database migration or a schema change for this to take … Read more

php/symfony/doctrine memory leak?

Tried doing $cupo->save(); $cupo->free(); $cupo = null; (But substituting my code) And I’m still getting memory overflows. Any other ideas, SO? Update: I created a new environment in my databases.yml, that looks like: all: doctrine: class: sfDoctrineDatabase param: dsn: ‘mysql:host=localhost;dbname=…….’ username: ….. password: ….. profiler: false The profiler: false entry disables doctrine’s query logging, that … Read more

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

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

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

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(); }

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);