如何从一个对象获取相关图像

时间:2014-12-24 13:41:31

标签: symfony doctrine-orm twig

我有一对多的关系。一个景点可以有许多吸引力的图像。吸引力 - 表格中有来自桌子吸引力的外键ID。

我尝试使用以下控制器功能获取图像:

public function attractionIndexAction( $id )
{
    $attractionRepo = $this->getDoctrine()
        ->getRepository( "RowocoAllgemeinBundle:Attraction")
        ->find( $id );

    $attractionImagesRepo = $this->getDoctrine()
        ->getRepository( "RowocoAllgemeinBundle:AttractionImages" )
        ->find( $attractionRepo );

    return $this->render("RowocoAllgemeinBundle:Attraction:attraction.html.twig",
            array(
                "attraction"        => $attractionRepo,
                "attractionImages"  => $attractionImagesRepo
            )
    );
}

在html.twig中,我尝试查看结果:

{% for image in attraction.attractionImages %}
<li data-thumb="/attractionimages/{{ image.folder }}/thumb/{{ image.filename }}">
    <img class="img-responsive" src="/attractionimages/{{ image.folder }}/{{ image.filename }}" />
</li>
{% endfor %}

Repo有结果,但我无法在树枝文件中查看它。

这是教义xml:

<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping http://doctrine-project.org/schemas/orm/doctrine-mapping.xsd">
  <entity name="Rowoco\AllgemeinBundle\Entity\AttractionImages" table="attraction_images">
    <indexes>
      <index name="ait_ai_fk1_idx" columns="id_attraction_imagestype"/>
      <index name="a_ai_fk2_idx" columns="id_attraction"/>
    </indexes>
    <id name="id" type="integer" column="id">
      <generator strategy="IDENTITY"/>
    </id>
    <field name="folder" type="string" column="folder" length="155" nullable="false"/>
    <field name="filename" type="string" column="filename" length="255" nullable="false"/>
    <field name="locked" type="integer" column="locked" nullable="true"/>
    <field name="proofed" type="integer" column="proofed" nullable="true"/>
    <field name="addate" type="datetime" column="addate" nullable="true"/>
    <field name="slider" type="integer" column="slider" nullable="true"/>
    <many-to-one field="idAttraction" target-entity="Attraction">
      <join-columns>
        <join-column name="id_attraction" referenced-column-name="id"/>
      </join-columns>
    </many-to-one>
    <many-to-one field="idAttractionImagestype" target-entity="AttractionImagestype">
      <join-columns>
        <join-column name="id_attraction_imagestype" referenced-column-name="id"/>
      </join-columns>
    </many-to-one>
  </entity>
</doctrine-mapping>

1 个答案:

答案 0 :(得分:3)

我猜您的图片不在attraction中,因为您使用数组键attractionImages将其作为单独的对象传递。尝试:

{% for image in attractionImages %}
<li data-thumb="/attractionimages/{{ image.folder }}/thumb/{{ image.filename }}">
    <img class="img-responsive" src="/attractionimages/{{ image.folder }}/{{ image.filename }}" />
</li>
{% endfor %}