学说中的多对多关系

时间:2013-07-25 09:56:30

标签: symfony doctrine-orm doctrine many-to-many dql

我有many-to-many关系。实体是ActorPys(电影和电视节目)。当我想拍摄电影时,这种关系很好(让一部电影中的所有演员和女演员都回归)但问题出现的时候我想要回复演员的所有电影。

这是phpmyadmin中的关系:

enter image description here

这是出现的错误:

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>
(...)

1 个答案:

答案 0 :(得分:1)

您正在从

获取学说集
$em->getRepository('ActorBundle:Actor')->findPeliculas($actId);

所以在Twig中,peliculas也是一个集合,如果您只想要第一个元素,请执行for..inpeliculas[0]

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