12

I'm having this issue when I upload the files to the server.

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

My Category Class:

    <?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';
  }
}

This is how I'm building the form

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 - ') );
}

I used to have

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

instead of this

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

but when I try that, I get [Semantical Error] line 0, col 108 near 'order_cat AS': Error: Class Prizes\PrizesBundle\Entity\Category has no field or association named order_cat

What am I doing wrong? How can I fix this? I already deleted the cache and verified the files.

EDIT: I need an answer to both errors.

Splendonia
  • 1,329
  • 3
  • 37
  • 59

3 Answers3

12

You code looks ok. You could try this:

Field named order_cat is private so it might be that due to reflection's limitation this field cannot be accessed directly but only via getters/setters.

Have you tried ORDER BY cat.orderCat instead?

Jovan Perovic
  • 19,846
  • 5
  • 44
  • 85
  • I have changed it and yes, it works thank you. But the first issue is still there – Splendonia Jul 26 '13 at 20:37
  • OK, but I am not sure to which other error are you referring to. You only mentioned the one with `order_cat` ordering... – Jovan Perovic Jul 26 '13 at 21:11
  • Hi, the first is this: Error: Class Prizes\PrizesBundle\Entity\Category has no field or association named order_cat the second is at the bottom : [Semantical Error] line 0, col 108 near 'order_cat AS': Error: Class Prizes\PrizesBundle\Entity\Category has no field or association named order_cat, your answer helped me with the second one, however the first one remains – Splendonia Jul 27 '13 at 22:12
  • I'm sorry - I haven't had a chance to answer your comment. Can you please tell me what was the issue after all? – Jovan Perovic Jul 29 '13 at 21:23
0

Try changing to reflect the getter:

WHERE cat.status = 1 ORDER BY cat.OrderCat
watermanio
  • 235
  • 1
  • 11
0

If after checking your code, you still get this error, maybe the problem is in generated proxy classes from Doctrine. Or if you use APC cache, you have to clear it, in order to use the newly generated proxy classes.

tomazahlin
  • 2,137
  • 1
  • 24
  • 29