错误:属性......也没有其中一个方法...存在且具有公共访问权限

时间:2016-02-19 14:29:44

标签: php symfony orm doctrine-orm

参考实际错误: 属性“category_id”和方法“getCategoryId()”,“categoryId()”,“isCategoryId()”,“hasCategoryId()”,“__ get()”都不存在,并且在类“Pas”中具有公共访问权限ShopTestBundle \实体\产品”。

我已经完成了对此错误的研究,但我无法弄明白。为什么我之前能够制作“产品”,但现在我不能?我看到getCategoryId方法不存在,但不应该教条创建它们(因为我确实创建了与doctrine的关系)。

如果我添加属性category_id,我会收到错误:

执行'SELECT t0.id AS id1,t0.category_id AS category_id2,t0.CategoryName AS CategoryName3 FROM categories 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 %}

以下是ProductType:

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

一如既往,任何帮助都是真实的,谢谢!

2 个答案:

答案 0 :(得分:3)

问题是您的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;

希望这对你有所帮助。

答案 1 :(得分:0)

如果您在表单中使用->add('category_id'),那么Symfony希望获得该字段的默认值 - 但您没有$category_id属性 - 只有$category

看起来你想要为“类别”显示多项选择类型,并为该类别属性的id显示简单输入 - 我不知道为什么。你可以为你的实体添加getter(和类似的setter)。

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

    return null;
}