Nunuz
Nunuz

Reputation: 47

Symfony2: how to show valuables from different tables?

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.

Upvotes: 0

Views: 47

Answers (2)

Emanuel Oster
Emanuel Oster

Reputation: 1316

You have a mistake in your Genre class. At least in your example, you call it Genere instead of Genre. Second, if a movie should have more than one genre then you should make a many-to-many association, as multiple movies can have the same genre. If a movie can only have one genre, then you have to switch the owning side with Genre (Movie should have Genre (singular), Genre should have Movies (plural))

Upvotes: 1

Alok Patel
Alok Patel

Reputation: 8032

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.

Upvotes: 2

Related Questions