如何将Doctrine注释信息转换为数组?

时间:2016-03-25 02:04:45

标签: php doctrine-orm annotations doctrine symfony

我创建了以下用于Symfony 3.0的Doctrine库存实体对象。

简单地说,如何通过注释访问我在实体中输入的信息?

例如,companyID具有引用inversedBy =“location”的ManyToOne注释。这个特殊信息对我来说非常有用,所以我可以通过外键或父母关系判断它是否是一种子关系。

如果我可以通过注释以某种方式获得有关我通过注释描述的实体的信息,那么Doc​​trine会非常棒。这可能吗?基本上我正在寻找实体上的内省函数。

<?php

namespace AppBundle\Entity;

use Gedmo\Translatable\Translatable;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
use Symfony\Component\Validator\Constraints as Assert;

/**
 * Inventory
 *
 * @ORM\Table(name="distribution_inventory")
 * @ORM\Entity(repositoryClass="AppBundle\Repository\InventoryRepository")
 */
class Inventory implements Translatable {

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

    /**
     * @var string
     * @Gedmo\Translatable
     * @ORM\Column(name="name", type="string", length=255, unique=true)
     */
    private $name;

     /**
     * @var string
     * @Gedmo\Slug(fields={"name","id"},suffix=".html")
     * @ORM\Column(name="inventoryslug", type="string", length=255, nullable=false, nullable=true)
     */
    private $inventoryslug;

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

    /**
     * @var string
     * @Gedmo\Translatable
     * @ORM\Column(name="description", type="text", nullable=true)
     */
    private $description;

     /**
     * @var string
     * @ORM\Column(name="imagename", type="string", nullable=true)
     */
    private $imagename;

    /**
     * @Gedmo\Locale
     * Used locale to override Translation listener`s locale
     * this is not a mapped field of entity metadata, just a simple property
     */
    private $locale;

    /**
     * @var \AppBundle\Entity\InventoryCategory
     *
     * @ORM\ManyToOne(targetEntity="\AppBundle\Entity\InventoryCategory")
     * @ORM\JoinColumns({
     *   @ORM\JoinColumn(name="categoryid", referencedColumnName="id")
     * })
     */
    private $category;

    /**
     * @var \AppBundle\Entity\Company
     * @Assert\Type(type="\AppBundle\Entity\Company")
     * @Assert\Valid()
     * @ORM\ManyToOne(targetEntity="\AppBundle\Entity\Company", inversedBy="Location")
     * @ORM\JoinColumn(name="companyid", referencedColumnName="id", onDelete="CASCADE")
     */
    protected $companyId;

    /**
     * @var \DateTime $created
     *
     * @Gedmo\Timestampable(on="create")
     * @ORM\Column(type="datetime")
     */
    private $created;

    /**
     * @var \DateTime $updated
     *
     * @Gedmo\Timestampable(on="update")
     * @ORM\Column(type="datetime")
     */
    private $updated;

    /**
     * @var string
     * @ORM\Column(name="defaultsellprice",precision=14, scale=2, nullable=true)
     */
    private $defaultsellprice;



     /**
     * @var boolean
     *
     * @ORM\Column(name="onwaycount", type="integer", nullable=false)
     */
     private $onwaycount;

      /**
     * @var boolean
     *
     * @ORM\Column(name="instorecount", type="integer", nullable=false)
     */
     private $instorecount;

    /**
     * @var boolean
     *
     * @ORM\Column(name="wayoutcount", type="integer", nullable=false)
     */
     private $wayoutcount;

    /**
     * @var boolean
     *
     * @ORM\Column(name="instore", type="string", length=10, nullable=false)
     */
     private $instore;

      /**
     * @var string
     *
     * @ORM\Column(name="isarchived", type="string", length=5, nullable=false,options={"default":false})
     */
     private $isarchived;

     /**
     * @var string
     *
     * @ORM\Column(name="archivestatus", type="string", length=5, nullable=false,options={"default":true})
     */
     private $archivestatus;


     function __construct() {
         $this->onwaycount=0;
         $this->instore=FALSE;
         $this->instorecount=0;
         $this->wayoutcount=0;
     }

