Default value in Doctrine

<?php /** * @Entity */ class myEntity { /** * @var string * * @ORM\Column(name=”myColumn”, type=”integer”, options={“default” : 0}) */ private $myColumn; … } Note that this uses SQL DEFAULT, which is not supported for some fields like BLOB and TEXT.

How to do left join in Doctrine?

If you have an association on a property pointing to the user (let’s say Credit\Entity\UserCreditHistory#user, picked from your example), then the syntax is quite simple: public function getHistory($users) { $qb = $this->entityManager->createQueryBuilder(); $qb ->select(‘a’, ‘u’) ->from(‘Credit\Entity\UserCreditHistory’, ‘a’) ->leftJoin(‘a.user’, ‘u’) ->where(‘u = :user’) ->setParameter(‘user’, $users) ->orderBy(‘a.created_at’, ‘DESC’); return $qb->getQuery()->getResult(); } Since you are applying a condition … Read more

Why I am suddenly getting a “Typed property must not be accessed before initialization” error when introducing properties type hints?

Since PHP 7.4 introduces type-hinting for properties, it is particularly important to provide valid values for all properties, so that all properties have values that match their declared types. A property that has never been assigned doesn’t have a null value, but it is on an undefined state, which will never match any declared type. … Read more

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

Count Rows in Doctrine QueryBuilder

Something like: $qb = $entityManager->createQueryBuilder(); $qb->select(‘count(account.id)’); $qb->from(‘ZaysoCoreBundle:Account’,’account’); $count = $qb->getQuery()->getSingleScalarResult(); Some folks feel that expressions are somehow better than just using straight DQL. One even went so far as to edit a four year old answer. I rolled his edit back. Go figure.

Doing a WHERE .. IN subquery in Doctrine 2

This is how I would try it: /** @var Doctrine\ORM\EntityManager $em */ $expr = $em->getExpressionBuilder(); $em->createQueryBuilder() ->select(array(‘DISTINCT i.id’, ‘i.name’, ‘o.name’)) ->from(‘Item’, ‘i’) ->join(‘i.order’, ‘o’) ->where( $expr->in( ‘o.id’, $em->createQueryBuilder() ->select(‘o2.id’) ->from(‘Order’, ‘o2’) ->join(‘Item’, ‘i2’, \Doctrine\ORM\Query\Expr\Join::WITH, $expr->andX( $expr->eq(‘i2.order’, ‘o2’), $expr->eq(‘i2.id’, ‘?1’) ) ) ->getDQL() ) ) ->andWhere($expr->neq(‘i.id’, ‘?2’)) ->orderBy(‘o.orderdate’, ‘DESC’) ->setParameter(1, 5) ->setParameter(2, 5) ; I didn’t … Read more

Doctrine2: Best way to handle many-to-many with extra columns in reference table

I’ve opened a similar question in the Doctrine user mailing list and got a really simple answer; consider the many to many relation as an entity itself, and then you realize you have 3 objects, linked between them with a one-to-many and many-to-one relation. http://groups.google.com/group/doctrine-user/browse_thread/thread/d1d87c96052e76f7/436b896e83c10868#436b896e83c10868 Once a relation has data, it’s no more a relation … Read more