【问题标题】:Symfony2 get serialized object in one to many relationSymfony2以一对多关系获取序列化对象
【发布时间】:2015-07-27 07:57:22
【问题描述】:

如何在 jsonSerialize 方法中获取序列化的 cardPrices? id 是 OneToMany 与 CardPrice 的关系,但如何获得这个价格? 'price' => $this->getId()->getPriceNet() 不起作用。

CustomerCardSubtype.php

class CustomerCardSubtype implements \JsonSerializable
{
    /**
     * Primary key.
     *
     * @var int
     *
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     * @ORM\OneToMany(targetEntity="CardPrice", mappedBy="customerCardSubtype")
     */
    private $id;

/**
 * @return array
 */
public function jsonSerialize()
{
    return [
        'id' => $this->getId(),
        'description' => $this->getDescription(),
        'name' => $this->getName()
    ];
}

CardPrice.php

class CardPrice implements \JsonSerializable
{

    /**
     * CustomerCardSubtype.
     *
     * @var CustomerCardSubtype
     *
     * @ORM\ManyToOne(targetEntity="CustomerCardSubtype")
     * @ORM\JoinColumn(name="customer_card_subtype_id", referencedColumnName="id", nullable=false, onDelete="RESTRICT")
     */
    private $customerCardSubtype;

/**
 * Get priceNet.
 *
 * @return float
 */
public function getPriceNet()
{
    return $this->priceNet;
}

【问题讨论】:

    标签: symfony doctrine-orm


    【解决方案1】:

    你误解了Doctrine Association Mapping的用法; id 应该是(并且是)一个整数,所以你不能在它上面调用方法。这不起作用:

    'price' => $this->getId()->getPriceNet()
    

    相反,为关联本身创建一个属性(以及可选的 get/set 方法)。

    您的CustomerCardSubtype 应该如下所示:

    <?php
    
    class CustomerCardSubtype implements \JsonSerializable
    {
        /**
         * Primary key.
         *
         * @var int
         *
         * @ORM\Id
         * @ORM\Column(type="integer")
         * @ORM\GeneratedValue(strategy="AUTO")
         */
        private $id;
    
        /**
         * Association.
         *
         * @ORM\OneToMany(targetEntity="CardPrice", mappedBy="customerCardSubtype")
         */
        private $cardPrice;
    
        /**
         * @param CardPrice $cardPrice
         */
        public function setCardPrice($cardPrice)
        {
            $this->cardPrice = $cardPrice;
        }
    
        /**
         * @return CardPrice
         */
        public function getCardPrice()
        {
            return $this->cardPrice;
        }
    
        /**
         * @return array
         */
        public function jsonSerialize()
        {
            return [
                'id' => $this->getId(),
                'description' => $this->getDescription(),
                'name' => $this->getName(),
                'price' => $this->cardPrice->getPriceNet()
            ];
        }
    }
    

    您现在应该能够在关联对象上调用getPriceNet 方法。如果您想将json_encode 整个CardPrice 对象作为嵌套元素,您也可以在其上实现JsonSerializable。希望这可以帮助。

    【讨论】:

    • 最后一行代码不应该改成'price' =&gt; $this-&gt;cardPrice-&gt;getPriceNet()吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-10-05
    • 2015-08-13
    • 1970-01-01
    • 2014-05-09
    • 2018-11-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多