创建对象失败。多对多关系

时间:2019-03-07 20:29:46

标签: php symfony sonata-admin

我的代码有问题。关于一家餐厅。我有两个具有多对多关系的对象:产品和天数。当我尝试创建日时,出现以下错误:“创建对象失败:App \ Entity \ Day \ Day”。我在哪里错了?

我将实体的代码放在这里。如果您需要更多,请告诉我。我认为这不是来自管理部分。

这是产品实体:

<?php

namespace App\Entity\Product;

use App\Entity\Category\Category;
use App\Entity\Day\Day;
use App\Entity\ProductEntry\ProductEntry;
use App\Entity\Restaurant\Restaurant;
use App\Entity\Schedule\Schedule;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use JMS\Serializer\Annotation as Serializer;

/**
 * Class Product
 * @ORM\Entity(repositoryClass="App\Repository\Product\ProductRepository")
 * @ORM\Table(name="products")
 * @package App\Entity\Product
 */
class Product
{
    /**
     * @var int
     *
     * @ORM\Id
     * @ORM\Column(name="id", type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

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

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

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="added_date", type="datetime", nullable=false)
     */
    private $addedDate;

    /**
     * @var boolean
     *
     * @ORM\Column(name="availability", type="boolean", nullable=true)
     */
    private $availability;

    /**
     * @var Category
     *
     * @ORM\ManyToOne(targetEntity="App\Entity\Category\Category", inversedBy="products", cascade={"persist"})
     * @ORM\JoinColumn(name="category_id", referencedColumnName="id", onDelete="CASCADE", nullable=true)
     */
    private $category;

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

    /**
     * @var ProductEntry
     *
     * @ORM\OneToMany(targetEntity="App\Entity\ProductEntry\ProductEntry", mappedBy="originalProduct", cascade={"persist"})
     * @Serializer\Exclude()
     */
    private $productsEntries;

    /**
     * @ORM\ManyToOne(targetEntity="App\Entity\Schedule\Schedule", inversedBy="products", cascade={"persist"})
     * @ORM\JoinColumn(name="schedule_id", referencedColumnName="id", onDelete="CASCADE")
     *
     * @var Schedule
     */
    private $schedule;

    /**
     * @ORM\ManyToOne(targetEntity="App\Entity\Restaurant\Restaurant", inversedBy="products", cascade={"persist"})
     * @ORM\JoinColumn(name="restaurant_id", referencedColumnName="id", onDelete="CASCADE")
     *
     * @var Restaurant
     */
    private $restaurant;

    /**
     * @var Day
     *
     * @ORM\ManyToMany(targetEntity="App\Entity\Day\Day", mappedBy="products", cascade={"persist"})
     */
    private $days;

    public function __toString()
    {
        return $this->name ?: "";
    }

    public function __construct()
    {
        $this->addedDate = new \DateTime();
        $this->days = new ArrayCollection();
    }

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

    /**
     * @param int $id
     */
    public function setId(int $id)
    {
        $this->id = $id;
    }

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

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

    /**
     * @return string
     */
    public function getDescription()
    {
        return $this->description;
    }

    /**
     * @param string $description
     */
    public function setDescription(string $description)
    {
        $this->description = $description;
    }

    /**
     * @return mixed
     */
    public function getAddedDate()
    {
        return $this->addedDate;
    }

    /**
     * @param mixed $addedDate
     */
    public function setAddedDate($addedDate)
    {
        $this->addedDate = $addedDate;
    }

    /**
     * @return bool
     */
    public function isAvailability()
    {
        return $this->availability;
    }

    /**
     * @param bool $availability
     */
    public function setAvailability($availability)
    {
        $this->availability = $availability;
    }

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

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

    /**
     * @return Category
     */
    public function getCategory()
    {
        return $this->category;
    }

    /**
     * @param Category $category
     */
    public function setCategory($category): void
    {
        $this->category = $category;
    }

    /**
     * @return ProductEntry
     */
    public function getProductsEntries()
        {
        return $this->productsEntries;
    }

    /**
     * @param ProductEntry $productsEntries
     */
    public function setProductsEntries(ProductEntry $productsEntries): void
    {
        $this->productsEntries = $productsEntries;
    }

    /**
     * @return Schedule
     */
    public function getSchedule()
    {
        return $this->schedule;
    }

    /**
     * @param Schedule $schedule
     */
    public function setSchedule(Schedule $schedule): void
    {
        $this->schedule = $schedule;
    }

