问题
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