从ManyToMany表中获取

时间:2017-05-16 04:09:35

标签: mysql doctrine-orm php-7 symfony-3.2

我正在尝试从mysql表中获取结果 我正在使用symfony3,当然还有教义2 我有2个实体Client.php

<?php

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Doctrine\ORM\Mapping\ManyToMany;
use Doctrine\ORM\Mapping\JoinTable;
use Doctrine\ORM\Mapping\JoinColumn;

/**
 * Client
 *
 * @ORM\Table(name="client")
 * @ORM\Entity(repositoryClass="AppBundle\Repository\ClientRepository")
 */
class Client
{
    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

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

    /**
    * @ManyToMany(targetEntity="
    * @JoinTable(name="listesouhait",
    * joinColumns={@JoinColumn(name="utilisateur_id", 
    referencedColumnName="id")},
     * inverseJoinColumns={@JoinColumn(name="evenement_id", 
    referencedColumnName="id")}
     *      )
     */
    private $evenements;

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

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

    /**
     * Set cin
     *
     * @param integer $cin
     *
     * @return Client
     */
    public function setCin($cin)
    {
        $this->cin = $cin;

        return $this;
    }

    /**
     * Get cin
     *
     * @return int
     */
    public function getCin()
    {
        return $this->cin;
    }

    /**
     * Add evenement
     *
     * @param \AppBundle\Entity\Evenement $evenement
     *
     * @return Client
     */
    public function addEvenement(\AppBundle\Entity\Evenement $evenement)
    {
        $this->evenements[] = $evenement;

        return $this;
    }

    /**
     * Remove evenement
     *
     * @param \AppBundle\Entity\Evenement $evenement
     */
    public function removeEvenement(\AppBundle\Entity\Evenement $evenement)
    {
        $this->evenements->removeElement($evenement);
    }

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

和Evenement.php

<?php

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Doctrine\ORM\Mapping\ManyToOne;
use Doctrine\ORM\Mapping\JoinColumn;

/**
* Evenement
*
* @ORM\Table(name="evenement")
* @ORM\Entity(repositoryClass="AppBundle\Repository\EvenementRepository")
*/
class Evenement
{
    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;
    //all attributes

执行php bin \ console doctrine时:schema:update --force
image source
现在一切都很好 但我找不到一个明确的代码插入我的controllerAction来获取表 listeshouhait 自动生成)中的所有记录,因为表 listeshouhait 不是真正的实体 还要提到我的存储库是空的 我在存储库中听说过 DQL ,它可能会有所帮助 可以帮我吗?

1 个答案:

答案 0 :(得分:0)

要获取附加到客户端的事件列表,请在控制器中:

$em = $this->getDoctrine()->getManager();

// Récupérer le client qui a l'id = 1
$item = $em->getRepository('AppBundle:Client')->findOneById(1);

// Liste des evenements 
$events = $item->getEvenements();
foreach ($events as $event)
{
    echo $event->getId() .'<br>';

}
die;