Piotr Woszczyk @ 2020
ORM:
ODM:
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(),
);
// ...
}
// ...
}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: trueUstawienia 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: falseuse 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;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;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;<?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;
}Automatycznie zapisuje kolejne wersje entity. Umożliwia pobranie lub cofnięcie się do dowolnej wersji.
use Gedmo\Mapping\Annotation as Gedmo;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
* @Gedmo\Loggable
*/
class Article
{
}<?php
$repo = $em->getRepository('Gedmo\Loggable\Entity\LogEntry');
$article = $em->find('Entity\Article', 1);
$repo->revert($article, 1);
$em->persist($article);
$em->flush();Umożliwia tworzenie dodatkowych pól na podstawie wartości pozostałych. Może zapewniać unikalnośc, być prefixowane i suffixowane.
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;