Git Product home page Git Product logo

awesome-doctrine's Introduction

Awesome Doctrine

A curated list of useful Doctrine snippets.

Contributions are highly encouraged and very welcome :)

Table of Contents

DQL

Defining a Column to be the Key of the Result Hydrated as Array

$em = $this->getEntityManager();

$query = $em->createQuery('SELECT c FROM SomeBundle:Configuration c INDEX BY c.name');
$query->getResult(\Doctrine\ORM\Query::HYDRATE_ARRAY);

[1]

Fetch Only Parts of an Entity

SELECT partial b{id, title} FROM Book b

INDEX BY in QueryBuilder

$qb = $em->createQueryBuilder();

$qb->select('u')
   ->from('SomeUserBundle:User', 'u', 'u.id')
   ->add('where', $qb->expr()->like('u.roles', ':role'))
   ->setParameter('role', $role);

Get Single Row or Null

$query->getOneOrNullResult();
  • no result: return null
  • more than one result: throw an NonUniqueResultException exception

[1] [2]

Ordering with Expressions

SELECT m, (m.comments + m.likes_count) AS HIDDEN score FROM Midia m ORDER BY score

Return Only a Value

$query = $entityManager->createQuery('SELECT COUNT(u.id) FROM User u');
$count = $query->getSingleScalarResult();

WHERE IN Clause

Doctrine 2.4

$categories = ... 
$categoryIds = array();

foreach ($categories as $category) {
    $categoryIds[] = $category->getId();
} 
$queryBuilder = $this
    ->where('model.category IN (:category_ids)')
    ->setParameter('category_ids', $categoryIds);

Doctrine 2.5+ supports ArrayCollection

$queryBuilder = $this
    ->where('model.category IN (:categories)') 
    ->setParameter('categories', $categories);

Performance

Temporarily Mark Entities as Read-Only at Runtime

If you have a very large UnitOfWork but know that a large set of entities has not changed, just mark them as read only.

$entityManager->getUnitOfWork()->markReadOnly($entity)

[1]

awesome-doctrine's People

Contributors

andreia avatar toooni avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.