So I have some entities which uses my SluggableTrait
, which contains name
and slug
fields along it's methods.
trait SluggableTrait
{
/**
* @var string
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $name;
/**
* @var string
* @ORM\Column(type="string", length=255
* @Gedmo\Slug(fields={"name"}, updatable=false))
*/
private $slug;
/**
* Set name
*
* @param string $name
*
* @return SluggableTrait
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* Set slug
*
* @param string $slug
*
* @return SluggableTrait
*/
public function setSlug($slug)
{
$this->slug = $slug;
return $this;
}
/**
* Get slug
*
* @return string
*/
public function getSlug()
{
return $this->slug;
}
/**
* @return string
*/
public function __toString()
{
return $this->getName() ? $this->getName() : 'n/a';
}
public function getClassName()
{
$reflect = new \ReflectionClass($this);
return $reflect->getShortName();
}
}
But whenever I try to use it in a YAML mapped entity, database won't create it's fields...
Let's say I have Foo
class
class Foo
{
use SluggableTrait;
/**
* @var integer
*/
private $id;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
}
With the YAML mapping
Utils\DoctrineBundle\Entity\Foo:
type: entity
id:
id:
type: integer
generator:
strategy: AUTO
When updating my schema (even creating a new empty database) my foo
table will only have id
field, name
and slug
doesn't exists.
Everything works fine if I use annotation in both items, the Trait and the Class. If mixed mapping, doesn't work.
When using YAML mapping in my trait, just to give it a try I get this error whenever I try to update my schema
[2015-12-07 13:20:33] app.ERROR: Doctrine\Common\Persistence\Mapping\MappingException: Class 'Utils\DoctrineBundle\Entity\SluggableTrait' does not exist (uncaught exception) at /srv/myapp/vendor/doctrine/common/lib/Doctrine/Common/Persistence/Mapping/MappingException.php line 96 while running console command
doctrine:schema:update
{"exception":"[object] (Doctrine\Common\Persistence\Mapping\MappingException(code: 0): Class 'Utils\DoctrineBundle\Entity\SluggableTrait' does not exist at /srv/myapp/vendor/doctrine/common/lib/Doctrine/Common/Persistence/Mapping/MappingException.php:96)"}[Doctrine\Common\Persistence\Mapping\MappingException]
Class 'Utils\DoctrineBundle\Entity\SluggableTrait' does not exist
doctrine:schema:update [--complete] [--dump-sql] [-f|--force] [--em [EM]]
I've tried to search for a Trait YAML example without success... So I'm stuck right now.
Well, after some tests, Doctrine doesn't allow you to mix YAML and ANOTTATION without redeclaring properties which obviously is what we want to avoid using a Trait (and extending a Class)
I've created a PR in order to follow this https://github.com/doctrine/common/pull/701
This PR allows you to use YAML mapped traits, but doesn't trigger some events so it needs to go deeper.
Short answer:
You can't mix YAML and Annotation mappings and Traits doesn't work with YAML mappings.