Symfony2:如何从数组中获取值并将其打印到twing文件中

时间:2014-03-05 15:11:56

标签: php symfony

我是Symfony的新手。我目前正在使用symfony2.4。

我卡住的地方 -

我有一个场景。其中我必须在twing文件中显示简单列表。数据来自数据库。这些数据来自两个连接表。

第一个表是团队

ID

名称

昵称

COUNTRY_ID

第二个表是国家/地区

ID

名称

我想打印这样的数据

     Name     | Nickname | Country(name)

Mumbai Indians| MI       | India
New Royals    | NR       | India

所以我在我的文件中做了什么:

<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 %}

所以国家/地区无效

这是我的控制器代码

$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);

修改

按照要求Teams.php(实体)

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(实体)

命名空间Admin \ SporteventsBundle \ Entity;

将Doctrine \ ORM \ Mapping用作ORM;

/ **  *国家  *  * @ORM \ Table(name =“countries”,uniqueConstraints = {@ ORM \ UniqueConstraint(name =“uq_countries_name”,columns = {“name”})})  * @ORM \ Entity   / 国家 {     / *      * @var整数      *      * @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;
}

}

5 个答案:

答案 0 :(得分:1)

假设您已正确设置关系,则需要执行{{ team.country.name }}

已更新以反映实体代码。

答案 1 :(得分:0)

在连接列注释中,团队实体中有错误。 Referencedcolumn一般是id(你的国家主键是什么)。然后尝试访问这样的国家

假设你已经正确地建立了你的人际关系,你需要在树枝上做{{team.country.name}}

答案 2 :(得分:0)

这部分:

/**
 * @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\Countries
 *
 * @ORM\ManyToOne(targetEntity="Admin\SporteventsBundle\Entity\Countries")
 * @ORM\JoinColumns({
 *   @ORM\JoinColumn(name="country_id", referencedColumnName="id")
 * })
 */
private $country;

referencedColumnName应该是id,而不是country_id

通过执行上述操作,您将正确创建关系,然后在twig文件中,您可以使用:

{{ team.country.name }}

希望这有帮助:)

答案 3 :(得分:0)

在Teams Entity中,在$ country字段中,尝试使用以下内容设置关系:

/**
 * @var \Admin\SporteventsBundle\Entity\Countries
 *
 * @ORM\ManyToOne(targetEntity="Admin\SporteventsBundle\Entity\Countries")
 * @ORM\JoinColumn(name="country_id", referencedColumnName="country_id")
 */
private $country;

然后您可以通过在树枝中键入以下内容来访问您所在国家/地区的名称:

{{ team.country.name }}

答案 4 :(得分:0)

也许它是一个阵列,你必须扫描它?

&#13;
&#13;
{% for country in team.country %}
  {{ country.name }}
{% endfor %}
&#13;
&#13;
&#13;