2017 © Pedro Peláez
 

library doctrine-behaviors

Doctrine2 behavior traits

image

nick4fake/doctrine-behaviors

Doctrine2 behavior traits

  • Thursday, July 30, 2015
  • by nick4fake
  • Repository
  • 3 Watchers
  • 2 Stars
  • 1,456 Installations
  • PHP
  • 2 Dependents
  • 0 Suggesters
  • 131 Forks
  • 0 Open issues
  • 9 Versions
  • 0 % Grown

The README.md

Doctrine2 Behaviors

Build Status, (*1)

This php 5.4+ library is a collection of traits that add behaviors to Doctrine2 entites and repositories., (*2)

It currently handles:, (*3)

Notice:

Some behaviors (translatable, timestampable, softDeletable, blameable, geocodable) need Doctrine subscribers in order to work. Make sure to activate them by reading the Subscribers section., (*4)

, (*5)

Subscribers

If you use symfony2, you can easily register them by importing a service definition file:, (*6)

``` yaml, (*7)

# app/config/config.yml
imports:
    - { resource: ../../vendor/knplabs/doctrine-behaviors/config/orm-services.yml }

You can also register them using doctrine2 api: ``` php <?php $em->getEventManager()->addEventSubscriber(new \Knp\DoctrineBehaviors\ORM\Translatable\TranslatableSubscriber); // register more if needed

Usage

All you have to do is to define a Doctrine2 entity and use traits:, (*8)

``` php, (*9)

<?php, (*10)

use Doctrine\ORM\Mapping as ORM; use Knp\DoctrineBehaviors\Model as ORMBehaviors;, (*11)

/** * @ORM\Entity(repositoryClass="CategoryRepository") */ class Category implements ORMBehaviors\Tree\NodeInterface, \ArrayAccess { use ORMBehaviors\Tree\Node, ORMBehaviors\Translatable\Translatable, ORMBehaviors\Timestampable\Timestampable, ORMBehaviors\SoftDeletable\SoftDeletable, ORMBehaviors\Blameable\Blameable, ORMBehaviors\Geocodable\Geocodable, ORMBehaviors\Loggable\Loggable, ORMBehaviors\Sluggable\Sluggable ;, (*12)

/**
 * @ORM\Id
 * @ORM\Column(type="integer")
 * @ORM\GeneratedValue(strategy="NONE")
 */
protected $id;

}, (*13)



For some behaviors like tree, you can use repository traits: ``` php <?php use Doctrine\ORM\EntityRepository; use Knp\DoctrineBehaviors\ORM as ORMBehaviors; class CategoryRepository extends EntityRepository { use ORMBehaviors\Tree\Tree, }

Voila!, (*14)

You now have a working Category that behaves like:, (*15)

, (*16)

tree:

``` php, (*17)

<?php, (*18)

$category = new Category;
$category->setId(1); // tree nodes need an id to construct path.
$child = new Category;
$child->setId(2);

$child->setChildOf($category);

$em->persist($child);
$em->persist($category);
$em->flush();

$root = $em->getRepository('Category')->getTree();

$root->getParent(); // null
$root->getChildNodes(); // ArrayCollection
$root[0][1]; // node or null
$root->isLeaf(); // boolean
$root->isRoot(); // boolean

<a name="translatable" id="translatable"></a> ### translatable: If you're working on a `Category` entity, the `Translatable` behavior expects a **CategoryTranslation** entity. This naming convention avoids you to handle manually entity associations. It is handled automatically by the TranslationSubscriber. In order to use the Translatable trait, you will have to create this `CategoryTranslation` entity. ``` php <?php use Doctrine\ORM\Mapping as ORM; use Knp\DoctrineBehaviors\Model as ORMBehaviors; /** * @ORM\Entity */ class CategoryTranslation { use ORMBehaviors\Translatable\Translation; /** * @ORM\Column(type="string", length=255) */ protected $name; /** * @ORM\Column(type="string", length=255) */ protected $description; /** * @return string */ public function getName() { return $this->name; } /** * @param string * @return null */ public function setName($name) { $this->name = $name; } /** * @return string */ public function getDescription() { return $this->description; } /** * @param string * @return null */ public function setDescription($description) { $this->description = $description; } }

The corresponding Category entity needs to use ORMBehaviors\Translatable\Translatable; and should only contain fields that you do not need to translate., (*19)

<?php
use Doctrine\ORM\Mapping as ORM;
use Knp\DoctrineBehaviors\Model as ORMBehaviors;

/**
 * @ORM\Entity
 */
class Category
{
    use ORMBehaviors\Translatable\Translatable;

    /**
     * @ORM\Column(type="string", length=255)
     */
    protected $someFieldYouDoNotNeedToTranslate;
}

After updating the database, ie. with ./console doctrine:schema:update --force, you can now work on translations using translate or getTranslations methods., (*20)

