简体   繁体   English

如何从Twig模板中的相关对象输出数据?

[英]How do I output data from a related object in a Twig template?

I'm still very new to Symfony2 so please go easy on me. 我对Symfony2还很新,所以请放轻松我。 I'm trying to loop through a table of flights (for a flight ticket booking system), which have several related fields, such as airline, and airport. 我正试图通过一个航班表(用于机票预订系统),该表有几个相关领域,如航空公司和机场。 I'm using the following method in my custom repository: 我在自定义存储库中使用以下方法:

public function getAllFlights($limit = 100)
{
    $dql = 'SELECT f FROM Flightcase\BookingBundle\Entity\Flight f';

    $query = $this->getEntityManager()->createQuery($dql);
    $query->setMaxResults($limit);

    return $query->getResult();
}

and the getAllFlights() is being passed to my Twig template like so: 并将getAllFlights()传递给我的Twig模板,如下所示:

$flights = $em->getRepository('FlightcaseBookingBundle:Flight')->getAllFlights();   

return $this->render('FlightcaseBookingBundle:Flight:list.html.twig', array('flights' => $flights));

And the Twig template is simply looping through the items inside the $flights collection like this: 而Twig模板只是循环遍历$ flights集合中的项目,如下所示:

{% for flight in flights %}
<tr>
<td>{{ flight.airline }}</td>
<td>{{ flight.origin }}</td>
<td>{{ flight.destination }}</td>
<td>{{ flight.dateFrom }}</td>
<td>{{ flight.timeFrom }}</td>
<td>{{ flight.dateTo }}</td>
<td>{{ flight.timeTo }}</td>
</tr>
{% endfor %}

But I get a ugly, cryptic exception telling me "Object of class Proxies\\FlightcaseBookingBundleEntityAirlineProxy could not be converted to string" which leads me to believe I need to fetch a specific property inside the Airline object such as the IATA code to output as string. 但是我得到一个丑陋的,神秘的异常告诉我“类代理\\ FlightcaseBookingBundleEntityAirlineProxy的对象无法转换为字符串”这使我相信我需要获取Airline对象中的特定属性,例如IATA代码以字符串形式输出。 But how can I access the $airline->getIataCode() inside the Twig template? 但是如何在Twig模板中访问$ airline-> getIataCode()? Or is there a way in my repository to convert the related objects into strings? 或者我的存储库中有一种方法可以将相关对象转换为字符串吗?

I am assuming that Airline is a separate entity, which has an association to the Flight entity in Doctrine. 我假设航空公司是一个独立的实体,它与Doctrine中的Flight实体有关联。 Something like: 就像是:

class Airline
{
    private $id;
    private $name;
    private $flights;
    ...
}

Is that correct? 那是对的吗? If so, then that's the reason you're seeing that specific error. 如果是这样,那就是你看到那个特定错误的原因。 You're giving Twig an object, and telling it to print it out... but what does that mean, exactly? 你给了Twig一个对象,然后告诉它打印出来......但这究竟是什么意思呢?

Let's assume that your class looks like the above, and you're just trying to print out the name of the Airline. 我们假设您的班级如上所示,而您只是想打印出航空公司的名称。

You could do one of two things: 你可以做两件事之一:

First, you could give your object a toString() method: 首先,您可以为对象提供toString()方法:

class Airline
    {
        public function toString()
        {
            return $this->getName();
        }
    }

Alternatively, you can give Twig something scalar to work with: Replace {{ flight.airline }} with {{ flight.airline.name }} . 或者,您可以为Twig提供标量{{ flight.airline }} :将{{ flight.airline }}替换为{{ flight.airline.name }}

Edit: 编辑:

Just saw that your Airline object has a property called $IataCode. 刚看到你的Airline对象有一个名为$ IataCode的属性。 In that case, you'd render it in Twig using {{ flight.airline.IataCode }} . 在这种情况下,您可以使用{{ flight.airline.IataCode }}在Twig中渲染它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM