Tag Archives: Doctrine2

Caching Data in Symfony2

It is not too easy to enhance the performance of your application. In Symfony2 you could get benefit from caching. The most effective way to improve performance of an application is to cache the full output of a page and then bypass the application entirely on each subsequent request. The cache is the “middle-man” of the request-response communication between the client and your application. Caching data in Symfony2 with doctrine cache bundle can be implemented as described below.

Installing this bundle can be done through these simple steps:

1. Add this bundle to your project as a composer dependency:


// composer.json
{
    // ...
    require: {
        // ...
        "doctrine/doctrine-cache-bundle": "~1.0"
    }
}

2. Add this bundle in your application kernel:


// application/ApplicationKernel.php
public function registerBundles()
{
    // ...
    $bundles[] = new \Doctrine\Bundle\DoctrineCacheBundle\DoctrineCacheBundle();

    return $bundles;
}

Usage:

Simply use doctrine_cache.providers.{provider_name} to inject it into the desired service.

Check the following sample:


$apcCache = $this->container->get('doctrine_cache.providers.my_apc_cache');
$arrayCache = $this->container->get('doctrine_cache.providers.my_array_cache');

Usage Example for MongoDb:

In Your Config File


// config.yml

doctrine_cache:
    aliases:
        cache_categories: classifiad_category_cache
    providers:
        classifiad_category_cache:
            mongodb:
                server: mongodb://%mongodb_host%:%mongodb_port%
                database_name: %mongodb_database%

In Your Controller


// Saving Cached Data
$CategoryCacheProvider = $this->container->get('doctrine_cache.providers.classifiad_category_cache');
$CategoryCacheProvider->save('cache_categories', $categories, 60 * 60);

// Fetching Cached Data
$CategoryCacheProvider = $this->container->get('doctrine_cache.providers.classifiad_category_cache');
if ($CategoryCacheProvider->fetch('cache_categories')) {
    $categories = $CategoryCacheProvider->fetch('cache_categories');
}

Doctrine 2 Single Class Inheritance (SCI)

Single Class Inheritance is an inheritance mapping strategy where all classes of a hierarchy are mapped to a single database table. In order to distinguish which row represents which type in the hierarchy a so-called discriminator column is used.

Things to note:

  • The @InheritanceType, @DiscriminatorColumn and @DiscriminatorMap must be specified on the topmost class that is part of the mapped entity hierarchy.
  • The names of the classes in the discriminator map do not need to be fully qualified if the classes are contained in the same namespace as the entity class on which the discriminator map is applied.

<?php

namespace Entities;

/**
* @Entity
* @Table(name=”pets”)
* @InheritanceType(“SINGLE_TABLE”)
* @DiscriminatorColumn(name=”pet_type”, type=”string”)
* @DiscriminatorMap({“cat” = “Cat”, “dog” = “Dog”})
*/
class Pet
{
/** @Id @Column(type=”integer”) @generatedValue */
private $id;

/** @Column(type=”string”, length=300) */
private $name;

/** @ManyToOne(targetEntity=”User”, inversedBy=”id”) */
private $owner;
}

/** @Entity */
class Dog extends Pet
{
/** @Column(type=”string”, length=50) */
private $kennels;
}

/** @Entity */
class Cat extends Pet
{
/** @Column(type=”string”, length=50) */
private $cattery;
}

/**
* @Entity
* @Table(name=”users”)
*/
class User
{
/** @Id @Column(type=”integer”) @generatedValue */
private $id;

/** @Column(length=255, nullable=false) */
private $name;

/** @OneToMany(targetEntity=”Pet”, mappedBy=”owner”) */
private $pets;
}