All Projects → andreia → Awesome Doctrine

andreia / Awesome Doctrine

A collection of useful Doctrine snippets.

Projects that are alternatives of or similar to Awesome Doctrine

Awesome Symfony
A collection of useful Symfony snippets.
Stars: ✭ 360 (+144.9%)
Mutual labels:  snippets, collection, curated-list
Doctrinejsonfunctions
Doctrine DQL functions for SQL JSON data type
Stars: ✭ 325 (+121.09%)
Mutual labels:  orm, doctrine
Sample Programs
Sample Programs in Every Programming Language
Stars: ✭ 323 (+119.73%)
Mutual labels:  snippets, collection
Cool Fashion Papers
👔👗🕶️🎩 Cool resources about Fashion + AI! (papers, datasets, workshops, companies, ...) (constantly updating)
Stars: ✭ 464 (+215.65%)
Mutual labels:  collection, curated-list
whatdevsneed
Discover new developer tools 🧰
Stars: ✭ 48 (-67.35%)
Mutual labels:  collection, curated-list
doctrine-json-odm
JSON Object-Document Mapping bundle for Symfony and Doctrine
Stars: ✭ 15 (-89.8%)
Mutual labels:  orm, doctrine
Sonatadoctrineormadminbundle
Integrate Doctrine ORM into the SonataAdminBundle
Stars: ✭ 400 (+172.11%)
Mutual labels:  orm, doctrine
Awesome Virtual Try On
A curated list of awesome research papers, projects, code, dataset, workshops etc. related to virtual try-on.
Stars: ✭ 175 (+19.05%)
Mutual labels:  collection, curated-list
Orm
A drop-in Doctrine ORM 2 implementation for Laravel 5+ and Lumen
Stars: ✭ 712 (+384.35%)
Mutual labels:  orm, doctrine
Doctrinebehaviors
Doctrine2 behavior traits
Stars: ✭ 782 (+431.97%)
Mutual labels:  orm, doctrine
Factory Bot
🤖 Provides a fixture factory for doctrine/orm entities.
Stars: ✭ 57 (-61.22%)
Mutual labels:  orm, doctrine
links-uteis
📎 A curated list of awesome project development links
Stars: ✭ 2,547 (+1632.65%)
Mutual labels:  collection, curated-list
Orm Pack
A Symfony Pack for Doctrine ORM
Stars: ✭ 1,850 (+1158.5%)
Mutual labels:  orm, doctrine
demos
Demonstrative scripts
Stars: ✭ 37 (-74.83%)
Mutual labels:  snippets, collection
Arbitrary Text To Image Papers
A collection of arbitrary text to image papers with code (constantly updating)
Stars: ✭ 196 (+33.33%)
Mutual labels:  collection, curated-list
Dtcqueuebundle
Symfony2/3/4/5 Queue Bundle (for background jobs) supporting Mongo (Doctrine ODM), Mysql (and any Doctrine ORM), RabbitMQ, Beanstalkd, Redis, and ... {write your own}
Stars: ✭ 115 (-21.77%)
Mutual labels:  orm, doctrine
30 Seconds Of Swift Code
A Swift implementation of 30-seconds-of-code: A curated collection of useful Swift 4 snippets that you can understand in 30 seconds or less.
Stars: ✭ 476 (+223.81%)
Mutual labels:  snippets, collection
Movies For Hackers
🎬 A curated list of movies every hacker & cyberpunk must watch.
Stars: ✭ 8,884 (+5943.54%)
Mutual labels:  collection, curated-list
Awesome Maps
There is more than google: A collection of great online maps 🌍🗺🌎
Stars: ✭ 124 (-15.65%)
Mutual labels:  collection, curated-list
Gosql
golang orm and sql builder
Stars: ✭ 141 (-4.08%)
Mutual labels:  orm

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

IN Clause in Raw SQL

$stmt = $this->getDoctrine()->getEntityManager()
    ->getConnection()
    ->prepare('SELECT t.id, t.name
        FROM table t
        WHERE t.id IN (:ids)');

$stmt->bindValue('ids', array(1, 2, 3, 4, 5, 6), \Doctrine\DBAL\Connection::PARAM_INT_ARRAY);
$stmt->execute();

or

$stmt = $this->getDoctrine()->getEntityManager()
    ->getConnection()
    ->executeQuery('SELECT t.id, t.name
        FROM table t
        WHERE t.id IN (:ids)',
        array('ids' => array(1, 2, 3, 4, 5, 6)),
        array('ids' => \Doctrine\DBAL\Connection::PARAM_INT_ARRAY)
    )
;

[1]

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

Select Directly by Foreign Key Without Join the Foreign Table

$q = $rep->createQueryBuilder('t')
    ->where('IDENTITY(t.user) = :userId')
    ->orderBy('t.id', 'DESC')
    ->setParameter('userId', $id)
    ->getQuery();

or

SELECT p FROM Product p WHERE IDENTITY(p.shop) = :shopId

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

Bulk Update Using 'update' Statement Instead of Iterating Through Entities - Object Persisting

$em = $this->getDoctrine()->getManager();
$repo = $em->getRepository('AppBundle:User');
$active = true;

$qb = $repo->createQueryBuilder('u');
    $qb->update()
        ->set('u.active', ':userActive')
        ->setParameter('userActive', $active);

$qb->getQuery()->execute();

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)

Raw SQL - DBAL

Update, Insert

$count = $conn->executeUpdate('UPDATE user SET username = ? WHERE id = ?', array('andreia', 1));
echo $count; // 1

Query Data

Select with Parameters

$sql = "SELECT * FROM site WHERE id = ?";
$stmt = $conn->prepare($sql);
$stmt->bindValue(1, $id);
$stmt->execute();
$sites = $stmt->fetchAll();

Select with Named Parameters

$sql = "SELECT * FROM user WHERE name = :name";
$stmt = $conn->prepare($sql);
$stmt->bindValue("name", $name);
$stmt->execute();
$users = $stmt->fetchArray();

Transaction

use Doctrine\DBAL\Connection;

class SomeClass
{
    private $conn;

    // ...

    public function __construct(Connection $conn)
    {
        $this->conn = $conn;
    }

    // ...

    function updateDatabase()
    {
        // ...
        
        try {
            $this->conn->beginTransaction(); 
            $this->conn->setAutoCommit(false);

            $this->conn->executeUpdate('INSERT INTO table1 (field1, field2, field3) VALUES(?, ?, ?)', [$field1, $field2, $field3]);
            $this->conn->executeUpdate('INSERT INTO table2 (field1, field2) VALUES(?, ?)', [$field1, $field2]);

            $this->conn->commit();
        } catch (\Exception $e) {
            // ...

            $this->conn->rollback();
        }
    }

    // ...
}

Truncate Table

$platform = $this->conn->getDatabasePlatform();
$this->conn->executeQuery('SET FOREIGN_KEY_CHECKS = 0;');
$truncateSql = $platform->getTruncateTableSQL('table_name');
$this->conn->executeUpdate($truncateSql);
$this->conn->executeQuery('SET FOREIGN_KEY_CHECKS = 1;');

[1]

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].