简体   繁体   English

在symfony2树枝模板中找不到实体

[英]Entity was not found in symfony2 twig template

In my project I have a table named service_provider.There is a rating system available for each service provider. 在我的项目中,我有一个名为service_provider的表。每个服务提供商都有一个评估系统。

My service provider entity, 我的服务提供商实体,

class ServiceProviders
{
    /**
     * @var integer
     *
     * @ORM\Column(name="service_provider_id", type="integer", nullable=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $serviceProviderId;


     /**
     * @var \Ratings
     *
     * @ORM\OneToOne(targetEntity="Ratings",inversedBy="serviceProviders",cascade={"all"})
     * @ORM\JoinColumns({
     *   @ORM\JoinColumn(name="service_provider_id", referencedColumnName="service_provider_id")
     * })
     */
    private $ratings;
}

My Rating entity, 我的评分实体,

class Ratings
{

    /**
     * @var integer
     *
     * @ORM\Column(name="service_provider_id", type="integer", nullable=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $serviceProviderId;

    /**
     * @var \ServiceProviders
     *
     * @ORM\OneToOne(targetEntity="ServiceProviders",mappedBy="ratings",cascade={"all"})
     * @ORM\JoinColumns({
     *   @ORM\JoinColumn(name="service_provider_id", referencedColumnName="service_provider_id")
     * })
     */
    private $serviceProviders;
}

and in my serviceProviderEntity I wrote a join query to fetch all data. 在我的serviceProviderEntity中,我编写了一个联接查询以获取所有数据。

class ServiceProvidersRepository extends EntityRepository
{
    public function findAllProviders($where,$order,$rowCount,$page)
    {
        $query = $this->createQueryBuilder('s')
                ->select('s')
            ->leftJoin('s.ratings','r')
                    ->where('s.status= :status')
            ->andWhere($where)
                ->setParameter('status', 'A')
            ->orderBy($order)
                    ->setMaxResults($rowCount)
                    ->setFirstResult($offset)
                    ->getQuery();
        $result = $query->getResult();
     }
}

It all works fine,but when there is no data corresponding to a service provider in rating table,An error occurred in twig template 'Entity was not found'.Can any one help me,how can solve this problem. 一切正常,但是当评级表中没有与服务提供者相对应的数据时,树枝模板“未找到实体”中发生了错误。任何人都可以帮助我,如何解决这个问题。

My controller action, 我的控制器动作

 public function providerSearchAction()
 {
       $pagination = $entityManager->getRepository('EvpEventpriceBundle:ServiceProviders')->findAllProviders($where,'s.serviceProviderId',$rowCount,$page);
       return $this->render('EvpEventpriceBundle:ServiceProviders:search.html.twig',array('pagination'=>$pagination));
  }

And my twig template, 还有我的树枝模板

{% for provider in pagination %}
     {% for i in 0..4 %}

    {% if provider.ratings|length %}
    {% if (i<((provider.ratings.totalValue/provider.ratings.totalVotes)|number_format)-0.5) %}
        {% set class = "ratings_stars ratings_vote" %}
    {% else %}
        {% set class = "ratings_stars" %}
    {% endif %}
    {% endif %}
    <div class="star_{{i+1}} {{class}}"></div>
    {% endfor %}

{% endfor %} {%endfor%}

can you change your 4th line of twig with this and try: 您可以以此来更改树枝的第4行并尝试:

{% if provider.ratings is defined and provider.ratings is not empty %}

and add {% set class = "" %} in case that the test don't instantiate class variable 并添加{% set class = "" %}以防测试未实例化类变量

There are a couple of things wrong here: 这里有几处错误:

You are mapping the same database column as a field/column and association in your entity. 您正在将同一数据库列映射为实体中的字段/列关联。 This will make Doctrine very confused! 这会使教义非常困惑! Always choose between a field/column or association (usually you'll want the association). 始终在字段/列关联之间进行选择(通常需要关联)。

Also, you're using @ORM\\JoinColumns where you shouldn't, @ORM\\JoinColumn is enough. 另外,您在不应该使用@ORM\\JoinColumns地方使用@ORM\\JoinColumn就足够了。

Don't use cascade options on both sides of the association, this will drastically impact Doctrine's performance. 不要在关联的两端都使用级联选项,这将严重影响Doctrine的性能。 Furthermore, it feels like you put cascade={"all"} in there because it sounded convenient when you read the docs (no pun intended). 此外,感觉就像您在其中放置了cascade={"all"} ,因为在阅读文档时听起来很方便(没有双关语)。 But you should think very carefully when using cascades. 但是在使用级联时,您应该非常仔细地考虑。 Every cascade has a performance penalty, so my advise is to wait using them until you actually need them. 每个级联都会降低性能,所以我的建议是等到真正需要它们后再使用它们。

nullable=FALSE isn't needed on @Column , nullable isn't allowed by default on fields/columns. nullable=FALSE@Column不需要nullable=FALSE ,默认情况下在字段/列上不允许nullable。 They are however allowed by default on associations. 他们上默认协会然而允许的。

You're using the plural form of names where the singular form is better. 您正在使用名称的复数形式,其中单数形式更好。 For example, your entity ServiceProviders can better be named ServiceProvider , because 1 entity is 1 ServiceProvider (not many ServiceProviders). 例如,最好将您的实体ServiceProviders命名为ServiceProvider ,因为1个实体就是1个ServiceProvider(不是很多ServiceProviders)。

I'm not really sure which side of the association you want to be the owning side. 我真的不知道你想成为拥有方,其关联的一面。 I'll assume you want Rating to be the owning side because you're using service_provider_id as foreign key. 我假设您想让Rating为拥有方,因为您正在使用service_provider_id作为外键。

Here's the fix to the things I mentioned above: 这是我上面提到的事情的解决方法:

/**
 * @ORM\Entity
 */
class ServiceProvider
{

    /**
     * @ORM\Column(type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $id;

    /**
     * @ORM\OneToOne(targetEntity="Rating", mappedBy="serviceProvider")
     */
    private $rating;

}

/**
 * @ORM\Entity
 */
class Rating
{

    /**
     * @ORM\Column(type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $id;

    /**
     * @ORM\OneToOne(targetEntity="Customer", inversedBy="rating")
     * @ORM\JoinColumn(name="service_provider_id", referencedColumnName="id")
     */
    private $serviceProvider;

}

You can (and should) always validate your mappings using the console: Doctrine 2: doctrine orm:validate-schema , Symfony 2: app/console doctrine:schema:validate . 您可以(并且应该)始终使用控制台来验证映射:教义2: doctrine orm:validate-schema ,Symfony 2: app/console doctrine:schema:validate

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

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