JMSSerializer Bundle - Circular Reference Error (Only on Prod Azure Environment) - Symfony4/Doctrine2 REST API

梦想的初衷 提交于 2019-12-11 14:49:43

问题


So I know somewhat similar issues have been discussed numerous times before but I haven't had any luck finding a solution with this specific issue.

Running locally (using MAMP) I have no issues with my API responses. However once deployed to the production Azure server (via Ansible) I run into the dreaded error:

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

I'm confident that all of my doctrine associations are setup correctly yet something is triggering an infinite loop.

Here is a simplified entity relationship and the main associations from within my doctrine classes.

Any comments or help would be greatly suggested - many thanks!

Plan -> (hasMany) Bundle -> (hasMany) -> Product

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;
}

回答1:


Use @Exclude annotation like that:

class BundleProduct {

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


来源:https://stackoverflow.com/questions/51406209/jmsserializer-bundle-circular-reference-error-only-on-prod-azure-environment

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!