dsa45132 2016-06-22 13:59
浏览 6
已采纳

Symfony2:如何显示来自不同表格的贵重物品?

I'm trying to create an website in Symfony in which I have a database with Movies. This how it looks like:

/**
 * Game entity.
 */

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Class Movie.
 *
 * @ORM\Table(name="movies")
 * @ORM\Entity(repositoryClass="AppBundle\Repository\Movie")
 */
class Movie
{
/**
 * Id.
 *
 * @ORM\Id
 * @ORM\Column(
 *     type="integer",
 *     nullable=false,
 *     options={
 *         "unsigned" = true
 *     }
 * )
 * @ORM\GeneratedValue(strategy="IDENTITY")
 *
 * @var integer $id
 */
private $id;

/**
 * Title.
 *
 * @ORM\Column(
 *     name="title",
 *     type="string",
 *     length=255,
 *     nullable=false
 * )
 *
 * @var string $title
 */
private $title;

/**
 * Notes.
 *
 * @ORM\Column(
 *     name="notes",
 *     type="text",
 *     nullable=true
 * )
 *
 * @var string $notes
 */
private $notes;

  /**
 * Genres array
 *
 * @ORM\ManyToOne(targetEntity="Genre")
 * @ORM\JoinColumn(name="genre_id", referencedColumnName="id")
 * )
 *
 * @var \Doctrine\Common\Collections\ArrayCollection $genres
 */
   protected $genres;


/**
 * Constructor.
 */
public function __construct()
{
    $this->tags = new \Doctrine\Common\Collections\ArrayCollection();
}

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

/**
 * Set id.
 *
 * @param integer $id
 */
public function setId($id)
{
    $this->id = $id;
}


/**
 * Set title.
 *
 * @param string $title
 */
public function setTitle($title)
{
    $this->title = $title;
}

/**
 * Get title.
 *
 * @return string 
 */
public function getTitle()
{
    return $this->title;
}

/**
 * Set notes.
 *
 * @param string $notes
 */
public function setNotes($notes)
{
    $this->notes = $notes;
}

/**
 * Get notes.
 *
 * @return string 
 */
public function getNotes()
{
    return $this->notes;
}


/**
 * Add genres
 *
 * @param \AppBundle\Entity\Genre $genres
 * @return Movie
 */
public function addGenre(\AppBundle\Entity\Genre $genres)
{
    $this->genres[] = $genres;

    return $this;
}  

/**
 * Set genres
 *
 * @param \AppBundle\Entity\Genre $genres
 * @return Movie
 */
public function setGenres(\AppBundle\Entity\Genre $genres = null)
{
    $this->genres = $genres;

    return $this;
}

/**
 * Get genres
 *
 * @return \AppBundle\Entity\Genre 
 */
public function getGenres()
{
    return $this->genres;
}
}

And each Movie has a Genre:

 /**
 * Genere entity.
 *
 */

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
  * Class Genere.
  *
  * @ORM\Table(name="genere")
  * @ORM\Entity(repositoryClass="AppBundle\Repository\Genere")
  */
 class Genere
{
/**
 * Id.
 *
 * @ORM\Id
 * @ORM\Column(
 *     type="integer",
 *     nullable=false,
 *     options={
 *         "unsigned" = true
 *     }
 * )
 * @ORM\GeneratedValue(strategy="IDENTITY")
 *
 * @var integer $id
 */
private $id;

/**
 * Name.
 *
 * @ORM\Column(
 *     name="name",
 *     type="string",
 *     length=128,
 *     nullable=false
 * )
 *
 * @var string $name
 */
private $name;
/**
 * Get Id.
 *
 * @return integer Result
 */
public function getId()
{
    return $this->id;
}

public function setId($id)
{
    $this->id = $id;
}

/**
 * Set name.
 *
 * @param string $name Name
 */
public function setName($name)
{
    $this->name = $name;
}

/**
 * Get name.
 *
 * @return string Name
 */
public function getName()
{
    return $this->name;
}

Now, my question is: how can I show a genre in movies list? I know how to make a list of movies, but no idea what to use to show genres. My MovieController currently looks like this:

/**
 * List action.
 *
 * @Route("/movie-list", name="movies-list")
 * @Route("/movies-list/")
 *
 * @throws NotFoundHttpException
 * @return Response A Response instance
 */
public function listAction()
{
    $movies = $this->findByStatId(2);
    if (!$movies) {
        throw new NotFoundHttpException('Movies not found!');
    }
    return $this->templating->renderResponse(
        'AppBundle:Movie:index.html.twig',
        array('movies' => $movies)
    );
}    

What should I add to be able to show genres? Where? Any advises would be welcome.

  • 写回答

2条回答 默认 最新

  • dongmiao260399 2016-06-22 14:04
    关注

    You already have genres along with $movies variable. You just have to call access genres like this:

    {% for genre in movies.genres %}
       {# Access each genre variable here #}
    {% endfor %}
    

    movies is a object passed to twig file and it has property genres to access related genres in it.

    To access genres on controller:

    <?php
        $genres=$movies->getGenres();
        // loop through $genres.
    ?>
    

    getGenres() will return collection of genres.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