2016-02-19 14 views
-1

實際錯誤對於參考: 屬性「category_id」或方法「getCategoryId()」,「categoryId )「,」isCategoryId()「,」hasCategoryId()「,」__get()「存在並且在類」Pas \ ShopTestBundle \ Entity \ Product「中具有公共訪問權限。錯誤:既不是屬性......也沒有一種方法...存在並且具有公共訪問

我已經完成了我對這個錯誤的研究,但是我找不到它。爲什麼我能夠創造'產品'之前,但現在我不能?我看到getCategoryId方法不存在,但不應該有教條創建它們(因爲我確實與教條建立了關係)。

如果我添加屬性CATEGORY_ID我得到錯誤:

在執行時出現異常 'SELECT t0.id AS ID1,AS category_id2,t0.CategoryName AS CategoryName3 FROM類別T0 t0.category_id':

SQLSTATE [42S22]:未找到列:1054'字段列表'中的未知列't0.category_id'

有人可以告訴我我做錯了什麼嗎?

關係是很多(產品)對一個(類別)。點擊添加新產品時發生錯誤。在此行中... ... <a class="btn btn-primary" href="{{ path('product_new') }}" role="button">Create a New Entry</a>導致這個...

{% extends '::base.html.twig' %} 

{% block body -%} 
    <h1>Product creation</h1> 

    {{ form(form) }} 

     <ul class="record_actions"> 
    <li> 
     <a href="{{ path('product') }}"> 
      Back to the list 
     </a> 
    </li> 
</ul> 
{% endblock %} 

下面是產品型號:

/* If I move the entity and array to category_id, nothing changes. */ 

class ProductType extends AbstractType 
{ 
    /** 
    * @param FormBuilderInterface $builder 
    * @param array $options 
    */ 
    public function buildForm(FormBuilderInterface $builder, array $options) 
    { 
     $builder 
      ->add('name') 
      ->add('price') 
      ->add('quantity', 'integer') 
      ->add('category', 'entity', array('class' => 'PasShopTestBundle:Product', 
               'property' => 'name', 
               'multiple' => 'true')) 
      ->add('category_id') 
     ; 
    } 

    /** 
    * @param OptionsResolverInterface $resolver 
    */ 
    public function setDefaultOptions(OptionsResolverInterface $resolver) 
    { 
     $resolver->setDefaults(array(
      'data_class' => 'Pas\ShopTestBundle\Entity\Product' 
     )); 
    } 

    /** 
    * @return string 
    */ 
    public function getName() 
    { 
     return 'pas_shoptestbundle_product'; 
    } 
} 

產品實體:

<?php 

namespace Pas\ShopTestBundle\Entity; 

use Doctrine\ORM\Mapping as ORM; 
use Doctrine\Common\Collections\ArrayCollection; 

/** 
* Product 
* 
* @ORM\Table(name="products") 
* @ORM\Entity(repositoryClass="Pas\ShopTestBundle\Entity\ProductRepository") 
*/ 
class Product 
{ 
    /** 
    * @var ArrayCollection 
    * 
    * @ORM\OneToMany(targetEntity="Description", mappedBy="product") 
    */ 
    private $descriptions; 

    /** 
    * @var Category 
    * 
    * @ORM\ManyToOne(targetEntity="Category", inversedBy="products", fetch="EAGER") 
    * @ORM\JoinColumn(name="category_id", referencedColumnName="id") 
    */ 
    private $category; 

    /** 
    * @var integer 
    * 
    * @ORM\Column(name="id", type="integer") 
    * @ORM\Id 
    * @ORM\GeneratedValue(strategy="AUTO") 
    */ 
    private $id; 

    /** 
    * @var string 
    * 
    * @ORM\Column(name="name", type="string", length=255) 
    */ 
    private $name; 

    /** 
    * @var float 
    * 
    * @ORM\Column(name="price", type="float") 
    */ 
    private $price; 

    /** 
    * @var integer 
    * 
    * @ORM\Column(name="quantity", type="integer") 
    */ 
    private $quantity; 

    // * 
    // * @var string 
    // * 
    // * @ORM\Column(name="categoryNames", type="string", length=255) 

    // private $categoryNames; 

    /** 
    * Creates Constructer for ArrayCollection 
    */ 
    public function __construct() { 
     $this->descriptions = new ArrayCollection(); 
    } 


    /** 
    * Get id 
    * 
    * @return integer 
    */ 
    public function getId() 
    { 
     return $this->id; 
    } 

    /** 
    * Set name 
    * 
    * @param string $name 
    * @return Product 
    */ 
    public function setName($name) 
    { 
     $this->name = $name; 

     return $this; 
    } 

    /** 
    * Get name 
    * 
    * @return string 
    */ 
    public function getName() 
    { 
     return $this->name; 
    } 

    /** 
    * Set price 
    * 
    * @param float $price 
    * @return Product 
    */ 
    public function setPrice($price) 
    { 
     $this->price = $price; 

     return $this; 
    } 

    /** 
    * Get price 
    * 
    * @return float 
    */ 
    public function getPrice() 
    { 
     return $this->price; 
    } 

    /** 
    * Set Quantity 
    * 
    * @param integer $quantity 
    * @return Product 
    */ 
    public function setQuantity($quantity) 
    { 
     $this->quantity = $quantity; 

     return $this; 
    } 

