【问题标题】:Error: Neither the Property...Nor one of the Methods...exists and have public access [duplicate]错误:属性...也没有一种方法...不存在并且具有公共访问权限[重复]
【发布时间】:2016-06-01 03:59:56
【问题描述】:

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

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

如果我添加属性 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 %}

下面是产品类型:

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

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

【问题讨论】:

    标签: php symfony orm doctrine-orm


    【解决方案1】:

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

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

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

    【讨论】:

      【解决方案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;
      

      希望对您有所帮助。

      【讨论】:

      • 谢谢你,你是对的。将在几分钟内接受。
      猜你喜欢
      • 2021-02-18
      • 2015-12-02
      • 2017-09-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-13
      • 2015-04-09
      • 2017-11-17
      相关资源
      最近更新 更多