10

当我将文件上传到服务器时,我遇到了这个问题。

Error: Class Prizes\PrizesBundle\Entity\Category has no field or association named order_cat

我的类别类:

    <?php

namespace Prizes\PrizesBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;

/**
 * @ORM\Entity 
 * @ORM\Table(name="category")
 */
class Category
{
  /**
   * @ORM\Id
   * @ORM\Column(type="integer")
   * @ORM\GeneratedValue(strategy="AUTO")
   */
  private $id;

  /**
   * @Gedmo\Translatable
   * @ORM\Column(type="string", length=45, nullable=false)
   */
  private $name;

  /**
   * @Gedmo\Translatable
   * @ORM\Column(type="string", length=45, nullable=false)
   */
  private $description;

  /**
   * @ORM\Column(type="string", length=255, nullable=false)
   */
  private $thumb;

  /**
   * @ORM\Column(type="string", length=255, nullable=false)
   */
  private $img;

  /**
   * @ORM\ManyToOne(targetEntity="Optime\AppStatusBundle\Entity\Status")
   * @ORM\JoinColumn(name="status", referencedColumnName="id")
   */
  private $status;

  /**
   * @Gedmo\Timestampable(on="create")
   * @ORM\Column(type="datetime")
   */
  private $created;

  /**
   * @Gedmo\Timestampable(on="update")
   * @ORM\Column(type="datetime")
   */
  private $modified;

  /**
   * @ORM\ManyToMany(targetEntity="Prize")
   * @ORM\JoinTable(name="prize_has_category",
   *      joinColumns={@ORM\JoinColumn(name="category", referencedColumnName="id")},
   *      inverseJoinColumns={@ORM\JoinColumn(name="prize", referencedColumnName="id")}
   *      )
   */
  private $prizes;

  /**
   * @Gedmo\Locale
   * Used locale to override Translation listener`s locale
   * this is not a mapped field of entity metadata, just a simple property
   */
  private $locale;
  /**
   * @Gedmo\Translatable
   * @ORM\Column(type="string", length=45, nullable=false)
   */
  private $order_cat;
  //    /**
  //     * @Gedmo\TreeLeft
  //     * @ORM\Column(name="lft", type="integer")
  //     */
  //    private $lft;

  //    /**
  //     * @Gedmo\TreeLevel
  //     * @ORM\Column(name="lvl", type="integer")
  //     */
  //    private $lvl;

  //    /**
  //     * @Gedmo\TreeRight
  //     * @ORM\Column(name="rgt", type="integer")
  //     */
  //    private $rgt;

  //    /**
  //     * @Gedmo\TreeRoot
  //     * @ORM\Column(name="root", type="integer", nullable=true)
  //     */
  //    private $root;

  //    /**
  //     * @Gedmo\TreeParent
  //     * @ORM\ManyToOne(targetEntity="Category", inversedBy="children")
  //     * @ORM\JoinColumn(name="parent_id", referencedColumnName="id", onDelete="SET NULL")
  //     */
  //    private $parent;

  //    /**
  //     * @ORM\OneToMany(targetEntity="Category", mappedBy="parent")
  //     * @ORM\OrderBy({"lft" = "ASC"})
  //     */
  //    private $children;

  public function __construct( )
  {
    $this->prizes = new \Doctrine\Common\Collections\ArrayCollection( );
  }

  public function getId( )
  {
    return $this->id;
  }

  public function setName( $name )
  {
    $this->name = $name;
  }

  public function getName( )
  {
    return $this->name;
  }

  public function setDescription( $description )
  {
    $this->description = $description;
  }

  public function getDescription( )
  {
    return $this->description;
  }

  public function setThumb( $thumb )
  {
    $this->thumb = $thumb;
  }

  public function getThumb( )
  {
    return $this->thumb;
  }

  public function setImg( $img )
  {
    $this->img = $img;
  }

  public function getImg( )
  {
    return $this->img;
  }

  public function setStatus( \Optime\AppStatusBundle\Entity\Status $status )
  {
    $this->status = $status;
  }

  public function getStatus( )
  {
    return $this->status;
  }

  public function getCreated( )
  {
    return $this->created;
  }

  public function getModified( )
  {
    return $this->modified;
  }

  public function getPrizes( )
  {
    return $this->prizes;
  }

  public function getOrderCat(){
      return $this->order_cat;
  }

  public function setOrderCat($order_cat){
      $this->order_cat = $order_cat;
  }

  static public function getListDQL( )
  {
    return "SELECT cat FROM " . Category::getFQCN( ) . " cat
                        WHERE cat.status = 1";
  }

  static public function getFQCN( )
  {
    return 'Prizes\PrizesBundle\Entity\Category';
  }
}

这就是我构建表单的方式

public function buildForm( FormBuilder $builder, array $options )
{
    $query = new QueryBuilder( $this->em);
    $query->addSelect( 's' )->from( Status::getFQCN( ), 's' )->join( 's.status_entity', 'se' )->where( "se.name = 'Prize'" );
    $builder->add( 'status', 'entity', array ( 'class' => 'AppStatusBundle:Status', 'property' => 'name', 'query_builder' => $query, "required" => false, 'empty_value' => ' - SELECT - ') );
    $builder->add( 'name', 'text', array ( "required" => false) );
    $builder->add( 'country', 'entity',array ('class' => 'CSCBundle:SystemCountry', 'property' => 'country.name', "required" => false, 'empty_value' => ' - SELECT - ') );
    $builder->add( 'category', 'entity', array ('class' => 'PrizesBundle:Category', 'property' => 'name', "required" => false, 'empty_value' => ' - SELECT - ', 'query_builder' => 
                function(EntityRepository $er) {
                        return $er->createQueryBuilder('c')->join( 'c.status', 's' )->where( "s.name = 'Alive'")
                            ->orderBy('c.order_cat', 'ASC');
                },) );
    $builder->add('brand', 'entity', array ( 'class' => 'PrizesBundle:Brand', 'property' => 'name', "required" => false, 'empty_value' => ' - SELECT - ') );
}

我曾经有

return "SELECT cat FROM " . Category::getFQCN( ) . " cat
                        WHERE cat.status = 1 ORDER BY cat.order_cat"

而不是这个

SELECT cat FROM " . Category::getFQCN( ) . " cat
                        WHERE cat.status = 1

但是当我尝试时,我得到[Semantical Error] line 0, col 108 near 'order_cat AS': Error: Class Prizes\PrizesBundle\Entity\Category has no field or association named order_cat

我究竟做错了什么?我怎样才能解决这个问题?我已经删除了缓存并验证了文件。

编辑:我需要两个错误的答案。

4

3 回答 3

12

你的代码看起来不错。你可以试试这个:

命名order_cat的字段private可能是由于反射的限制,该字段不能直接访问,只能通过 getter/setter 访问。

你试过ORDER BY cat.orderCat吗?

于 2013-07-26T19:44:16.303 回答
0

尝试更改以反映吸气剂:

WHERE cat.status = 1 ORDER BY cat.OrderCat
于 2013-07-26T20:10:56.937 回答
0

如果在检查您的代码后,您仍然收到此错误,则问题可能出在 Doctrine 生成的代理类中。或者,如果您使用 APC 缓存,则必须清除它,才能使用新生成的代理类。

于 2018-01-30T16:42:47.263 回答