Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony get Values from Entity

Is there a possibility to read all available Values from an entity?

E.G.

class Properties
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var string
     *
     * @ORM\Column(name="UserID", type="string", length=255)
     */
    private $userID;

    /**
     * @var string
     *
     * @ORM\Column(name="Sport", type="string", length=1)
     */
    private $sport;

.
.
.

So that I will get the name of the Value like: Id, UserID, Sport?

like image 345
TheTom Avatar asked Nov 28 '25 23:11

TheTom


2 Answers

You can read the info you need thru the Doctrine metadata info as follow:

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

    $className = "Acme\DemoBundle\Entity\Properties";

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

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

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

    // OR query for all fields
    // Returns an array with all the identifier column names. 
    $metadata->getIdentifierColumnNames();

More info on the API DOC

Hope this help

like image 176
Matteo Avatar answered Dec 01 '25 17:12

Matteo


You can make use of ReflectionClass::getProperties() to loop through all properties.

http://php.net/manual/en/reflectionclass.getproperties.php

like image 45
Rvanlaak Avatar answered Dec 01 '25 18:12

Rvanlaak