    /** 
    * Get Quantity 
    * 
    * @return integer 
    */ 
    public function getQuantity() 
    { 
     return $this->quantity; 
    } 

    /** 
    * Add descriptions 
    * 
    * @param \Pas\ShopTestBundle\Entity\Description $descriptions 
    * @return Product 
    */ 
    public function addDescription(\Pas\ShopTestBundle\Entity\Description $descriptions) 
    { 
     $this->descriptions[] = $descriptions; 

     return $this; 
    } 

    /** 
    * Remove descriptions 
    * 
    * @param \Pas\ShopTestBundle\Entity\Description $descriptions 
    */ 
    public function removeDescription(\Pas\ShopTestBundle\Entity\Description $descriptions) 
    { 
     $this->descriptions->removeElement($descriptions); 
    } 

    /** 
    * Get descriptions 
    * 
    * @return \Doctrine\Common\Collections\Collection 
    */ 
    public function getDescriptions() 
    { 
     return $this->descriptions; 
    } 

    /** 
    * Converts Product Name and Description to a Viewable String 
    * @return String 
    */ 
    public function __toString() { 
     return $this->getName(); 
     return $this->getDescriptions(); 
     return $this->getCategory(); 
    } 

    /** 
    * Set category 
    * 
    * @param \Pas\ShopTestBundle\Entity\Category $category 
    * @return Product 
    */ 
    public function setCategory(\Pas\ShopTestBundle\Entity\Category $category = null) 
    { 
     $this->category = $category; 

     return $this; 
    } 

    /** 
    * Get category 
    * 
    * @return \Pas\ShopTestBundle\Entity\Category 
    */ 
    public function getCategory() 
    { 
     return $this->category; 
    } 
} 

分類實體:

<?php 

namespace Pas\ShopTestBundle\Entity; 

use Doctrine\ORM\Mapping as ORM; 
use Doctrine\Common\Collections\ArrayCollection; 

/** 
* Category 
* 
* @ORM\Table(name="categories") 
* @ORM\Entity(repositoryClass="Pas\ShopTestBundle\Entity\CategoryRepository") 
*/ 
class Category 
{ 

    /** 
    * @var ArrayCollection 
    * 
    * @ORM\OneToMany(targetEntity="Product", mappedBy="category") 
    */ 
    private $products; 

    /** 
    * @var integer 
    * 
    * @ORM\Column(name="id", type="integer") 
    * @ORM\Id 
    * @ORM\GeneratedValue(strategy="AUTO") 
    */ 
    private $id; 

    /** 
    * @var string 
    * 
    * @ORM\Column(name="CategoryName", type="string", length=255) 
    */ 
    private $categoryName; 

    public function __construct() { 
     $this->products = new ArrayCollection(); 
    } 

    /** 
    * Get id 
    * 
    * @return integer 
    */ 
    public function getId() 
    { 
     return $this->id; 
    } 

    /** 
    * Set categoryName 
    * 
    * @param string $categoryName 
    * @return Category 
    */ 
    public function setCategoryName($categoryName) 
    { 
     $this->categoryName = $categoryName; 

     return $this; 
    } 

    /** 
    * Get categoryName 
    * 
    * @return string 
    */ 
    public function getCategoryName() 
    { 
     return $this->categoryName; 
    } 

    /** 
    * Add products 
    * 
    * @param \Pas\ShopTestBundle\Entity\Product $products 
    * @return Category 
    */ 
    public function addProduct(\Pas\ShopTestBundle\Entity\Product $products) 
    { 
     $this->products[] = $products; 

     return $this; 
    } 

    /** 
    * Remove products 
    * 
    * @param \Pas\ShopTestBundle\Entity\Product $products 
    */ 
    public function removeProduct(\Pas\ShopTestBundle\Entity\Product $products) 
    { 
     $this->products->removeElement($products); 
    } 

    /** 
    * Get products 
    * 
    * @return \Doctrine\Common\Collections\Collection 
    */ 
    public function getProducts() 
    { 
     return $this->products; 
    } 

    public function __toString() { 
     return $this->getCategoryName(); 
     return $this->getProducts(); 
     return $this->getCategory(); 
    } 
} 

和往常一樣幫助是真正appricated,謝謝!

回答

2

的問題是,在你的ProductType你有

public function buildForm(FormBuilderInterface $builder, array $options) 
    { 
     ... 
      ->add('category_id') 
     ... 
    } 

至極的實體產品不存在。您必須將該屬性作爲類別。 category_id僅適用於Doctrine的使用,而不適用於表單類型。

/** 
    * @var Category 
    * 
    * @ORM\ManyToOne(targetEntity="Category", inversedBy="products", fetch="EAGER") 
    * @ORM\JoinColumn(name="category_id", referencedColumnName="id") 
    */ 
    private $category; 

希望這對你有所幫助。

+0

謝謝你,你是對的。將在幾分鐘內接受。 –

0

如果在你的表單中使用->add('category_id')那麼Symfony希望獲得該字段的默認值 - 但是你沒有$category_id屬性 - 只有$category

看起來你想顯示「類別」的多種選擇類型和該類別屬性的id的簡單輸入 - 我不知道爲什麼。你可以爲你的實體添加getter(和類似的setter)。

public function getCategoryId() 
{ 
    if ($this->category) { 
     return $this->category->id; 
    } 

    return null; 
} 
相關問題