Symfony2:ManyToMany属性

时间:2014-07-21 20:02:28

标签: php symfony

我目前在使用属性的ManyToMany关系时遇到问题。 我有一个实体: - 产品 - PackProduit(两个实体之间的关系) - 包装(更多产品)

问题是我无法恢复产品构成包装,因为我会看到产品包装。

这些是创建的实体:

produit.php

<?php
namespace Acme\DemoBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;

/**
 * Produit
 *
 * @ORM\Table(name="Acme_produit")
 * @ORM\Entity(repositoryClass="Acme\DemoBundle\Entity\ProduitRepository")
 */
class Produit
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @ORM\OneToMany(targetEntity="Acme\DemoBundle\Entity\PackProduit", mappedBy="produit")
     */
    protected $packProduits;

    /**
     * @var string
     *
     * @ORM\Column(name="nom", type="string", length=255)
     */
    private $nom;

    /**
     * @var string
     *
     * @ORM\Column(name="designation", type="string", length=255)
     */
    private $designation;

    /**
     * @var integer
     *
     * @ORM\Column(name="quantite", type="integer")
     */
    private $quantite;



    public function __construct()
    {
        $this->packProduits = new ArrayCollection();
    }



    public function __toString()
    {
        return $this->nom;
    }

    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set nom
     *
     * @param string $nom
     * @return Produit
     */
    public function setNom($nom)
    {
        $this->nom = $nom;

        return $this;
    }

    /**
     * Get nom
     *
     * @return string 
     */
    public function getNom()
    {
        return $this->nom;
    }

    /**
     * Set designation
     *
     * @param string $designation
     * @return Produit
     */
    public function setDesignation($designation)
    {
        $this->designation = $designation;

        return $this;
    }

    /**
     * Get designation
     *
     * @return string 
     */
    public function getDesignation()
    {
        return $this->designation;
    }

    /**
     * Set quantite
     *
     * @param integer $quantite
     * @return Produit
     */
    public function setQuantite($quantite)
    {
        $this->quantite = $quantite;

        return $this;
    }

    /**
     * Get quantite
     *
     * @return integer 
     */
    public function getQuantite()
    {
        return $this->quantite;
    }

    /**
     * Add packProduits
     *
     * @param \Acme\DemoBundle\Entity\PackProduit $packProduits
     * @return Produit
     */
    public function addPackProduit(\Acme\DemoBundle\Entity\PackProduit $packProduits)
    {
        $this->packProduits[] = $packProduits;

        return $this;
    }

    /**
     * Remove packProduits
     *
     * @param \Acme\DemoBundle\Entity\PackProduit $packProduits
     */
    public function removePackProduit(\Acme\DemoBundle\Entity\PackProduit $packProduits)
    {
        $this->packProduits->removeElement($packProduits);
    }

    /**
     * Get packProduits
     *
     * @return \Doctrine\Common\Collections\Collection 
     */
    public function getPackProduits()
    {
        return $this->packProduits;
    }
}

PackProduit.php

<?php

namespace Acme\DemoBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * PackProduit
 *
 * @ORM\Table(name="Acme_pack_produit")
 * @ORM\Entity(repositoryClass="Acme\DemoBundle\Entity\PackProduitRepository")
 */
class PackProduit
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer", name="id")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @ORM\ManyToOne(targetEntity="Acme\DemoBundle\Entity\Produit", inversedBy="packProduits")
     */
    protected $produit;

    /**
     * @ORM\ManyToOne(targetEntity="Acme\DemoBundle\Entity\Pack", inversedBy="packProduits")
     */
    protected $pack;

    /**
     * @var integer
     *
     * @ORM\Column(name="quantite", type="integer")
     */
    private $quantite;



    public function __toString()
    {
        return 'PackProduit{'.$this->quantite.'}';
    }

    /**
     * Set quantite
     *
     * @param integer $quantite
     * @return PackProduit
     */
    public function setQuantite($quantite)
    {
        $this->quantite = $quantite;

        return $this;
    }

    /**
     * Get quantite
     *
     * @return integer 
     */
    public function getQuantite()
    {
        return $this->quantite;
    }

    /**
     * Set produit
     *
     * @param \Acme\DemoBundle\Entity\Produit $produit
     * @return PackProduit
     */
    public function setProduit(\Acme\DemoBundle\Entity\Produit $produit)
    {
        $this->produit = $produit;

        return $this;
    }

    /**
     * Get produit
     *
     * @return \Acme\DemoBundle\Entity\Produit 
     */
    public function getProduit()
    {
        return $this->produit;
    }

    /**
     * Set pack
     *
     * @param \Acme\DemoBundle\Entity\Pack $pack
     * @return PackProduit
     */
    public function setPack(\Acme\DemoBundle\Entity\Pack $pack)
    {
        //$pack->addPackProduit($this);
        $this->pack = $pack;

        return $this;
    }

    /**
     * Get pack
     *
     * @return \Acme\DemoBundle\Entity\Pack 
     */
    public function getPack()
    {
        return $this->pack;
    }

    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }
}

