Doctrine Extensions
Piotr Woszczyk @ 2020
Informacje podstawowe
- https://github.com/Atlantic18/DoctrineExtensions
- Doctrine >= 2.5
- 2011
- Gediminas Morkevicius
- StofDoctrineExtensionsBundle
Lista rozszerzeń
- Blameable
- Loggable
- Sluggable
- Timestampable
- Translatable
- Tree
ORM:
- IpTraceable
- SoftDeleteable
- Sortable
- Uploadable
ODM:
- References
- ReferenceIntegrity
Instalacja StofDoctrineExtensionsBundle
composer require stof/doctrine-extensions-bundlejeśli wyłączony flex:
// app/AppKernel.php
class AppKernel extends Kernel
{
public function registerBundles()
{
$bundles = array(
// ...
new Stof\DoctrineExtensionsBundle\StofDoctrineExtensionsBundle(),
);
// ...
}
// ...
}Konfiguracja StofDoctrineExtensionsBundle
Włączanie rozszerzeń:
# app/config/config.yml
# (or config/packages/stof_doctrine_extensions.yaml if you use Flex)
stof_doctrine_extensions:
default_locale: en_US
orm:
default:
tree: true
timestampable: false # not needed: listeners are not enabled by default
other:
timestampable: trueKonfiguracja StofDoctrineExtensionsBundle
Ustawienia dla translatable, loggable, tree:
# app/config/config.yml
# (or config/packages/doctrine.yaml if you use Flex)
doctrine:
orm:
entity_managers:
default:
mappings:
gedmo_translatable:
type: annotation
prefix: Gedmo\Translatable\Entity
dir: "%kernel.root_dir%/../vendor/gedmo/doctrine-extensions/lib/Gedmo/Translatable/Entity"
alias: GedmoTranslatable # (optional) it will default to the name set for the mapping
is_bundle: false
gedmo_translator:
type: annotation
prefix: Gedmo\Translator\Entity
dir: "%kernel.root_dir%/../vendor/gedmo/doctrine-extensions/lib/Gedmo/Translator/Entity"
alias: GedmoTranslator # (optional) it will default to the name set for the mapping
is_bundle: false
gedmo_loggable:
type: annotation
prefix: Gedmo\Loggable\Entity
dir: "%kernel.root_dir%/../vendor/gedmo/doctrine-extensions/lib/Gedmo/Loggable/Entity"
alias: GedmoLoggable # (optional) it will default to the name set for the mapping
is_bundle: false
gedmo_tree:
type: annotation
prefix: Gedmo\Tree\Entity
dir: "%kernel.root_dir%/../vendor/gedmo/doctrine-extensions/lib/Gedmo/Tree/Entity"
alias: GedmoTree # (optional) it will default to the name set for the mapping
is_bundle: falseBlameable
- Zapisanie nazwy lub referencji do użytkownika, który ostatnio dokonał zmiany
- Określenie pól jakich zmiana ma powodować zapisanie zmieniającego
- Określenie wartości po jakich osiągnięciu ma być zapisany zmieniający
- Konfigurację przez annotacje, YAML, XML lub trait (BlameableEntity)
Blameable
use Gedmo\Mapping\Annotation as Gedmo;
use Doctrine\ORM\Mapping as ORM;
/**
* @var string $updatedBy
*
* @Gedmo\Blameable(on="update")
* @ORM\Column(type="string")
*/
private $updatedBy;
/**
* @var string $contentChangedBy
*
* @ORM\Column(name="content_changed_by", type="string", nullable=true)
* @Gedmo\Blameable(on="change", field={"title", "body"})
*/
private $contentChangedBy;Blameable
use Gedmo\Mapping\Annotation as Gedmo;
use Doctrine\ORM\Mapping as ORM;
/**
* @var string $contentChangedBy
*
* @ORM\Column(name="content_changed_by", type="string", nullable=true)
* @Gedmo\Blameable(on="change", field="title", value="Dupa")
*/
private $contentChangedBy;Blameable
use Gedmo\Mapping\Annotation as Gedmo;
use Doctrine\ORM\Mapping as ORM;
/**
* @var User $updatedBy
*
* @Gedmo\Blameable(on="update")
* @ORM\ManyToOne(targetEntity="App\Entity\User")
* @ORM\JoinColumn(name="updated_by", referencedColumnName="id")
*/
private $updatedBy;
/**
* @var User $contentChangedBy
*
* @Gedmo\Blameable(on="change", fields={"title", "body"})
* @ORM\ManyToOne(targetEntity="App\Entity\User")
* @ORM\JoinColumn(name="content_changed_by", referencedColumnName="id")
*/
private $contentChangedBy;Blameable
<?php
namespace App\Entity;
use Gedmo\Blameable\Traits\BlameableEntity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
*/
class Article
{
/**
* Hook blameable behavior
* updates createdBy, updatedBy fields
*/
use BlameableEntity;
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(length=128)
*/
private $title;
}Loggable
Automatycznie zapisuje kolejne wersje entity. Umożliwia pobranie lub cofnięcie się do dowolnej wersji.
Loggable
use Gedmo\Mapping\Annotation as Gedmo;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
* @Gedmo\Loggable
*/
class Article
{
}Loggable
<?php
$repo = $em->getRepository('Gedmo\Loggable\Entity\LogEntry');
$article = $em->find('Entity\Article', 1);
$repo->revert($article, 1);
$em->persist($article);
$em->flush();Sluggable
Umożliwia tworzenie dodatkowych pól na podstawie wartości pozostałych. Może zapewniać unikalnośc, być prefixowane i suffixowane.
Sluggable
use Gedmo\Mapping\Annotation as Gedmo;
use Doctrine\ORM\Mapping as ORM;
/**
* @Gedmo\Slug(fields={"title", "code"})
* @ORM\Column(length=128, unique=true)
*/
private $slug;Sluggable
- fields (required, default=[])
- updatable (optional, default=true)
- unique (optional, default=true)
- unique_base (optional, default=null)
- separator (optional, default="-")
- prefix (optional, default="")
- suffix (optional, default="")
- style (optional, default="default")
- handlers (optional, default=[])
Doctrine Extensions
By Piotr Woszczyk
Doctrine Extensions
- 41