Doctrine prefix embeddable
I am new to Doctrine and I am trying to implement the new Embeddables present in Doctrine 2.5.
Simplified code:
<?php namespace Blah;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
* @ORM\Table(name="test")
*/
class Test {
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
* @var integer
*/
private $id;
/**
* @ORM\Embedded(class="Name")
* @var Name
*/
private $name;
public function __construct(Name $name)
{
$this->name = $name;
}
public function getId()
{
return $this->id;
}
/**
* @return Name
*/
public function getName()
{
return $this->name;
}
}
/**
* @ORM\Embeddable
*/
class Name {
/**
* @ORM\Column(type="string")
*/
private $value;
public function __construct($value)
{
$this->value = $value;
}
public function __toString()
{
return $this->value;
}
}
Seeing the query logs, Doctrine tries to insert / get a column oddly named name_value instead of a name.
What I want is to enter an object of type Name in Test, have it stored in the database as a string. When I receive an object, I want the name to be of type Name, not a string.
I tried using @ORM \ Column (type = "string") on the object in the name property and it kind of works. But instead of type Name, I get a string when Doctrine retrieves the object.
I also tried setupv * @ORM \ Column (type = "string", name = "name") * on the value object, the value is value, only to have Doctrine name as name_name instead of name_value.
The database schema was created manually. I am using Laravel and Doctrine using mitchellvanw / laravel doctrine.
source to share
After posting to the Doctrine mailing list (thanks to Marco Pivette for pointing out the responsible code), I learned that Doctrine prefix embeds design by design.
To remove prefixes, you must set columnPrefix=false
and correctly specify your value.
Final code:
<?php namespace Blah;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
* @ORM\Table(name="test")
*/
class Test {
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
* @var integer
*/
private $id;
/**
* @ORM\Embedded(class="Name", columnPrefix=false)
* @var Name
*/
private $name;
/**
*
* @param Name $name
*/
public function __construct(Name $name)
{
$this->name = $name;
}
/**
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* @return Name
*/
public function getName()
{
return $this->name;
}
}
/**
* @ORM\Embeddable
*/
class Name {
/**
* @ORM\Column(type="string", name="name")
*/
private $value;
/**
* @param string
*/
public function __construct($value)
{
$this->value = $value;
}
/**
* @return string
*/
public function __toString()
{
return $this->value;
}
}
Update:
Doctrine's attached documentation has been updated to reflect this configuration.
source to share