【问题标题】:JMSSerializer Bundle - Circular Reference Error (Only on Prod Azure Environment) - Symfony4/Doctrine2 REST APIJMSSerializer Bundle - 循环引用错误(仅在 Prod Azure 环境中) - Symfony4/Doctrine2 REST API
【发布时间】:2018-07-18 15:49:45
【问题描述】:

所以我知道之前已经讨论过很多次类似的问题,但我没有找到解决这个特定问题的方法。

在本地运行(使用 MAMP)我的 API 响应没有问题。然而,一旦部署到生产 Azure 服务器(通过 Ansible),我就会遇到可怕的错误:

request.CRITICAL: Uncaught PHP Exception Symfony\Component\Serializer\Exception\CircularReferenceException: "A circular reference has been detected when serializing the object of class "App\ServiceProviderBundle\Entity\Plan

我确信我的所有教义关联都已正确设置,但某些东西正在触发无限循环。

这是一个简化的实体关系和我的学说类中的主要关联。

我们会大力推荐任何 cmets 或帮助 - 非常感谢!

计划 -> (hasMany) 捆绑包 -> (hasMany) -> 产品

class Plan {

/**
 * @ORM\OneToMany(targetEntity="App\ServiceProviderBundle\Entity\Bundle", mappedBy="plan")
 */
    private $bundles;
}


class Bundle {

    /**
     * @ORM\ManyToOne(targetEntity="App\ServiceProviderBundle\Entity\Plan", inversedBy="bundles")
     * @ORM\JoinColumn(nullable=true)
     */
    private $plan;

    /**
     * @SerializedName("products")
     * @ORM\OneToMany(targetEntity="App\ServiceProviderBundle\Entity\BundleProduct", mappedBy="bundle",
     *     cascade={"persist", "remove"})
     * @ORM\JoinColumn(nullable=false)
     */
    private $bundleProducts;
}


class BundleProduct {

    /**
     * @ORM\ManyToOne(targetEntity="App\ServiceProviderBundle\Entity\Bundle", inversedBy="bundleProducts")
     * @ORM\JoinColumn(nullable=false)
     */
    private $bundle;
}

【问题讨论】:

    标签: api doctrine-orm symfony4 jmsserializerbundle


    【解决方案1】:

    像这样使用@Exclude注解:

    class BundleProduct {
    
        /**
         * @ORM\ManyToOne(targetEntity="App\ServiceProviderBundle\Entity\Bundle", inversedBy="bundleProducts")
         * @ORM\JoinColumn(nullable=false)
         * @Exclude
         */
        private $bundle;
    }
    

    【讨论】:

    • 您好 Nikita,感谢您回复我。我一直在默认情况下排除所有内容并使用 @Expose 添加我需要的属性。我想我找到了问题 - 没有为“prod”环境启用 JMSSerializerBundle。虚拟机启动后,我会在大约 5 分钟内知道...
    猜你喜欢
    • 2018-07-09
    • 1970-01-01
    • 1970-01-01
    • 2017-09-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-21
    • 1970-01-01
    相关资源
    最近更新 更多