PHP code example of 68publishers / doctrine-sluggable
1. Go to this page and download the library: Download 68publishers/doctrine-sluggable library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
68publishers / doctrine-sluggable example snippets
/** @var Doctrine\ORM\EntityManager $em */
/** @var Doctrine\Common\Annotations\Reader $reader */
$subscriber = new SixtyEightPublishers\DoctrineSluggable\SluggableEventSubscriber(
new SixtyEightPublishers\DoctrineSluggable\DefinitionStorage\AnnotationSluggableDefinitionStorage($reader)
);
$em->getEventManager()->addEventSubscriber($subscriber);
use Doctrine\ORM\Mapping as ORM;
use SixtyEightPublishers\DoctrineSluggable\Annotation as Sluggable;
/**
* @ORM\Entity
*/
class Product {
/**
* @ORM\Column(type="string", length=255)
*
* @var string
*/
private $name;
/**
* @ORM\ManyToOne(targetEntity="Category")
*
* @var Category
*/
private $category;
/**
* @Sluggable\Slug(
* strategy="SixtyEightPublishers\DoctrineSluggable\Strategy\GenerateOnInsertStrategy",
* strategyOptions={
* "fields": {"name"},
* "checkOnUpdate": true
* },
* finder="SixtyEightPublishers\DoctrineSluggable\Finder\FieldBasedSimilarSlugFinder",
* finderOptions={
* "field": "category",
* "type": "outer"
* },
* transliterator="SixtyEightPublishers\DoctrineSluggable\Transliterator\CamelCaseTransliterator"
* )
* @ORM\Column(type="string", length=255)
*
* @var string
*/
private $slug;
/**
* @return string
*/
public function getSlug() : string
{
# slug is generated on EntityManager's onFlush event
if (NULL === $this->slug) {
throw new RuntimeException('Slug is not set.');
}
return $this->slug;
}
}