访问twig变量对象数组

时间:2014-01-23 19:03:59

标签: php symfony twig

我有一个twig变量,它包含:

object(Video\VideoBundle\Entity\Relacionado)[366]
  protected 'video' => 
    object(Video\VideoBundle\Entity\Video)[339]
      private 'id' => int 1
  **protected 'categoria' => 
    object(Proxies\__CG__\Video\VideoBundle\Entity\Categoria)[397]
      public '__initializer__' => 
        object(Closure)[388]
      public '__cloner__' => 
        object(Closure)[389]
      public '__isInitialized__' => boolean true
      private 'id' (Video\VideoBundle\Entity\Categoria) => int 1**

我想访问TWIG中的类别ID变量

我的枝条文件:

<div class="Descripcion">
    <h1> {{ video.name }} ( {{ video.duracion}} min ) </h1>

                        {% for tag in tags %}

                             {{ dump(tag) }}

                        {% endfor %}
    </div>

和mycontroller渲染:

$tagvideo = $em->getRepository('VideoBundle:Relacionado')->findCategoriaVideo($video->getId());

return $this->render('VideoBundle:Default:video.html.twig', array('video' => $video, 'relacionados' => $relacionados, 'categorias' => $categorias ,'tags' => $tagvideo));

和我的查询dql

public function findCategoriaVideo($idvideo)
    {
        $em = $this->getEntityManager();
        $dql = 'SELECT c,r FROM VideoBundle:Relacionado r INNER JOIN r.video c WHERE r.video  = :id';
        $consulta = $em->createQuery($dql);
        $consulta->setParameter('id',$idvideo);
        return $consulta->getResult();

    }

我认为我错误地使用了我的联系:

/**
 * Relacionado
 *
 * @ORM\Table()
 * @ORM\Entity
 * @ORM\Entity(repositoryClass="Video\VideoBundle\Entity\RelacionadoRepository")
 */

class Relacionado
{
    /**
    * @ORM\Id
    * @ORM\ManyToOne(targetEntity="Video\VideoBundle\Entity\Video")
    */
    protected $video;
    /**
    * @ORM\Id
    * @ORM\ManyToOne (targetEntity = "Video\VideoBundle\Entity\Categoria")
    */
    protected $categoria;


class Categoria
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     * @ORM\OneToMany (targetEntity = "Video\VideoBundle\Entity\Relacionado", mappedBy = "Categoria")
 */
    private $id;

class Video
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")

     */
    private $id;

在表格Relacionado中,我将视频与每个视频相关联的类别相关联,

但我不知道因为结果视频是正确的但是类别号! :(

我无法访问实体Categoria,因为它未正确映射:

protected 'categoria' => 
    object(Proxies\__CG__\Video\VideoBundle\Entity\Categoria)[397]

3 个答案:

答案 0 :(得分:0)

如果您的twig变量的名称是实体,则可以使用以下命令访问该类别:

 {{ entity.getCategory }}

答案 1 :(得分:0)

未找到:

{{ dump(tag.getCategoria) }}

or

tag.categoria.id

任何发现!!!

ContextErrorException: Notice: Undefined property: Video\VideoBundle\Entity\Relacionado::$Categoria in C:\wamp\www\video\src\Video\VideoBundle\Entity\Relacionado.php line 45

但我在我的实体Relacionado中定义了这个属性

class Relacionado
{
    /**
    * @ORM\Id
    * @ORM\ManyToOne(targetEntity="Video\VideoBundle\Entity\Video")
    */
    protected $video;
    /**
    * @ORM\Id
    * @ORM\ManyToOne(targetEntity="Video\VideoBundle\Entity\Categoria")
    */
    protected $categoria;

我认为问题出在:

*protected 'categoria' => 
    object(Proxies\__CG__\Video\VideoBundle\Entity\Categoria)[397]

为什么要使用Proxies__CG__?¿????

答案 2 :(得分:0)

我已经解决了我的问题! 问题是咨询:

public function findCategoriaVideo($idvideo)
    {
        $em = $this->getEntityManager();
        $dql = 'SELECT r,c FROM VideoBundle:Relacionado r JOIN r.categoria c WHERE r.video  = :id';
        $consulta = $em->createQuery($dql);
        $consulta->setParameter('id',$idvideo);
        //$consulta->setMaxResults(8);
       return $consulta->getResult();

    }

和我的班级:

class Relacionado
{

    /**
     * @ORM\Id
     * @ORM\ManyToOne(targetEntity = "Video\VideoBundle\Entity\Categoria")
     * @ORM\JoinColumn(name="categoria_id", referencedColumnName="id")
     */
    protected $categoria;

    /**
    * @ORM\Id
     * @ORM\ManyToOne (targetEntity = "Video\VideoBundle\Entity\Video")
     * @ORM\JoinColumn(name="video_id", referencedColumnName="id")
   **/
    protected $video;


class Categoria
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     * @ORM\ManyToOne(targetEntity="Video\VideoBundle\Entity\Relacionado")
     * @ORM\JoinColumn(name="id", referencedColumnName="categoria_id")
     *
     */

使用asossiantions实体我可以访问我的Categoria对象。

非常感谢所有