forked from doctrine-extensions/DoctrineExtensions
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[example] created an example on initialization of entity manager with…
… extensions
- Loading branch information
Showing
7 changed files
with
433 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,184 @@ | ||
<?php | ||
namespace Entity; | ||
|
||
use Doctrine\Common\Collections\ArrayCollection; | ||
use Gedmo\Mapping\Annotation as Gedmo; | ||
use Doctrine\ORM\Mapping as ORM; | ||
|
||
/** | ||
* @Gedmo\Tree(type="nested") | ||
* @ORM\Table(name="ext_categories") | ||
* @ORM\Entity(repositoryClass="Entity\Repository\CategoryRepository") | ||
*/ | ||
class Category | ||
{ | ||
/** | ||
* @ORM\Column(type="integer") | ||
* @ORM\Id | ||
* @ORM\GeneratedValue | ||
*/ | ||
private $id; | ||
|
||
/** | ||
* @Gedmo\Translatable | ||
* @Gedmo\Slug(fields={"title"}) | ||
* @ORM\Column(length=64, unique=true) | ||
*/ | ||
private $slug; | ||
|
||
/** | ||
* @Gedmo\Translatable | ||
* @ORM\Column(length=64) | ||
*/ | ||
private $title; | ||
|
||
/** | ||
* @Gedmo\TreeLeft | ||
* @ORM\Column(type="integer") | ||
*/ | ||
private $lft; | ||
|
||
/** | ||
* @Gedmo\TreeRight | ||
* @ORM\Column(type="integer") | ||
*/ | ||
private $rgt; | ||
|
||
/** | ||
* @Gedmo\TreeParent | ||
* @ORM\ManyToOne(targetEntity="Category", inversedBy="children") | ||
* @ORM\JoinColumn(name="parent_id", referencedColumnName="id", onDelete="SET NULL") | ||
*/ | ||
private $parent; | ||
|
||
/** | ||
* @Gedmo\TreeRoot | ||
* @ORM\Column(type="integer", nullable=true) | ||
*/ | ||
private $root; | ||
|
||
/** | ||
* @Gedmo\TreeLevel | ||
* @ORM\Column(name="lvl", type="integer") | ||
*/ | ||
private $level; | ||
|
||
/** | ||
* @ORM\OneToMany(targetEntity="Category", mappedBy="parent") | ||
*/ | ||
private $children; | ||
|
||
/** | ||
* @Gedmo\Translatable | ||
* @ORM\Column(type="text", nullable=true) | ||
*/ | ||
private $description; | ||
|
||
/** | ||
* @Gedmo\Timestampable(on="create") | ||
* @ORM\Column(type="datetime") | ||
*/ | ||
private $created; | ||
|
||
/** | ||
* @Gedmo\Timestampable(on="update") | ||
* @ORM\Column(type="datetime") | ||
*/ | ||
private $updated; | ||
|
||
/** | ||
* Used locale to override Translation listener`s locale | ||
* @Gedmo\Locale | ||
*/ | ||
private $locale; | ||
|
||
public function __construct() | ||
{ | ||
$this->children = new ArrayCollection(); | ||
} | ||
|
||
public function getSlug() | ||
{ | ||
return $this->slug; | ||
} | ||
|
||
public function getId() | ||
{ | ||
return $this->id; | ||
} | ||
|
||
public function setTitle($title) | ||
{ | ||
$this->title = $title; | ||
} | ||
|
||
public function getTitle() | ||
{ | ||
return $this->title; | ||
} | ||
|
||
public function setDescription($description) | ||
{ | ||
$this->description = strip_tags($description); | ||
} | ||
|
||
public function getDescription() | ||
{ | ||
return $this->description; | ||
} | ||
|
||
public function setParent($parent) | ||
{ | ||
$this->parent = $parent; | ||
} | ||
|
||
public function getParent() | ||
{ | ||
return $this->parent; | ||
} | ||
|
||
public function getRoot() | ||
{ | ||
return $this->root; | ||
} | ||
|
||
public function getLevel() | ||
{ | ||
return $this->level; | ||
} | ||
|
||
public function getChildren() | ||
{ | ||
return $this->children; | ||
} | ||
|
||
public function getLeft() | ||
{ | ||
return $this->lft; | ||
} | ||
|
||
public function getRight() | ||
{ | ||
return $this->rgt; | ||
} | ||
|
||
public function getCreated() | ||
{ | ||
return $this->created; | ||
} | ||
|
||
public function getUpdated() | ||
{ | ||
return $this->updated; | ||
} | ||
|
||
public function setTranslatableLocale($locale) | ||
{ | ||
$this->locale = $locale; | ||
} | ||
|
||
public function __toString() | ||
{ | ||
return $this->getTitle(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
namespace Entity\Repository; | ||
|
||
use Gedmo\Tree\Entity\Repository\NestedTreeRepository; | ||
|
||
class CategoryRepository extends NestedTreeRepository | ||
{ | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
#!/usr/bin/env php | ||
<?php | ||
|
||
$cli = include __DIR__.'/console.php'; | ||
$cli->run(); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php | ||
|
||
$em = include __DIR__.'/../em.php'; | ||
|
||
$cli = new Symfony\Component\Console\Application('My CLI interface', '1.0.0'); | ||
$cli->setCatchExceptions(true); | ||
// commands | ||
$cli->addCommands(array( | ||
// DBAL Commands | ||
new Doctrine\DBAL\Tools\Console\Command\RunSqlCommand(), | ||
new Doctrine\DBAL\Tools\Console\Command\ImportCommand(), | ||
|
||
// ORM Commands | ||
new Doctrine\ORM\Tools\Console\Command\ClearCache\MetadataCommand(), | ||
new Doctrine\ORM\Tools\Console\Command\ClearCache\ResultCommand(), | ||
new Doctrine\ORM\Tools\Console\Command\ClearCache\QueryCommand(), | ||
new Doctrine\ORM\Tools\Console\Command\SchemaTool\CreateCommand(), | ||
new Doctrine\ORM\Tools\Console\Command\SchemaTool\UpdateCommand(), | ||
new Doctrine\ORM\Tools\Console\Command\SchemaTool\DropCommand(), | ||
new Doctrine\ORM\Tools\Console\Command\EnsureProductionSettingsCommand(), | ||
new Doctrine\ORM\Tools\Console\Command\ConvertDoctrine1SchemaCommand(), | ||
new Doctrine\ORM\Tools\Console\Command\GenerateRepositoriesCommand(), | ||
new Doctrine\ORM\Tools\Console\Command\GenerateEntitiesCommand(), | ||
new Doctrine\ORM\Tools\Console\Command\GenerateProxiesCommand(), | ||
new Doctrine\ORM\Tools\Console\Command\ConvertMappingCommand(), | ||
new Doctrine\ORM\Tools\Console\Command\RunDqlCommand(), | ||
new Doctrine\ORM\Tools\Console\Command\ValidateSchemaCommand(), | ||
)); | ||
// helpers | ||
$helpers = array( | ||
'db' => new Doctrine\DBAL\Tools\Console\Helper\ConnectionHelper($em->getConnection()), | ||
'em' => new Doctrine\ORM\Tools\Console\Helper\EntityManagerHelper($em), | ||
'dialog' => new Symfony\Component\Console\Helper\DialogHelper(), | ||
); | ||
foreach ($helpers as $name => $helper) { | ||
$cli->getHelperSet()->set($helper, $name); | ||
} | ||
|
||
return $cli; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
<?php | ||
// connection args, modify at will | ||
$connection = array( | ||
'host' => '127.0.0.1', | ||
'port' => 3306, | ||
'user' => 'root', | ||
'password' => 'nimda', | ||
'dbname' => 'test', | ||
'driver' => 'pdo_mysql' | ||
); | ||
|
||
// First of all autoloading of vendors | ||
|
||
$vendorPath = realpath(__DIR__.'/../vendor'); | ||
$gedmoPath = realpath(__DIR__.'/../lib'); | ||
|
||
$doctrineClassLoaderFile = $vendorPath.'/doctrine-common/lib/Doctrine/Common/ClassLoader.php'; | ||
if (!file_exists($doctrineClassLoaderFile)) { | ||
die('cannot find vendor, run: php bin/vendors.php to install doctrine'); | ||
} | ||
|
||
require $doctrineClassLoaderFile; | ||
use Doctrine\Common\ClassLoader; | ||
// autoload all vendors | ||
|
||
$loader = new ClassLoader('Doctrine\Common', $vendorPath.'/doctrine-common/lib'); | ||
$loader->register(); | ||
|
||
$loader = new ClassLoader('Doctrine\DBAL', $vendorPath.'/doctrine-dbal/lib'); | ||
$loader->register(); | ||
|
||
$loader = new ClassLoader('Doctrine\ORM', $vendorPath.'/doctrine-orm/lib'); | ||
$loader->register(); | ||
|
||
// gedmo extensions | ||
$loader = new ClassLoader('Gedmo', $gedmoPath); | ||
$loader->register(); | ||
|
||
// if you use yaml, you need a yaml parser, same as command line tool | ||
$loader = new ClassLoader('Symfony', $vendorPath); | ||
$loader->register(); | ||
|
||
// autoloader for Entity namespace | ||
$loader = new ClassLoader('Entity', __DIR__.'/app'); | ||
$loader->register(); | ||
// Second configure ORM | ||
|
||
$config = new Doctrine\ORM\Configuration; | ||
$config->setProxyDir(sys_get_temp_dir()); | ||
$config->setProxyNamespace('Proxy'); | ||
$config->setAutoGenerateProxyClasses(false); | ||
|
||
// standard annotation reader | ||
$annotationReader = new Doctrine\Common\Annotations\AnnotationReader; | ||
// gedmo annotation loader | ||
Doctrine\Common\Annotations\AnnotationRegistry::registerAutoloadNamespace( | ||
'Gedmo\Mapping\Annotation', | ||
$gedmoPath | ||
); | ||
// standard doctrine annotations | ||
Doctrine\Common\Annotations\AnnotationRegistry::registerFile( | ||
$vendorPath.'/doctrine-orm/lib/Doctrine/ORM/Mapping/Driver/DoctrineAnnotations.php' | ||
); | ||
// register annotation driver | ||
$driverChain = new Doctrine\ORM\Mapping\Driver\DriverChain(); | ||
$annotationDriver = new Doctrine\ORM\Mapping\Driver\AnnotationDriver($annotationReader, array( | ||
__DIR__.'/app/Entity', // example entity | ||
$gedmoPath.'/Gedmo/Translatable/Entity', | ||
$gedmoPath.'/Gedmo/Loggable/Entity', | ||
$gedmoPath.'/Gedmo/Tree/Entity', | ||
)); | ||
|
||
// drivers for diferent namespaces | ||
$driverChain->addDriver($annotationDriver, 'Entity'); | ||
$driverChain->addDriver($annotationDriver, 'Gedmo\Translatable\Entity'); | ||
$driverChain->addDriver($annotationDriver, 'Gedmo\Loggable\Entity'); | ||
$driverChain->addDriver($annotationDriver, 'Gedmo\Tree\Entity'); | ||
|
||
// register metadata driver | ||
$config->setMetadataDriverImpl($driverChain); | ||
// cache | ||
$config->setMetadataCacheImpl(new Doctrine\Common\Cache\ArrayCache); | ||
$config->setQueryCacheImpl(new Doctrine\Common\Cache\ArrayCache); | ||
|
||
$evm = new Doctrine\Common\EventManager(); | ||
// gedmo extension listeners | ||
$evm->addEventSubscriber(new Gedmo\Sluggable\SluggableListener); | ||
$evm->addEventSubscriber(new Gedmo\Tree\TreeListener); | ||
$evm->addEventSubscriber(new Gedmo\Loggable\LoggableListener); | ||
$evm->addEventSubscriber(new Gedmo\Timestampable\TimestampableListener); | ||
$translatable = new Gedmo\Translatable\TranslationListener; | ||
$translatable->setTranslatableLocale('en'); | ||
$translatable->setDefaultLocale('en'); | ||
$evm->addEventSubscriber($translatable); | ||
|
||
// mysql set names UTF-8 | ||
$evm->addEventSubscriber(new Doctrine\DBAL\Event\Listeners\MysqlSessionInit()); | ||
// create entity manager | ||
return Doctrine\ORM\EntityManager::create($connection, $config, $evm); |
Oops, something went wrong.