Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

read metadata of a Doctrine entity property

I have to following entity :

/**
 * ProductService
 *
 * @ORM\Table(name="sf_products_services")
 * @ORM\Entity(repositoryClass="Evo\BackendBundle\Entity\ProductServiceRepository")
 */
class ProductService
{
    [...]

    /**
     * @var string
     *
     * @ORM\Column(name="name", type="string", length=150)
     */
    protected $name;

    [...]

How can I read the "length" value of the $name property ? I read I could use doctrine metadata, but I dont find anything about how to use it and how to read these data.

like image 680
VaN Avatar asked Apr 02 '15 14:04

VaN


2 Answers

In accordion with the @wonde answer you can read the info you need thru the Doctrine metadata info as follow:

    $doctrine = $this->getContainer()->get("doctrine");
    $em = $doctrine->getManager();

    $className = "Evo\BackendBundle\Entity\ProductService";

    $metadata = $em->getClassMetadata($className);

    $nameMetadata = $metadata->fieldMappings['name'];

    echo $nameMetadata['type'];  //print "string"
    echo $nameMetadata['length']; // print "150"

Hope this help

like image 79
Matteo Avatar answered Oct 20 '22 10:10

Matteo


getClassMetadata( mixed $className ) Returns the ORM metadata descriptor for a class

e.g

$metadata = $entityManager->getClassMetadata($className);

"The class name must be the fully-qualified class name without a leading backslash (as it is returned by get_class($obj)) or an aliased class name. Examples: MyProject\Domain\User sales:PriceRequest"

like image 27
wonde Avatar answered Oct 20 '22 11:10

wonde