PHP code example of ck-developer / doctrine-behaviors
1. Go to this page and download the library: Download ck-developer/doctrine-behaviors 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/ */
ck-developer / doctrine-behaviors example snippets
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' ];
}
public function generateSlugValue($values)
{
return implode('-', $values);
}
}
use Doctrine\ORM\Mapping as ORM;
use Knp\DoctrineBehaviors\Model as ORMBehaviors;
/**
* @ORM\Entity
*/
class BlogPost
{
use ORMBehaviors\Tokenable\Tokenable;
public function getRegenerateTokenOnUpdate()
{
return true;
}
}
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');
}
}
php
$em->getEventManager()->addEventSubscriber(new \Knp\DoctrineBehaviors\ORM\Translatable\TranslatableSubscriber);
// register more if needed
php
$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
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;
}
}
php
$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();
php
public function __call($method, $arguments)
{
return $this->proxyCurrentLocaleTranslation($method, $arguments);
}
php
$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
$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
php
$category = new Category;
$em->persist($category);
// instances of %knp.doctrine_behaviors.blameable_subscriber.user_entity%
$creator = $category->getCreatedBy();
$updater = $category->getUpdatedBy();
php
/**
* @ORM\Entity
*/
class Category
{
use ORMBehaviors\Loggable\Loggable;
// 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!';
}
}
php
$em->getEventManager()->addEventSubscriber(
new \Knp\DoctrineBehaviors\ORM\Loggable\LoggableSubscriber(
new ClassAnalyzer,
function($message) {
// do stuff with message
}
)
);
php
$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);
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.