2017-12-04 13 views
-1

이 함께 도와주세요 렌더링, 내가 컨트롤러에 템플릿 나뭇 가지를 렌더링 할 때이 문제가 관계에 문제가 있어요 것은 "템플릿 관계 ManyToMany 심포니 교리가

어느 재산"브레 "나 방법 중 하나를 ManyToMany "Doctrine \ ORM \ PersistentCollection"클래스에 공개 액세스 권한이 있습니다. 내 엔티티와 컨트롤러를 넣을 예정입니다. 제발 도와주세요. 이와 나 :

<?php 

namespace AdminBundle\Entity; 

use Doctrine\ORM\Mapping as ORM; 
use Symfony\Component\Validator\Constraints as Assert; 
use Doctrine\Common\Collections\ArrayCollection; 

/** 
* Menu 
* 
* @ORM\Table(name="menu") 
* @ORM\Entity(repositoryClass="AdminBundle\Repository\MenuRepository") 
*/ 
class Menu 
{ 
    /** 
    * @var int 
    * 
    * @ORM\Column(name="id", type="integer") 
    * @ORM\Id 
    * @ORM\GeneratedValue(strategy="AUTO") 
    */ 
    private $id; 

    /** 
    * @var float 
    * @Assert\NotBlank() 
    * @Assert\Type(type="float") 
    * @ORM\Column(name="precio", type="float") 
    */ 
    private $precio; 

    /** 
    * @var \DateTime 
    * @Assert\Date() 
    * @Assert\NotBlank() 
    * @ORM\Column(name="fecha", type="date") 
    */ 
    private $fecha; 

    /** 
    * @var \DateTime 
    * @Assert\DateTime() 
    * @Assert\NotBlank() 
    * @ORM\Column(name="fechacomprar", type="datetime") 
    */ 
    private $fechacomprar; 


    /** 
    * @var \DateTime 
    * @Assert\DateTime() 
    * @Assert\NotBlank() 
    * @ORM\Column(name="fechavence", type="datetime") 
    */ 
    private $fechavence; 

    /** 
    * @var \Doctrine\Common\Collections\Collection 
    * 
    * @ORM\ManyToMany(targetEntity="Alimento", inversedBy="menu") 
    * @ORM\JoinTable(name="alimento_menu", 
    * joinColumns={ 
    *  @ORM\JoinColumn(name="menu_id", referencedColumnName="id") 
    * }, 
    * inverseJoinColumns={ 
    *  @ORM\JoinColumn(name="alimento_id", referencedColumnName="id") 
    * } 
    *) 
    */ 
    private $alimento; 

    /** 
    * Constructor 
    */ 
    public function __construct() 
    { 
     $this->alimento = new ArrayCollection(); 
    } 


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

    /** 
    * Set precio 
    * 
    * @param float $precio 
    * 
    * @return Menu 
    */ 
    public function setPrecio($precio) 
    { 
     $this->precio = $precio; 

     return $this; 
    } 

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

    /** 
    * Set fecha 
    * 
    * @param \DateTime $fecha 
    * 
    * @return Menu 
    */ 
    public function setFecha($fecha) 
    { 
     $this->fecha = $fecha; 

     return $this; 
    } 

    /** 
    * Get fecha 
    * 
    * @return \DateTime 
    */ 
    public function getFecha() 
    { 
     return $this->fecha; 
    } 

    /** 
    * Set fechacomprar 
    * 
    * @param \DateTime $fechacomprar 
    * 
    * @return Menu 
    */ 
    public function setFechacomprar($fechacomprar) 
    { 
     $this->fechacomprar = $fechacomprar; 

     return $this; 
    } 

    /** 
    * Get fechacomprar 
    * 
    * @return \DateTime 
    */ 
    public function getFechacomprar() 
    { 
     return $this->fechacomprar; 
    } 

    /** 
    * Set fechavence 
    * 
    * @param \DateTime $fechavence 
    * 
    * @return Menu 
    */ 
    public function setFechavence($fechavence) 
    { 
     $this->fechavence = $fechavence; 

     return $this; 
    } 

    /** 
    * Get fechavence 
    * 
    * @return \DateTime 
    */ 
    public function getFechavence() 
    { 
     return $this->fechavence; 
    } 


    /** 
    * Add alimento 
    * 
    * @param \AdminBundle\Entity\Alimento $alimento 
    * 
    * @return Menu 
    */ 
    public function addAlimento(Alimento $alimento) 
    { 
     $this->alimento[] = $alimento; 

     return $this; 
    } 

