Reputation: 3262
I am new to Symfony. I am currently using symfony2.4 .
where I stuck -
I have one scenario. In which I have to show simple listing in twing file. The data comes from database. This data comes from the two joined tables.
First table is Teams
id
name
nickname
country_id
Second table is Country
id
name
I want to print data like this
Name | Nickname | Country(name)
Mumbai Indians| MI | India
New Royals | NR | India
So What I do in my twing file :
<table border=1>
<tr>
<th>Name</th>
<th>Nickname</th>
<th>Country</th>
</tr>
{% for team in teams %}
<tr>
<td>{{ team.name }}</td>
<td>{{ team.nickname }}</td>
<td>{{ team.country }}</td> <<<<<<<<<<<
</tr>
{% endfor %}
So Here country is not working
This is my controller code
$teams = $this->getDoctrine()
->getRepository('AdminSporteventsBundle:Teams')
->findAll();
if (!$teams) {
throw $this->createNotFoundException('No news found');
}
$build['teams'] = $teams;
return $this->render('AdminSporteventsBundle:Teams:teams_list.html.twig', $build);
EDIT
As requested Teams.php (Entity)
namespace Admin\SporteventsBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Teams
*
* @ORM\Table(name="teams", uniqueConstraints={@ORM\UniqueConstraint(name="uq_teams_name_sport_id", columns={"sport_id", "name"})}, indexes={@ORM\Index(name="idx_teams_by_sport_id", columns={"sport_id"}), @ORM\Index(name="idx_teams_by_sport_id_country_id", columns={"sport_id", "country_id"}), @ORM\Index(name="idx_teams_not_deleted", columns={"deleted"}), @ORM\Index(name="IDX_96C22258F92F3E70", columns={"country_id"}), @ORM\Index(name="IDX_96C222581177C375", columns={"information_page_id"}), @ORM\Index(name="IDX_96C222586D947EBB", columns={"logo_image_id"})})
* @ORM\Entity
*/
class Teams
{
/**
* @var integer
*
* @ORM\Column(name="team_id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="SEQUENCE")
* @ORM\SequenceGenerator(sequenceName="teams_team_id_seq", allocationSize=1, initialValue=1)
*/
private $teamId;
/**
* @var string
*
* @ORM\Column(name="name", type="string", nullable=false)
*/
private $name;
/**
* @var boolean
*
* @ORM\Column(name="deleted", type="boolean", nullable=false)
*/
private $deleted;
/**
* @var integer
*
* @ORM\Column(name="editable_pages_page_id", type="integer", nullable=true)
*/
private $editablePagesPageId;
/**
* @var string
*
* @ORM\Column(name="nickname", type="string", nullable=true)
*/
private $nickname;
/**
* @var \Admin\SporteventsBundle\Entity\Countries
*
* @ORM\ManyToOne(targetEntity="Admin\SporteventsBundle\Entity\Countries")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="country_id", referencedColumnName="country_id")
* })
*/
private $country;
/**
* @var \Admin\SporteventsBundle\Entity\InformationPages
*
* @ORM\ManyToOne(targetEntity="Admin\SporteventsBundle\Entity\InformationPages")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="information_page_id", referencedColumnName="information_page_id")
* })
*/
private $informationPage;
/**
* @var \Admin\SporteventsBundle\Entity\Images
*
* @ORM\ManyToOne(targetEntity="Admin\SporteventsBundle\Entity\Images")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="logo_image_id", referencedColumnName="image_id")
* })
*/
private $logoImage;
/**
* @var \Admin\SporteventsBundle\Entity\Sports
*
* @ORM\ManyToOne(targetEntity="Admin\SporteventsBundle\Entity\Sports")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="sport_id", referencedColumnName="sport_id")
* })
*/
private $sport;
/**
* Get teamId
*
* @return integer
*/
public function getTeamId()
{
return $this->teamId;
}
/**
* Set name
*
* @param string $name
* @return Teams
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* Set deleted
*
* @param boolean $deleted
* @return Teams
*/
public function setDeleted($deleted)
{
$this->deleted = $deleted;
return $this;
}
/**
* Get deleted
*
* @return boolean
*/
public function getDeleted()
{
return $this->deleted;
}
/**
* Set editablePagesPageId
*
* @param integer $editablePagesPageId
* @return Teams
*/
public function setEditablePagesPageId($editablePagesPageId)
{
$this->editablePagesPageId = $editablePagesPageId;
return $this;
}
/**
* Get editablePagesPageId
*
* @return integer
*/
public function getEditablePagesPageId()
{
return $this->editablePagesPageId;
}
/**
* Set nickname
*
* @param string $nickname
* @return Teams
*/
public function setNickname($nickname)
{
$this->nickname = $nickname;
return $this;
}
/**
* Get nickname
*
* @return string
*/
public function getNickname()
{
return $this->nickname;
}
/**
* Set country
*
* @param \Admin\SporteventsBundle\Entity\Countries $country
* @return Teams
*/
public function setCountry(\Admin\SporteventsBundle\Entity\Countries $country = null)
{
$this->country = $country;
return $this;
}
/**
* Get country
*
* @return \Admin\SporteventsBundle\Entity\Countries
*/
public function getCountry()
{
return $this->country;
}
/**
* Set informationPage
*
* @param \Admin\SporteventsBundle\Entity\InformationPages $informationPage
* @return Teams
*/
public function setInformationPage(\Admin\SporteventsBundle\Entity\InformationPages $informationPage = null)
{
$this->informationPage = $informationPage;
return $this;
}
/**
* Get informationPage
*
* @return \Admin\SporteventsBundle\Entity\InformationPages
*/
public function getInformationPage()
{
return $this->informationPage;
}
/**
* Set logoImage
*
* @param \Admin\SporteventsBundle\Entity\Images $logoImage
* @return Teams
*/
public function setLogoImage(\Admin\SporteventsBundle\Entity\Images $logoImage = null)
{
$this->logoImage = $logoImage;
return $this;
}
/**
* Get logoImage
*
* @return \Admin\SporteventsBundle\Entity\Images
*/
public function getLogoImage()
{
return $this->logoImage;
}
/**
* Set sport
*
* @param \Admin\SporteventsBundle\Entity\Sports $sport
* @return Teams
*/
public function setSport(\Admin\SporteventsBundle\Entity\Sports $sport = null)
{
$this->sport = $sport;
return $this;
}
/**
* Get sport
*
* @return \Admin\SporteventsBundle\Entity\Sports
*/
public function getSport()
{
return $this->sport;
}
public function __toString()
{
return $this->name;
}
}
EDIT Countries.php (Entity)
namespace Admin\SporteventsBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/** * Countries * * @ORM\Table(name="countries", uniqueConstraints={@ORM\UniqueConstraint(name="uq_countries_name", columns={"name"})}) * @ORM\Entity / class Countries { /* * @var integer * * @ORM\Column(name="country_id", type="integer") * @ORM\Id * @ORM\GeneratedValue(strategy="SEQUENCE") * @ORM\SequenceGenerator(sequenceName="countries_country_id_seq", allocationSize=1, initialValue=1) */ private $countryId;
/**
* @var string
*
* @ORM\Column(name="name", type="string", nullable=false)
*/
private $name;
/**
* Get countryId
*
* @return integer
*/
public function getCountryId()
{
return $this->countryId;
}
/**
* Set name
*
* @param string $name
* @return Countries
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* @return string
*/
public function getName()
{
return $this->name;
}
public function __toString()
{
return $this->name;
}
}
Upvotes: 0
Views: 1068
Reputation: 1330
maybe it's an array , and you must scan it?
{% for country in team.country %}
{{ country.name }}
{% endfor %}
Upvotes: 0
Reputation: 1
In the Teams Entity , at the $country field, try using the following to set the relationship:
/**
* @var \Admin\SporteventsBundle\Entity\Countries
*
* @ORM\ManyToOne(targetEntity="Admin\SporteventsBundle\Entity\Countries")
* @ORM\JoinColumn(name="country_id", referencedColumnName="country_id")
*/
private $country;
and then you can access the name of your country by typing the following in twig:
{{ team.country.name }}
Upvotes: 0
Reputation: 965
Yo have mistake in the teams entity at join column annotation. Referencedcolumn generally is id(what's your country primary key). Then try to access country like this
You need to do {{ team.country.name }} in twig assuming you have set your relationships up proper
Upvotes: 0
Reputation: 84
This part:
/**
* @var \Admin\SporteventsBundle\Entity\Countries
*
* @ORM\ManyToOne(targetEntity="Admin\SporteventsBundle\Entity\Countries")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="country_id", referencedColumnName="country_id")
* })
*/
private $country;
need to be changed to:
/**
* @var \Admin\SporteventsBundle\Entity\Countries
*
* @ORM\ManyToOne(targetEntity="Admin\SporteventsBundle\Entity\Countries")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="country_id", referencedColumnName="id")
* })
*/
private $country;
as referencedColumnName
should be id
, not country_id
By doing as above, you will create the relationship properly then in your twig file, you can use:
{{ team.country.name }}
Hope this help :)
Upvotes: 0
Reputation: 2040
You need to do {{ team.country.name }}
assuming you have set your relationships up properly.
Updated to reflect entity code.
Upvotes: 1