``` php, (*21)

<?php, (*22)

$category = new Category;
$category->translate('fr')->setName('Chaussures');
$category->translate('en')->setName('Shoes');
$em->persist($category);

// In order to persist new translations, call mergeNewTranslations method, before flush
$category->mergeNewTranslations();

$category->translate('en')->getName();

#### guess the current locale You can configure the way the subscriber guesses the current locale, by giving a callable as its first argument. This library provides a callable object (`Knp\DoctrineBehaviors\ORM\Translatable\CurrentLocaleCallable`) that returns the current locale using Symfony2. #### proxy translations An extra feature allows you to proxy translated fields of a translatable entity. You can use it in the magic `__call` method of you translatable entity so that when you try to call `getName` (for example) it will return you the translated value of the name for current locale: ``` php <?php public function __call($method, $arguments) { return $this->proxyCurrentLocaleTranslation($method, $arguments); }

, (*23)

soft-deletable

``` php, (*24)

<?php, (*25)

$category = new Category;
$em->persist($category);
$em->flush();

// get id
$id = $category->getId();

// now remove it
$em->remove($category);
$em->flush();

// hey, i'm still here:
$category = $em->getRepository('Category')->findOneById($id);

// but i'm "deleted"
$category->isDeleted(); // === true

``` php <?php $category = new Category; $em->persist($category); $em->flush(); // I'll delete you tomorow $category->setDeletedAt((new \DateTime())->modify('+1 day')); // Ok, I'm here $category->isDeleted(); // === false /* * 24 hours later... */ // Ok I'm deleted $category->isDeleted(); // === true

, (*26)

timestampable

``` php, (*27)

<?php, (*28)

$category = new Category;
$em->persist($category);
$em->flush();

$id = $category->getId();
$category = $em->getRepository('Category')->findOneById($id);

$category->getCreatedAt();
$category->getUpdatedAt();


<a name="blameable" id="blameable"></a> ### blameable Blameable is able to track creators and updators of a given entity. A blameable [callable](#callables) is used to get the current user from your application. In the case you are using a Doctrine Entity to represent your users, you can configure the subscriber to manage automatically the association between this user entity and your entites. Using symfony2, all you have to do is to configure the DI parameter named `%knp.doctrine_behaviors.blameable_subscriber.user_entity%` with a fully qualified namespace, for example: # app/config/config.yml parameters: knp.doctrine_behaviors.blameable_subscriber.user_entity: AppBundle\Entity\User Then, you can use it like that: ``` php <?php $category = new Category; $em->persist($category); // instances of %knp.doctrine_behaviors.blameable_subscriber.user_entity% $creator = $em->getCreatedBy(); $updater = $em->getUpdatedBy();

, (*29)

loggable

Loggable is able to track lifecycle modifications and log them using any third party log system. A loggable callable is used to get the logger from anywhere you want., (*30)