    /**
     * Get id
     *
     * @return int
     */
    public function getId() {
        return $this->id;
    }

    /**
     * Set name
     *
     * @param string $name
     *
     * @return Inventory
     */
    public function setName($name) {
        $this->name = $name;

        return $this;
    }

    /**
     * Get name
     *
     * @return string
     */
    public function getName() {
        return $this->name;
    }

    /**
     * Set barcode
     *
     * @param string $barcode
     *
     * @return Inventory
     */
    public function setBarcode($barcode) {
        $this->barcode = $barcode;

        return $this;
    }

    /**
     * Get barcode
     *
     * @return string
     */
    public function getBarcode() {
        return $this->barcode;
    }

    /**
     * Set description
     *
     * @param string $description
     *
     * @return Inventory
     */
    public function setDescription($description) {
        $this->description = $description;

        return $this;
    }

    /**
     * Get description
     *
     * @return string
     */
    public function getDescription() {
        return $this->description;
    }
    public function getImagename() {
        return $this->imagename;
    }

    public function getCategory() {
        return $this->category;
    }

    public function getCompanyId() {
        return $this->companyId;
    }

    public function getCreated() {
        return $this->created;
    }

    public function getUpdated() {
        return $this->updated;
    }

    public function getOnwaycount() {
        return $this->onwaycount;
    }

    public function getInstorecount() {
        return $this->instorecount;
    }

    public function getWayoutcount() {
        return $this->wayoutcount;
    }

    public function getInstore() {
        return $this->instore;
    }

    public function setImagename($imagename) {
        $this->imagename = $imagename;
        return $this;
    }

    public function setCategory(\AppBundle\Entity\InventoryCategory $category) {
        $this->category = $category;
        return $this;
    }

    public function setCompanyId(\AppBundle\Entity\Company $companyId) {
        $this->companyId = $companyId;
        return $this;
    }

    public function setCreated(\DateTime $created) {
        $this->created = $created;
        return $this;
    }

    public function setUpdated(\DateTime $updated) {
        $this->updated = $updated;
        return $this;
    }


    public function setOnwaycount($onwaycount) {
        $this->onwaycount = $onwaycount;
        return $this;
    }

    public function setInstorecount($instorecount) {
        $this->instorecount = $instorecount;
        return $this;
    }

    public function setWayoutcount($wayoutcount) {
        $this->wayoutcount = $wayoutcount;
        return $this;
    }

    public function setInstore($instore) {
        $this->instore = $instore;
        return $this;
    }

    public function getDefaultsellprice() {
        return $this->defaultsellprice;
    }

    public function setDefaultsellprice($defaultsellprice) {
        $this->defaultsellprice = $defaultsellprice;
        return $this;
    }

    public function getInventoryslug() {
        return $this->inventoryslug;
    }

    public function setInventoryslug($inventoryslug) {
        $this->inventoryslug = $inventoryslug;
        return $this;
    }



    public function setTranslatableLocale($locale) {
        $this->locale = $locale;
    }

    public function getIsarchived() {
        return $this->isarchived;
    }

    public function getArchivestatus() {
        return $this->archivestatus;
    }

    public function setIsarchived($isarchived) {
        $this->isarchived = $isarchived;
        return $this;
    }

    public function setArchivestatus($archivestatus) {
        $this->archivestatus = $archivestatus;
        return $this;
    }



}

发现这不确定它是否会有所帮助(http://tocacar.com/2013/01/25/doctrine2-object-introspection/

1 个答案:

答案 0 :(得分:1)

获取元数据数组:

$cmf = $em->getMetadataFactory();
$metadata = $cmf->getMetadataFor(\AppBundle\Entity\Inventory::class);
//Doctrine\ORM\Mapping\ClassMetadata instance
//as array:
$metadata = (array) $metadata;

获取反转信息:

$metadata->getAssociationMapping('companyId')['inversedBy'];
//as array
$metadata['associationMappings']['companyId']['inversedBy'];

您可以找到更多信息on the docs