Pack.php

<?php
namespace Acme\DemoBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;

/**
 * Pack
 *
 * @ORM\Table(name="Acme_pack")
 * @ORM\Entity(repositoryClass="Acme\DemoBundle\Entity\PackRepository")
 */
class Pack
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @ORM\OneToMany(targetEntity="Acme\DemoBundle\Entity\PackProduit", mappedBy="produit", cascade={"persist", "remove"})
     */
    protected $packProduits;

    /**
     * @ORM\ManyToOne(targetEntity="Acme\DemoBundle\Entity\Tva", cascade={"persist"})
     * @ORM\JoinColumn(nullable=false)
     */
    private $tva;

    /**
     * @var string
     *
     * @ORM\Column(name="nom", type="string", length=255)
     */
    private $nom;

    /**
     * @var float
     *
     * @ORM\Column(name="prix", type="float")
     */
    private $prix;

    /**
     * @var float
     *
     * @ORM\Column(name="coef_correlation", type="float")
     */
    private $coefCorrelation;

    /**
     * @var boolean
     *
     * @ORM\Column(name="active", type="boolean")
     */
    private $active;


    public function __construct()
    {
        $this->packProduits = new \Doctrine\Common\Collections\ArrayCollection();
        $this->active = 1;
    }

    public function __toString()
    {
        return $this->nom;
    }

    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set nom
     *
     * @param string $nom
     * @return Pack
     */
    public function setNom($nom)
    {
        $this->nom = $nom;

        return $this;
    }

    /**
     * Get nom
     *
     * @return string 
     */
    public function getNom()
    {
        return $this->nom;
    }

    /**
     * Set prix
     *
     * @param float $prix
     * @return Pack
     */
    public function setPrix($prix)
    {
        $this->prix = $prix;

        return $this;
    }

    /**
     * Get prix
     *
     * @return float 
     */
    public function getPrix()
    {
        return $this->prix;
    }

    /**
     * Add packProduits
     *
     * @param \Acme\DemoBundle\Entity\PackProduit $packProduits
     * @return Pack
     */
    public function addPackProduit(\Acme\DemoBundle\Entity\PackProduit $packProduits)
    {
        $this->packProduits[] = $packProduits;

        return $this;
    }

    /**
     * Remove packProduits
     *
     * @param \Acme\DemoBundle\Entity\PackProduit $packProduits
     */
    public function removePackProduit(\Acme\DemoBundle\Entity\PackProduit $packProduits)
    {
        $this->packProduits->removeElement($packProduits);
    }

    /**
     * Get packProduits
     *
     * @return \Doctrine\Common\Collections\Collection 
     */
    public function getPackProduits()
    {
        return $this->packProduits;
    }

    /**
     * Set active
     *
     * @param boolean $active
     * @return Pack
     */
    public function setActive($active)
    {
        $this->active = $active;

        return $this;
    }

    /**
     * Get active
     *
     * @return boolean 
     */
    public function getActive()
    {
        return $this->active;
    }

    /**
     * Set tva
     *
     * @param \Acme\DemoBundle\Entity\Tva $tva
     * @return Pack
     */
    public function setTva(\Acme\DemoBundle\Entity\Tva $tva)
    {
        $this->tva = $tva;

        return $this;
    }

    /**
     * Get tva
     *
     * @return \Acme\DemoBundle\Entity\Tva 
     */
    public function getTva()
    {
        return $this->tva;
    }

    /**
     * Set coefCorrelation
     *
     * @param float $coefCorrelation
     * @return Pack
     */
    public function setCoefCorrelation($coefCorrelation)
    {
        $this->coefCorrelation = $coefCorrelation;

        return $this;
    }

    /**
     * Get coefCorrelation
     *
     * @return float 
     */
    public function getCoefCorrelation()
    {
        return $this->coefCorrelation;
    }
}

谢谢!

1 个答案:

答案 0 :(得分:0)

$produits = array();
foreach($pack->getPackProduits() as $packProduits){
    $produits[] = $packProduits->getProduit();
}

$produits数组现在认为产品属于$pack。 使用代理方法,您可以使其更加舒适,但我建议您将关系表从实体中删除。我觉得有点hacky。

相关问题