我有2个实体 - 产品和类别。 但我在symfony开发工具栏中有错误 - 无效实体2.
确切的错误是:
Entities Mapping
Class Mapping errors
AppBundle\Entity\category
The association AppBundle\Entity\category#products refers to the owning side field AppBundle\Entity\Product#category which does not exist.
AppBundle\Entity\Product
The association AppBundle\Entity\Product#categoryId refers to the inverse side field AppBundle\Entity\category#product which does not exist.here
我已经多次尝试修复symfony docs之后的版本,但它不起作用。
所以,请告诉我,我错了......
以下是我的2个实体:
产品
<?php
namespace AppBundle\Entity;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Validator\Constraints as Assert;
use Doctrine\ORM\Mapping as ORM;
/**
* Product
*
* @ORM\Table(name="products")
* @ORM\Entity(repositoryClass="AppBundle\Repository\ProductRepository")
*/
class Product
{
/**
* @var int
*
* @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 string
*
* @ORM\Column(name="sku", type="string", length=255, unique=true)
*/
private $sku;
/**
*
* @ORM\ManyToOne(targetEntity="AppBundle\Entity\category", inversedBy="product")
* @ORM\JoinColumn(name="categoryId", referencedColumnName="id")
*/
private $categoryId;
/**
* @var string
*
* @ORM\Column(name="image", type="string", length=255, nullable=true)
* @Assert\File(
* maxSize="5242880",
* mimeTypes = {
* "image/png",
* "image/jpeg",
* "image/jpg",
* "image/gif",
* }
* )
*/
private $image;
/**
* @var string
*
* @ORM\Column(name="description", type="text", nullable=true)
*/
private $description;
/**
* @var string
*
* @ORM\Column(name="price", type="decimal", precision=10, scale=2, nullable=true)
*/
private $price;
/**
* @var bool
*
* @ORM\Column(name="isActive", type="boolean", nullable=true)
*/
private $isActive;
/**
* @var int
*
* @ORM\Column(name="quantity", type="integer", nullable=true)
*/
private $quantity;
/**
* @var decimal
*/
private $percent;
/**
* @var decimal
*/
private $endPrice;
/**
* Get id
*
* @return int
*/
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 sku
*
* @param string $sku
*
* @return Product
*/
public function setSku($sku)
{
$this->sku = $sku;
return $this;
}
/**
* Get sku
*
* @return string
*/
public function getSku()
{
return $this->sku;
}
/**
* Set categoryId
*
* @param integer $categoryId
*
* @return Product
*/
public function setCategoryId($categoryId)
{
$this->categoryId = $categoryId;
return $this;
}
/**
* Get categoryId
*
* @return int
*/
public function getCategoryId()
{
return $this->categoryId;
}
/**
* Set image
*
* @param string $image
*
* @return Product
*/
public function setImage($image)
{
$this->image = $image;
return $this;
}
/**
* Get image
*
* @return string
*/
public function getImage()
{
return $this->image;
}
/**
* Set description
*
* @param string $description
*
* @return Product
*/
public function setDescription($description)
{
$this->description = $description;
return $this;
}
/**
* Get description
*
* @return string
*/
public function getDescription()
{
return $this->description;
}
/**
* Set price
*
* @param string $price
*
* @return Product
*/
public function setPrice($price)
{
$this->price = $price;
return $this;
}
/**
* Get price
*
* @return string
*/
public function getPrice()
{
return $this->price;
}
/**
* Set isActive
*
* @param boolean $isActive
*
* @return Product
*/
public function setIsActive($isActive)
{
$this->isActive = $isActive;
return $this;
}
/**
* Get isActive
*
* @return bool
*/
public function getIsActive()
{
return $this->isActive;
}
/**
* Set quantity
*
* @param integer $quantity
*
* @return Product
*/
public function setQuantity($quantity)
{
$this->quantity = $quantity;
return $this;
}
/**
* Get quantity
*
* @return int
*/
public function getQuantity()
{
return $this->quantity;
}
/**
* @return decimal
*/
public function getPercent()
{
return $this->percent;
}
/**
* @param decimal $percent
*/
public function setPercent($percent)
{
if(null != $percent) {
$this->percent = $percent;
}
}
/**
* @return decimal
*/
public function getEndPrice()
{
return $this->endPrice;
}
/**
* @param decimal $endPrice
*/
public function setEndPrice($endPrice)
{
$this->endPrice = $endPrice;
}
}
............................................... .................................................. .................................................. .................................................. .................................... 类
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
/**
* category
*
* @ORM\Table(name="category")
* @ORM\Entity(repositoryClass="AppBundle\Repository\categoryRepository")
*/
class category
{
/**
* @var int
*
* @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 int
*
* @ORM\Column(name="parent", type="integer", nullable=true)
*/
private $parent;
/**
* @var bool
*
* @ORM\Column(name="isActive", type="boolean", nullable=true)
*/
private $isActive;
/**
* @ORM\OneToMany(targetEntity="AppBundle\Entity\Product" , mappedBy="category")
*/
private $products;
public function __construct()
{
$this->products = new ArrayCollection();
}
/**
* Get id
*
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* Set name
*
* @param string $name
*
* @return category
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* Set parent
*
* @param integer $parent
*
* @return category
*/
public function setParent($parent)
{
$this->parent = $parent;
return $this;
}
/**
* Get parent
*
* @return int
*/
public function getParent()
{
return $this->parent;
}
/**
* Set isActive
*
* @param boolean $isActive
*
* @return category
*/
public function setIsActive($isActive)
{
$this->isActive = $isActive;
return $this;
}
/**
* Get isActive
*
* @return bool
*/
public function getIsActive()
{
return $this->isActive;
}
public function __toString()
{
return $this->id."";
}
/**
* @return mixed
*/
public function getProducts()
{
return $this->products;
}
/**
* @param mixed $products
*/
public function setProducts($products)
{
$this->products = $products;
}
}
如何解决?
答案 0 :(得分:0)
尝试替换此产品实体
private $categoryId;
通过这个
private $category;
不要忘记改变吸气剂和吸气剂设定器
答案 1 :(得分:0)
尝试改变这个:
* @ORM\ManyToOne(targetEntity="AppBundle\Entity\category", inversedBy="product")
到此:
* @ORM\ManyToOne(targetEntity="Category", inversedBy="products")
和此:
* @ORM\OneToMany(targetEntity="AppBundle\Entity\Product" , mappedBy="category")
到此:
* @ORM\OneToMany(targetEntity="Product" , mappedBy="category")