1

我有many-to-many关系。实体是ActorPys(电影和电视节目)。当我想要电影演员时,关系工作正常(返回电影的所有演员和女演员)但是当我想检索演员的所有电影时问题就来了。

这是 phpmyadmin 中的关系:

在此处输入图像描述

这是出现的错误:

Key "actor" for array with keys "0" does not exist in ActorBundle:Default:actor.html.twig at line 3

这里是必要的代码:

演员的DefaultController

class DefaultController extends Controller
{
    public function peliculasAction($actId)
    {
        $em = $this->getDoctrine()->getManager();

        $peliculas = $em->getRepository('ActorBundle:Actor')->findPeliculas($actId);

        return $this->render('ActorBundle:Default:actor.html.twig', array('peliculas' => $peliculas
                ));
    }
}

ActorRepository

class ActorRepository extends EntityRepository
{
    public function findPeliculas($actId)
    {
        $em = $this->getEntityManager();

        $consulta = $em->createQuery('
            SELECT p, d, a
            FROM ActorBundle:Actor a
            JOIN a.pys p JOIN p.director d
            WHERE a.actId = :actId
            ORDER BY p.pysAnyo DESC
        ');

        $consulta->setParameter('actId', $actId);

        return $consulta->setHint(\Doctrine\ORM\Query::HINT_CUSTOM_OUTPUT_WALKER, 'Gedmo\\Translatable\\Query\\TreeWalker\\TranslationWalker')->getResult();
    }
}

actor.html.twig

(...)
{% block title %}{{ peliculas.actor.actNombre ~ ' ' ~ peliculas.actor.actApellidos }}{% endblock %}

{% block article %}
    <section class="article">
        <div id="datos-actor-director-genero">
            <strong>{{ 'Nombre:' | trans }} </strong>{{ peliculas.actor.actNombre ~ ' ' ~ peliculas.actor.actApellidos }}</br>
            <strong>{{ 'Fecha de nacimiento:' | trans }} </strong>{{ peliculas.actor.actFechaNacimiento | localizeddate('long', 'none') }}
        </div>
        <table>
            {% for pys in peliculas %}
                <tr id="fechaVotacion">
                    <td colspan="4">{{ pys.pysAnyo }}</td>
(...)
4

1 回答 1

1

您正在从

$em->getRepository('ActorBundle:Actor')->findPeliculas($actId);

所以在 Twig 中,peliculas也是一个集合,如果你只想要第一个元素,就做for..in一个peliculas[0]

peliculas没有键actor,只有数字(0:第一个演员,1:第二个演员,等等......)

于 2013-07-25T10:05:54.833 回答