代码之家  ›  专栏  ›  技术社区  ›  François Tomasi

OneToMany关联的对象即使在即时抓取模式下也不会加载

  •  0
  • François Tomasi  · 技术社区  · 7 年前

    将Symfony 3.3.13用于API

    我有 木质命令 命令 实体,其中 命令 链接到 木质命令 .

    具有以下代码:

    <?php
    
    namespace AppBundle\Entity;
    
    use Doctrine\ORM\Mapping as ORM;
    use JMS\Serializer\Annotation as JMS;
    
    /**
     * LigneCommande
     *
     * @ORM\Table(name="ligne_commande")
     * @ORM\Entity(repositoryClass="AppBundle\Repository\LigneCommandeRepository")
     */
    class LigneCommande
    {
        /**
         * @var int
         *
         * @JMS\Groups({"listeLigneCommandes", "detailLigneCommande"})
         *
         * @ORM\Column(name="id", type="integer")
         * @ORM\Id
         * @ORM\GeneratedValue(strategy="AUTO")
         */
        protected $id;
    
        /**
         * @var Commande
         *
         * @JMS\Groups({"listeLigneCommandes", "detailLigneCommande"})
         *
         * Many LigneCommande have One numeroCommande.
         * @ORM\ManyToOne(targetEntity="Commande", inversedBy="lignes", cascade={"all"}, fetch="EAGER")
         */
        protected $commande;
    
        /**
         * @var Article
         *
         * @JMS\Groups({"listeLigneCommandes", "detailLigneCommande"})
         *
         * Many LigneCommande have One codeArticle.
         * @ORM\ManyToOne(targetEntity="Article", inversedBy="lignes", cascade={"all"}, fetch="EAGER")
         */
        protected $article;
    
        /**
         * @var string
         *
         * @JMS\Groups({"listeLigneCommandes", "detailLigneCommande"})
         *
         * @ORM\Column(name="libelle", type="string", length=255)
         */
        protected $libelle;
    
        /**
         * @var string
         *
         * @JMS\Groups({"listeLigneCommandes", "detailLigneCommande"})
         *
         * @ORM\Column(name="unite", type="string", length=255)
         */
        protected $unite;
    
        /**
         * @var float
         *
         * @JMS\Groups({"listeLigneCommandes", "detailLigneCommande"})
         *
         * @ORM\Column(name="qte_commande", type="float")
         */
        protected $qteCommande;
    
        /**
         * @var float
         *
         * @JMS\Groups({"listeLigneCommandes", "detailLigneCommande"})
         *
         * @ORM\Column(name="qte_recue", type="float")
         */
        protected $qteRecue;
    
        /**
         * @var float
         *
         * @JMS\Groups({"listeLigneCommandes", "detailLigneCommande"})
         *
         * @ORM\Column(name="qte_arecevoir", type="float")
         */
        protected $qteArecevoir;
    
        /**
         * @var float
         *
         * @JMS\Groups({"listeLigneCommandes", "detailLigneCommande"})
         *
         * @ORM\Column(name="identifiant", type="float", unique=true)
         */
        protected $identifiant;
    
    
        /**
         * Get id
         *
         * @return int
         */
        public function getId()
        {
            return $this->id;
        }
    
        /**
         * Set commande
         *
         * @param Commande $commande
         *
         * @return LigneCommande
         */
        public function setCommande(Commande $commande)
        {
            $this->numero = $commande;
    
            return $this;
        }
    
        /**
         * Get commande
         *
         * @return Commande
         */
        public function getCommande()
        {
            return $this->commande;
        }
    
        /**
         * Set article
         *
         * @param Article $article
         *
         * @return LigneCommande
         */
        public function setArticle(Article $article)
        {
            $this->article = $article;
    
            return $this;
        }
    
        /**
         * Get article
         *
         * @return Article
         */
        public function getArticle()
        {
            return $this->article;
        }
    
        /**
         * Set libelle
         *
         * @param string $libelle
         *
         * @return LigneCommande
         */
        public function setLibelle($libelle)
        {
            $this->libelle = $libelle;
    
            return $this;
        }
    
        /**
         * Get libelle
         *
         * @return string
         */
        public function getLibelle()
        {
            return $this->libelle;
        }
    
        /**
         * Set unite
         *
         * @param string $unite
         *
         * @return LigneCommande
         */
        public function setUnite($unite)
        {
            $this->unite = $unite;
    
            return $this;
        }
    
        /**
         * Get unite
         *
         * @return string
         */
        public function getUnite()
        {
            return $this->unite;
        }
    
        /**
         * Set qteCommande
         *
         * @param float $qteCommande
         *
         * @return LigneCommande
         */
        public function setQteCommande($qteCommande)
        {
            $this->qteCommande = $qteCommande;
    
            return $this;
        }
    
        /**
         * Get qteCommande
         *
         * @return float
         */
        public function getQteCommande()
        {
            return $this->qteCommande;
        }
    
        /**
         * Set qteRecue
         *
         * @param float $qteRecue
         *
         * @return LigneCommande
         */
        public function setQteRecue($qteRecue)
        {
            $this->qteRecue = $qteRecue;
    
            return $this;
        }
    
        /**
         * Get qteRecue
         *
         * @return float
         */
        public function getQteRecue()
        {
            return $this->qteRecue;
        }
    
        /**
         * Set qteArecevoir
         *
         * @param float $qteArecevoir
         *
         * @return LigneCommande
         */
        public function setQteArecevoir($qteArecevoir)
        {
            $this->qteArecevoir = $qteArecevoir;
    
            return $this;
        }
    
        /**
         * Get qteArecevoir
         *
         * @return float
         */
        public function getQteArecevoir()
        {
            return $this->qteArecevoir;
        }
    
        /**
         * Set identifiant
         *
         * @param float $identifiant
         *
         * @return LigneCommande
         */
        public function setIdentifiant($identifiant)
        {
            $this->identifiant = $identifiant;
    
            return $this;
        }
    
        /**
         * Get identifiant
         *
         * @return float
         */
        public function getIdentifiant()
        {
            return $this->identifiant;
        }
    }
    

    命令。php 具有以下代码:

    <?php
    
    namespace AppBundle\Entity;
    
    use Doctrine\Common\Collections\ArrayCollection;
    use Doctrine\Common\Collections\Collection;
    use Doctrine\ORM\Mapping as ORM;
    use JMS\Serializer\Annotation as JMS;
    
    /**
     * Commande
     *
     * @ORM\Table(name="commande")
     * @ORM\Entity(repositoryClass="AppBundle\Repository\CommandeRepository")
     */
    class Commande
    {
        /**
         * @var int
         *
         * @JMS\Groups({"listeCommandes"})
         *
         * @ORM\Column(name="id", type="integer")
         * @ORM\Id
         * @ORM\GeneratedValue(strategy="AUTO")
         */
        private $id;
    
        /**
         * @var string
         *
         * @JMS\Groups({"listeCommandes", "detailCommande"})
         *
         * @ORM\Column(name="numero", type="string", length=8, unique=true)
         */
        private $numero;
    
        /**
         * @var \DateTime
         *
         * @JMS\Groups({"listeCommandes", "detailCommande"})
         *
         * @ORM\Column(name="date", type="date")
         */
        private $date;
    
        /**
         * @var int
         *
         * @JMS\Groups({"listeCommandes", "detailCommande"})
         *
         * @ORM\Column(name="chantier_id", type="integer")
         *
         * Many Commande have One chantierCode.
         * @ORM\ManyToOne(targetEntity="Chantier")
         * @ORM\JoinColumn(name="chantier_id", referencedColumnName="id")
         */
        private $chantier;
    
        /**
         * @var string
         *
         * @JMS\Groups({"listeCommandes", "detailCommande"})
         *
         * @ORM\Column(name="fournisseur_code", type="string", length=7)
         */
        private $fournisseurCode;
    
        /**
         * @var string
         *
         * @JMS\Groups({"listeCommandes", "detailCommande"})
         *
         * @ORM\Column(name="fournisseur_nom", type="string", length=255)
         */
        private $fournisseurNom;
    
        /**
         * @var string
         *
         * @JMS\Groups({"listeCommandes", "detailCommande"})
         *
         * @ORM\Column(name="redacteur_code", type="string", length=5)
         */
        private $redacteurCode;
    
        /**
         * @var string
         *
         * @JMS\Groups({"listeCommandes", "detailCommande"})
         *
         * @ORM\Column(name="secteur_code", type="string", length=1, nullable=true)
         */
        private $secteurCode;
    
        /**
         * @var string
         *
         * @JMS\Groups({"listeCommandes", "detailCommande"})
         *
         * @ORM\Column(name="livraison_nom", type="string", length=255)
         */
        private $livraisonNom;
    
        /**
         * @var string
         *
         * @JMS\Groups({"listeCommandes", "detailCommande"})
         *
         * @ORM\Column(name="livraison_adresse", type="string", length=255)
         */
        private $livraisonAdresse;
    
        /**
         * @var string
         *
         * @JMS\Groups({"listeCommandes", "detailCommande"})
         *
         * @ORM\Column(name="livraison_cp", type="string", length=5)
         */
        private $livraisonCp;
    
        /**
         * @var string
         *
         * @JMS\Groups({"listeCommandes", "detailCommande"})
         *
         * @ORM\Column(name="livraison_ville", type="string", length=255)
         */
        private $livraisonVille;
    
        /**
         * @var Collection
         *
         * @ORM\OneToMany(targetEntity="LigneCommande", mappedBy="commande")
         */
        protected $lignes;
    
    
        public function __construct() {
            $this->lignes = new ArrayCollection();
        }
    
    
        /**
         * Get id
         *
         * @return int
         */
        public function getId()
        {
            return $this->id;
        }
    
        /**
         * Set numero
         *
         * @param string $numero
         *
         * @return Commande
         */
        public function setNumero($numero)
        {
            $this->numero = $numero;
    
            return $this;
        }
    
        /**
         * Get numero
         *
         * @return string
         */
        public function getNumero()
        {
            return $this->numero;
        }
    
        /**
         * Set date
         *
         * @param \DateTime $date
         *
         * @return Commande
         */
        public function setDate($date)
        {
            $this->date = $date;
    
            return $this;
        }
    
        /**
         * Get date
         *
         * @return \DateTime
         */
        public function getDate()
        {
            return $this->date;
        }
    
        /**
         * Set chantier
         *
         * @param int $chantier
         *
         * @return Commande
         */
        public function setChantier($chantier)
        {
            $this->chantier = $chantier;
    
            return $this;
        }
    
        /**
         * Get chantier
         *
         * @return int
         */
        public function getChantier()
        {
            return $this->chantier;
        }
    
        /**
         * Set fournisseurCode
         *
         * @param string $fournisseurCode
         *
         * @return Commande
         */
        public function setFournisseurCode($fournisseurCode)
        {
            $this->fournisseurCode = $fournisseurCode;
    
            return $this;
        }
    
        /**
         * Get fournisseurCode
         *
         * @return string
         */
        public function getFournisseurCode()
        {
            return $this->fournisseurCode;
        }
    
        /**
         * Set fournisseurNom
         *
         * @param string $fournisseurNom
         *
         * @return Commande
         */
        public function setFournisseurNom($fournisseurNom)
        {
            $this->fournisseurNom = $fournisseurNom;
    
            return $this;
        }
    
        /**
         * Get fournisseurNom
         *
         * @return string
         */
        public function getFournisseurNom()
        {
            return $this->fournisseurNom;
        }
    
        /**
         * Set redacteurCode
         *
         * @param string $redacteurCode
         *
         * @return Commande
         */
        public function setRedacteurCode($redacteurCode)
        {
            $this->redacteurCode = $redacteurCode;
    
            return $this;
        }
    
        /**
         * Get redacteurCode
         *
         * @return string
         */
        public function getRedacteurCode()
        {
            return $this->redacteurCode;
        }
    
        /**
         * Set secteurCode
         *
         * @param string $secteurCode
         *
         * @return Commande
         */
        public function setSecteurCode($secteurCode)
        {
            $this->secteurCode = $secteurCode;
    
            return $this;
        }
    
        /**
         * Get secteurCode
         *
         * @return string
         */
        public function getSecteurCode()
        {
            return $this->secteurCode;
        }
    
        /**
         * Set livraisonNom
         *
         * @param string $livraisonNom
         *
         * @return Commande
         */
        public function setLivraisonNom($livraisonNom)
        {
            $this->livraisonNom = $livraisonNom;
    
            return $this;
        }
    
        /**
         * Get livraisonNom
         *
         * @return string
         */
        public function getLivraisonNom()
        {
            return $this->livraisonNom;
        }
    
        /**
         * Set livraisonAdresse
         *
         * @param string $livraisonAdresse
         *
         * @return Commande
         */
        public function setLivraisonAdresse($livraisonAdresse)
        {
            $this->livraisonAdresse = $livraisonAdresse;
    
            return $this;
        }
    
        /**
         * Get livraisonAdresse
         *
         * @return string
         */
        public function getLivraisonAdresse()
        {
            return $this->livraisonAdresse;
        }
    
        /**
         * Set livraisonCp
         *
         * @param string $livraisonCp
         *
         * @return Commande
         */
        public function setLivraisonCp($livraisonCp)
        {
            $this->livraisonCp = $livraisonCp;
    
            return $this;
        }
    
        /**
         * Get livraisonCp
         *
         * @return string
         */
        public function getLivraisonCp()
        {
            return $this->livraisonCp;
        }
    
        /**
         * Set livraisonVille
         *
         * @param string $livraisonVille
         *
         * @return Commande
         */
        public function setLivraisonVille($livraisonVille)
        {
            $this->livraisonVille = $livraisonVille;
    
            return $this;
        }
    
        /**
         * Get livraisonVille
         *
         * @return string
         */
        public function getLivraisonVille()
        {
            return $this->livraisonVille;
        }
    
        /**
         * @return Collection
         */
        public function getLignes() {
            return $this->lignes;
        }
    }
    

    木质命令控制器。php 具有以下代码:

    /**
         * @Rest\View(serializerGroups={"detailLigneCommande"})
         * @Rest\Get(
         *     path = "/ligneCommandes/{id}",
         *     name = "show_ligne_commande",
         *     requirements = {"id"="\d+"}
         * )
         */
        public function getLigneCommandeAction(Request $request)
        {
            $ligneCommande = $this->getDoctrine()
                ->getRepository('AppBundle:LigneCommande')
                ->find($request->get('id'));
            /* @var $ligneCommande LigneCommande */
    
            $ligneCommande->getCommande();
    
            if (empty($ligneCommande)) {
                return View::create(['message' => 'Ligne de commande introuvable'], Response::HTTP_OK);
            } else {
                return $ligneCommande;
            }
        }
    

    所以在我的API中,当我调用 localhost:8000/ligneCommandes/1 使用fetch“EAGER”属性,我希望得到这样的结果:

    {
        "id": 1,
        "commande": {
            "numero": "CF000001",
            "date": "2017-10-13T00:00:00+00:00",
            "chantier": 1,
            "fournisseur_code": "F000001",
            "fournisseur_nom": "Fournisseur A",
            "redacteur_code": "FTOMA",
            "secteur_code": "1",
            "livraison_nom": "Nom livraison",
            "livraison_adresse": "Adresse livraison",
            "livraison_cp": "74940",
            "livraison_ville": "Ville livraison"
        },
        "article": {},
        "libelle": "Lorem ipsum dolor sit amet, consectetur adipiscing elit.",
        "unite": "TL",
        "qte_commande": 10,
        "qte_recue": 8,
        "qte_arecevoir": 2,
        "identifiant": 1
    }
    

    但我明白了:

    {
        "id": 1,
        "commande": {},
        "article": {},
        "libelle": "Lorem ipsum dolor sit amet, consectetur adipiscing elit.",
        "unite": "TL",
        "qte_commande": 10,
        "qte_recue": 8,
        "qte_arecevoir": 2,
        "identifiant": 1
    }
    

    我真的不知道为什么,即使使用fetch-EAGER方法,对象仍然作为代理加载。

    感谢您的帮助:)

    1 回复  |  直到 7 年前
        1
  •  0
  •   François Tomasi    7 年前

    好的,我找到了一个解决方案。 只要删除序列化程序组,它就可以工作了。