``` php, (*31)

<?php, (*32)

/** * @ORM\Entity */ class Category { use ORMBehaviors\Loggable\Loggable;, (*33)

// you can override the default log messages defined in trait:
public function getUpdateLogMessage(array $changeSets = [])
{
    return 'Changed: '.print_r($changeSets, true);
}

public function getRemoveLogMessage()
{
    return 'removed!';
}

}, (*34)


These messages are then passed to the configured callable. You can define your own, by passing another callable to the LoggableSubscriber: ``` php <?php $em->getEventManager()->addEventSubscriber( new \Knp\DoctrineBehaviors\ORM\Loggable\LoggableSubscriber( new ClassAnalyzer, function($message) { // do stuff with message } ) );

If you're using symfony, you can also configure which callable to use:, (*35)

// app/config/config.yml
parameters:
    knp.doctrine_behaviors.loggable_subscriber.logger_callable.class: Your\InvokableClass

, (*36)

geocodable

Geocodable Provides extensions to PostgreSQL platform in order to work with cube and earthdistance extensions., (*37)

It allows you to query entities based on geographical coordinates. It also provides an easy entry point to use 3rd party libraries like the exellent geocoder to transform addresses into latitude and longitude., (*38)

``` php, (*39)

<?php, (*40)

$geocoder = new \Geocoder\Geocoder;
// register geocoder providers

// $subscriber instanceof GeocodableSubscriber (add "knp.doctrine_behaviors.geocodable_subscriber" into your services.yml)
$subscriber->setGeolocationCallable(function($entity) use($geocoder) {
    $location = $geocoder->geocode($entity->getAddress());
    return new Point(
        $location->getLatitude(),
        $location->getLongitude()
    ));
});

$category = new Category;
$em->persist($category);

$location = $category->getLocation(); // instanceof Point

// find cities in a cricle of 500 km around point 47 lon., 7 lat.
$nearCities = $repository->findByDistance(new Point(47, 7), 500);

<a name="sluggable" id="sluggable"></a> ### sluggable Sluggable generates slugs (uniqueness is not guaranteed) for an entity. Will automatically generate on update/persist (you can disable the on update generation by overriding `getRegenerateSlugOnUpdate` to return false. You can also override the slug delimiter from the default hyphen by overriding `getSlugDelimiter`. Use cases include SEO (i.e. URLs like http://mysite.com/post/3/introduction-to-php) ```php <?php use Doctrine\ORM\Mapping as ORM; use Knp\DoctrineBehaviors\Model as ORMBehaviors; /** * @ORM\Entity */ class BlogPost { use ORMBehaviors\Sluggable\Sluggable; /** * @ORM\Column(type="string") */ protected $title; public function getSluggableFields() { return [ 'title' ]; } }

, (*41)

filterable:

Filterable can be used at the Repository level, (*42)

It allows to simple filter our result, (*43)

Joined filters example:, (*44)

<?php

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity(repositoryClass="ProductRepository")
 */
class ProductEntity
{

    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @ORM\Column(type="string", nullable=true)
     */
    private $name;

    /**
     * @ORM\Column(type="integer")
     */
    private $code;

    /**
     * @ORM\OneToMany(targetEntity="Order", mappedBy="product")
     */
    protected $orders;
}

and repository:, (*45)

<?php

use Knp\DoctrineBehaviors\ORM\Filterable;
use Doctrine\ORM\EntityRepository;

class ProductRepository extends EntityRepository
{
    use Filterable\FilterableRepository;

    public function getLikeFilterColumns()
    {
        return ['e:name', 'o:code'];
    }

    public function getEqualFilterColumns()
    {
        return [];
    }

    protected function createFilterQueryBuilder()
    {
        return $this
            ->createQueryBuilder('e')
            ->leftJoin('e.orders', 'o');
    }
}

Now we can filtering using:, (*46)

    $products = $em->getRepository('Product')->filterBy(['o:code' => '21']);

, (*47)

callables

Callables are used by some subscribers like blameable and geocodable to fill information based on 3rd party system., (*48)

For example, the blameable callable can be any symfony2 service that implements __invoke method or any anonymous function, as soon as they return currently logged in user representation (which means everything, a User entity, a string, a username, ...). For an example of DI service that is invoked, look at the Knp\DoctrineBehaviors\ORM\Blameable\UserCallable class., (*49)

In the case of geocodable, you can set it as any service that implements __invoke or anonymous function that returns a Knp\DoctrineBehaviors\ORM\Geocodable\Type\Point object., (*50)

The Versions

30/07 2015

dev-master

9999999-dev http://knplabs.com

Doctrine2 behavior traits

  Sources   Download

MIT

The Requires

 

The Development Requires

filterable doctrine2 translatable tree behaviors timestampable blameable softdeletable

25/09 2014

dev-fix/bc-layer

dev-fix/bc-layer http://knplabs.com

Doctrine2 behavior traits

  Sources   Download

MIT

The Requires

 

The Development Requires

filterable doctrine2 translatable tree behaviors timestampable blameable softdeletable

17/09 2014

v1.0.2

1.0.2.0 http://knplabs.com

Doctrine2 behavior traits

  Sources   Download

MIT

The Requires

 

The Development Requires

filterable doctrine2 translatable tree behaviors timestampable blameable softdeletable

23/08 2014

dev-fix/tests

dev-fix/tests http://knplabs.com

Doctrine2 behavior traits

  Sources   Download

MIT

The Requires

 

The Development Requires

filterable doctrine2 translatable tree behaviors timestampable blameable softdeletable

22/01 2014

v1.0.1

1.0.1.0 http://knplabs.com

Doctrine2 behavior traits

  Sources   Download

MIT

The Requires

 

The Development Requires

filterable doctrine2 translatable tree behaviors timestampable blameable softdeletable

25/06 2013

dev-trsteel88/master

dev-trsteel88/master http://knplabs.com

Doctrine2 behavior traits

  Sources   Download

MIT

The Requires

 

The Development Requires

filterable doctrine2 translatable tree behaviors timestampable blameable softdeletable

17/06 2013

v1.0.0

1.0.0.0 http://knplabs.com

Doctrine2 behavior traits

  Sources   Download

MIT

The Requires

 

The Development Requires

filterable doctrine2 translatable tree behaviors timestampable blameable softdeletable

26/02 2013

dev-Feature/TraitsMethodsRenaming

dev-Feature/TraitsMethodsRenaming http://knplabs.com

Doctrine2 behavior traits

  Sources   Download

MIT

The Requires

 

The Development Requires

filterable doctrine2 translatable tree behaviors timestampable blameable softdeletable

05/12 2012

dev-odm

dev-odm http://knplabs.com

Doctrine2 behavior traits

  Sources   Download

MIT

The Requires

 

The Development Requires

filterable doctrine2 translatable tree behaviors timestampable blameable softdeletable