无法创建对象.多一的关系



我的代码有问题。这是关于一家餐厅。我有两个具有多一关系的对象:产品和日子。当我尝试创建一天时,我会收到此错误:"无法创建对象:app entity day day"。我在哪里错了?

我在此处为实体提供了代码。如果您需要更多,请告诉我。我认为这不是从管理员部门。

这是产品实体:

<?php
namespace AppEntityProduct;
use AppEntityCategoryCategory;
use AppEntityDayDay;
use AppEntityProductEntryProductEntry;
use AppEntityRestaurantRestaurant;
use AppEntityScheduleSchedule;
use DoctrineCommonCollectionsArrayCollection;
use DoctrineORMMapping as ORM;
use JMSSerializerAnnotation as Serializer;
/**
 * Class Product
 * @ORMEntity(repositoryClass="AppRepositoryProductProductRepository")
 * @ORMTable(name="products")
 * @package AppEntityProduct
 */
class Product
{
    /**
     * @var int
     *
     * @ORMId
     * @ORMColumn(name="id", type="integer")
     * @ORMGeneratedValue(strategy="AUTO")
     */
    private $id;
    /**
     * @var string
     *
     * @ORMColumn(name="name", type="string", length=255)
     */
    private $name;
    /**
     * @var string
     *
     * @ORMColumn(name="description", type="string", length=255, nullable=true)
     */
    private $description;
    /**
     * @var DateTime
     *
     * @ORMColumn(name="added_date", type="datetime", nullable=false)
     */
    private $addedDate;
    /**
     * @var boolean
     *
     * @ORMColumn(name="availability", type="boolean", nullable=true)
     */
    private $availability;
    /**
     * @var Category
     *
     * @ORMManyToOne(targetEntity="AppEntityCategoryCategory", inversedBy="products", cascade={"persist"})
     * @ORMJoinColumn(name="category_id", referencedColumnName="id", onDelete="CASCADE", nullable=true)
     */
    private $category;
    /**
     * @var float
     *
     * @ORMColumn(name="price", type="float", length=255, nullable=true)
     */
    private $price;
    /**
     * @var ProductEntry
     *
     * @ORMOneToMany(targetEntity="AppEntityProductEntryProductEntry", mappedBy="originalProduct", cascade={"persist"})
     * @SerializerExclude()
     */
    private $productsEntries;
    /**
     * @ORMManyToOne(targetEntity="AppEntityScheduleSchedule", inversedBy="products", cascade={"persist"})
     * @ORMJoinColumn(name="schedule_id", referencedColumnName="id", onDelete="CASCADE")
     *
     * @var Schedule
     */
    private $schedule;
    /**
     * @ORMManyToOne(targetEntity="AppEntityRestaurantRestaurant", inversedBy="products", cascade={"persist"})
     * @ORMJoinColumn(name="restaurant_id", referencedColumnName="id", onDelete="CASCADE")
     *
     * @var Restaurant
     */
    private $restaurant;
    /**
     * @var Day
     *
     * @ORMManyToMany(targetEntity="AppEntityDayDay", 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;
    }
}

这是一天实体:

<?php
namespace AppEntityDay;
use AppEntityProductProduct;
use DoctrineCommonCollectionsArrayCollection;
use DoctrineORMMapping as ORM;
/**
 * Class Day
 * @package AppEntityDay
 * @ORMEntity(repositoryClass="AppRepositoryDayDayRepository")
 * @ORMTable(name="days")
 */
class Day
{
    /**
     * @var integer
     *
     * @ORMId
     * @ORMColumn(name="id", type="integer")
     * @ORMGeneratedValue(strategy="AUTO")
     */
    private $id;
    /**
     * @var string
     *
     * @ORMColumn(name="name", type="string", nullable=false)
     */
    private $name;
    /**
     * @var DateTime
     *
     * @ORMColumn(name="current_date", type="datetime", nullable=false)
     */
    private $currentDate;
    /**
     * @var DateTime
     *
     * @ORMColumn(name="max_date", type="datetime", nullable=true)
     */
    private $maxDate;
    /**
     * @var Product
     *
     * @ORMManyToMany(targetEntity="AppEntityProductProduct", inversedBy="days", cascade={"persist"})
     * @ORMJoinTable(name="product_day",
     *     joinColumns={@ORMJoinColumn(name="day_id", referencedColumnName="id")},
     *     inverseJoinColumns={@ORMJoinColumn(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;
    }
}

看来您为您的一天实体创建了一个名为 current_date

的列
    /**
     * @var DateTime
     *
     * @ORMColumn(name="current_date", type="datetime", nullable=false)
     */
    private $currentDate;

如果您使用的是mySQL,则current_date是一个保留的单词。请检查https://dev.mysql.com/doc/refman/5.5/en/keywords.html以获取更多参考。

就像文森特(Vincent除非您真的将它们放在子文件夹中,否则应该是namespace AppEntity

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

/**
 * @var Category
 *
 * @ORMManyToOne(targetEntity="AppEntityCategoryCategory", inversedBy="products", cascade={"persist"})
 * @ORMJoinColumn(onDelete="CASCADE", nullable=true)
 */
private $category;
/**
 * @ORMManyToOne(targetEntity="AppEntityScheduleSchedule", inversedBy="products", cascade={"persist"})
 * @ORMJoinColumn(onDelete="CASCADE")
 */
private $schedule;
/**
 * @ORMManyToOne(targetEntity="AppEntityRestaurantRestaurant", inversedBy="products", cascade={"persist"})
 * @ORMJoinColumn(onDelete="CASCADE")
 */
private $restaurant;

同样适用于您的一天实体。

/**
 * @var Product
 *
 * @ORMManyToMany(targetEntity="AppEntityProductProduct", inversedBy="days", cascade={"persist"})
 * @ORMJoinTable(name="product_day")
 */
private $products;

这样做将阻止您进行一些小型映射错误。

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

相关内容

  • 没有找到相关文章

最新更新