TheTom
TheTom

Reputation: 1054

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?

Upvotes: 3

Views: 5394

Answers (2)

Rvanlaak
Rvanlaak

Reputation: 3085

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

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

Upvotes: 1

Matteo
Matteo

Reputation: 39470

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

Upvotes: 4

Related Questions