Is there a built-in way to get all of the changed/updated fields in a Doctrine 2 entity

You can use Doctrine\ORM\EntityManager#getUnitOfWork to get a Doctrine\ORM\UnitOfWork. Then just trigger changeset computation (works only on managed entities) via Doctrine\ORM\UnitOfWork#computeChangeSets(). You can use also similar methods like Doctrine\ORM\UnitOfWork#recomputeSingleEntityChangeSet(Doctrine\ORM\ClassMetadata $meta, $entity) if you know exactly what you want to check without iterating over the entire object graph. After that you can use Doctrine\ORM\UnitOfWork#getEntityChangeSet($entity) to retrieve all … Read more

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

What is Doctrine hydration? [closed]

Hydration is a method used to return query results. For example: HYDRATE_ARRAY – This will return you an array of records that are represented by another array: $q = Doctrine_Query::create() ->from(‘Post p’) ->setHydrationMode(Doctrine::HYDRATE_ARRAY); $resultSet = $q->execute(); // $resultSet is an array foreach ($resultSet as $post) { // $post is an array echo $post[‘title’]; } HYDRATE_RECORD … Read more

cascade={“remove”} VS orphanRemoval=true VS ondelete=”CASCADE

onDelete=”CASCADE” is managed by the database itself. cascade={“remove”} is managed by doctrine. onDelete=”CASCADE” is faster because the operations are performed on database level instead by doctrine. The removing is performed by the database server and not Doctrine. With cascade={“remove”} doctrine has to manage the entity itself and will perform extra checks to see if it … 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 2 can’t use nullable=false in manyToOne relation?

Use the JoinColumn annotation on your ManyToOne relation: /** * @ORM\ManyToOne(targetEntity=”Package”, inversedBy=”users”) * @ORM\JoinColumn(name=”package_id”, referencedColumnName=”id”, nullable=false) */ private $package; The ManyToOne itself cannot be nullable, because it doesn’t relate to a specific column. The JoinColumn on the other hand identifies the column in the database. Thus, you can use “normal” attributes like nullable or unique!

PHP ORMs: Doctrine vs. Propel

I’d go with Doctrine. It seems to me that it is a much more active project and being the default ORM for symfony it is better supported (even though officially the ORMs are considered equal). Furthermore I better like the way you work with queries (DQL instead of Criteria): <?php // Propel $c = new … Read more

Order by multiple columns with Doctrine

You have to add the order direction right after the column name: $qb->orderBy(‘column1 ASC, column2 DESC’); As you have noted, multiple calls to orderBy do not stack, but you can make multiple calls to addOrderBy: $qb->addOrderBy(‘column1’, ‘ASC’) ->addOrderBy(‘column2’, ‘DESC’);

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

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)