    /** 
    * Remove alimento 
    * 
    * @param \AdminBundle\Entity\Alimento $alimento 
    */ 
    public function removeAlimento(Alimento $alimento) 
    { 
     $this->alimento->removeElement($alimento); 
    } 

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


<?php 

namespace AdminBundle\Entity; 

use Doctrine\Common\Collections\ArrayCollection; 
use Doctrine\ORM\Mapping as ORM; 
use Symfony\Component\Validator\Constraints as Assert; 
use Symfony\Bridge\Doctrine\Validator\Constraints as DoctrineAssert; 


/** 
* Alimento 
* 
* @ORM\Table(name="alimento") 
* @ORM\Entity(repositoryClass="AdminBundle\Repository\AlimentoRepository") 
* @DoctrineAssert\UniqueEntity("nombre") 
*/ 
class Alimento 
{ 
    /** 
    * @var int 
    * 
    * @ORM\Column(name="id", type="integer") 
    * @ORM\Id 
    * @ORM\GeneratedValue(strategy="AUTO") 
    */ 
    private $id; 

    /** 
    * @var string 
    * @Assert\NotBlank(message="Debe escribir un alimento") 
    * @Assert\Type(type="string") 
    * @Assert\Regex(pattern="/\d/", match=false, message="Debe escribir un nombre válido") 
    * @ORM\Column(name="nombre", type="string", length=255, unique=true) 
    */ 
    private $nombre; 

    /** 
    * @var float 
    * @Assert\NotBlank(message="Debe especificar un precio") 
    * @Assert\Type(type="float", message="Debe escribir un precio válido") 
    * @ORM\Column(name="precio", type="float") 
    */ 
    private $precio; 


    /** 
    * @ORM\ManyToOne(targetEntity="TipoAlimento") 
    * @Assert\NotBlank() 
    */ 
    private $tipo; 

    /** 
    * @var \Doctrine\Common\Collections\Collection 
    * 
    * @ORM\ManyToMany(targetEntity="Menu", mappedBy="alimento") 
    */ 
    private $menu; 

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

    /** 
    * Set nombre 
    * 
    * @param string $nombre 
    * 
    * @return Alimento 
    */ 
    public function setNombre($nombre) 
    { 
     $this->nombre = $nombre; 

     return $this; 
    } 

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

    /** 
    * Set precio 
    * 
    * @param float $precio 
    * 
    * @return Alimento 
    */ 
    public function setPrecio($precio) 
    { 
     $this->precio = $precio; 

     return $this; 
    } 

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

    /** 
    * Set tipo 
    * 
    * @param \AdminBundle\Entity\TipoAlimento $tipo 
    * 
    * @return Alimento 
    */ 
    public function setTipo(TipoAlimento $tipo) 
    { 
     $this->tipo = $tipo; 

     return $this; 
    } 

    /** 
    * Get tipo 
    * 
    * @return \AdminBundle\Entity\TipoAlimento 
    */ 
    public function getTipo() 
    { 
     return $this->tipo; 
    } 

    /** 
    * @return string 
    */ 
    function __toString() 
    { 
     return $this->getNombre(); 
    } 

    /** 
    * Constructor 
    */ 
    public function __construct() 
    { 
     $this->menu = new ArrayCollection(); 
    } 

    /** 
    * Add menu 
    * 
    * @param \AdminBundle\Entity\Menu $menu 
    * 
    * @return Alimento 
    */ 
    public function addMenu(Menu $menu) 
    { 
     $this->menu[] = $menu; 

     return $this; 
    } 

    /** 
    * Remove menu 
    * 
    * @param \AdminBundle\Entity\Menu $menu 
    */ 
    public function removeMenu(Menu $menu) 
    { 
     $this->menu->removeElement($menu); 
    } 

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

컨트롤러 :

그리고 10 템플릿에이 같은 일을하고있는 중이 야 : 여기

{% for m in menu %} 
        <tr> 
         <td>{{ m.id }}</td> 
         <td>{{ m.fecha | date('d/m/y') }}</td> 
         <td>{{ m.alimento.nombre }}</td> 
         <td>{{ m.fechacomprar | date('d/m/y') }}</td> 
         <td>{{ m.fechavence | date('d/m/y') }}</td> 
         <td>{{ m.precio }}</td> 
        </tr> 
       {% endfor %} 

문제가 m.alimento.nombre 것은 잘못입니다! 그것은 많은에 많은 만약

+2

m.alimento는 모음입니다. m.alimento % {{alimento.nombre}} {% endfor %} –

+1

에있는 alimento에 대해 {%를 시도해보십시오. 오류 메시지가 표시됩니다. – habibun

답변

1

단순히 당신이 시도 할 수 있습니다,이 경우, m.alimento는 각 이상의 루프 그래서 각 메뉴는 많은 ALIMENTOS있을 것이라는 점을 의미한다 음식의 __toString

{{ m.alimento|join(',') }} 
1

를 반환합니다 그들의.

{% for a in m.alimento %} 
<td>{{ a.nombre }}</td> 
{% endfor %} 
+0

Owww, 나는 이것을 믿을 수 없다. 나는이 문제를 이틀 동안 해왔다. 그리고 바로 지금 고쳐졌다, 고마워, 정말 고마워! .....이 웹은 최고야! –

+0

당신은 환영합니다 – Carlos