    /**
     * @return Restaurant
     */
    public function getRestaurant()
    {
        return $this->restaurant;
    }

    /**
     * @param Restaurant $restaurant
     */
    public function setRestaurant(Restaurant $restaurant): void
    {
        $this->restaurant = $restaurant;
    }

    /**
     * @return Day
     */
    public function getDays()
    {
        return $this->days;
    }

    /**
     * @param Day $days
     */
    public function setDays(Day $days): void
    {
        $this->days = $days;
    }
}

这里是Day实体:

<?php

namespace App\Entity\Day;

use App\Entity\Product\Product;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;

/**
 * Class Day
 * @package App\Entity\Day
 * @ORM\Entity(repositoryClass="App\Repository\Day\DayRepository")
 * @ORM\Table(name="days")
 */
class Day
{

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

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

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="current_date", type="datetime", nullable=false)
     */
    private $currentDate;

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="max_date", type="datetime", nullable=true)
     */
    private $maxDate;

    /**
     * @var Product
     *
     * @ORM\ManyToMany(targetEntity="App\Entity\Product\Product", inversedBy="days", cascade={"persist"})
     * @ORM\JoinTable(name="product_day",
     *     joinColumns={@ORM\JoinColumn(name="day_id", referencedColumnName="id")},
     *     inverseJoinColumns={@ORM\JoinColumn(name="product_id", referencedColumnName="id")})
     */
    private $products;

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

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

    /**
     * @param int $id
     */
    public function setId(int $id): void
    {
        $this->id = $id;
    }

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

    /**
     * @param string $name
     */
    public function setName(string $name): void
    {
        $this->name = $name;
    }

    /**
     * @return \DateTime
     */
    public function getCurrentDate()
    {
        return $this->currentDate;
    }

    /**
     * @param \DateTime $currentDate
     */
    public function setCurrentDate(\DateTime $currentDate): void
    {
        $this->currentDate = $currentDate;
    }

    /**
     * @return \DateTime
     */
    public function getMaxDate()
    {
        return $this->maxDate;
    }

    /**
     * @param \DateTime $maxDate
     */
    public function setMaxDate(\DateTime $maxDate): void
    {
        $this->maxDate = $maxDate;
    }

    /**
     * @return Product
     */
    public function getProducts()
    {
        return $this->products;
    }

    /**
     * @param Product $products
     */
    public function setProducts(Product $products): void
    {
        $this->products = $products;
    }
}

2 个答案:

答案 0 :(得分:1)

似乎您为“日实体”创建了一个列,名为current_date

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="current_date", type="datetime", nullable=false)
     */
    private $currentDate;

如果使用MySQL,current_date是保留字。请检查https://dev.mysql.com/doc/refman/5.5/en/keywords.html以获取更多参考。

答案 1 :(得分:0)

就像文森特(Vincent)在评论中说的那样,您的命名空间很腥... 除非您确实将它们放在子文件夹中,否则应为namespace App\Entity

在“产品”实体中,您可以为某些参数缩短JoinColumn
除非您真的需要特定的东西,否则您不需要写太多。 Symfony具有“默认”值。 另外,您不要在映射上添加@var。 Symfony将仅将它作为参数而不是映射。

/**
 * @var Category
 *
 * @ORM\ManyToOne(targetEntity="App\Entity\Category\Category", inversedBy="products", cascade={"persist"})
 * @ORM\JoinColumn(onDelete="CASCADE", nullable=true)
 */
private $category;
/**
 * @ORM\ManyToOne(targetEntity="App\Entity\Schedule\Schedule", inversedBy="products", cascade={"persist"})
 * @ORM\JoinColumn(onDelete="CASCADE")
 */
private $schedule;
/**
 * @ORM\ManyToOne(targetEntity="App\Entity\Restaurant\Restaurant", inversedBy="products", cascade={"persist"})
 * @ORM\JoinColumn(onDelete="CASCADE")
 */
private $restaurant;

这同样适用于您的Day实体。

/**
 * @var Product
 *
 * @ORM\ManyToMany(targetEntity="App\Entity\Product\Product", inversedBy="days", cascade={"persist"})
 * @ORM\JoinTable(name="product_day")
 */
private $products;

这样做将防止您犯一些小映射错误。

首先尝试纠正这些问题,然后让我们知道您是否还有问题。