错误:属性都不是.也不是方法之一..存在并具有公共访问权限


Error: Neither the Property...Nor one of the Methods...exists and have public access

实际错误供参考:属性"category_id"和方法之一"getCategoryId()","categoryId()","isCategoryId()","hasCategoryId()","__get()"都不存在,并且在类"Pas''ShopTestBundle''Entity''Product"中具有公共访问权限。

我已经对这个错误进行了研究,但我无法弄清楚。为什么我以前能够创建"产品",但现在我不能?我看到getCategoryId方法不存在,但教义不应该创建它们(因为我确实创建了与教义的关系)。

如果我添加属性category_id则出现错误:

执行"选择 t0.id AS id1,t0.category_id AS category_id2,t0"时发生异常。类别名称 AS 类别名称 3 来自类别 t0':

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

与往常一样,任何帮助都是真正值得赞赏的,谢谢!

问题是在你的产品类型中你有

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

实体产品中不存在。必须将属性引用为类别。该category_id仅对教义使用有效,对表格类型无效。

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

希望这对你有帮助。

如果你在你的表单中使用->add('category_id')那么Symfony想要获取该字段的默认值 - 但你没有$category_id属性 - 只是$category

看起来您想显示"类别"的多项选择类型以及该类别属性的 id 的简单输入 - 我不知道为什么。您可以通过为您的实体添加该 getter(和类似的 setter)来做到这一点。

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