我遇到了问题,希望能得到专家的帮助。
我有实体:
产品
// src/Acme/DemoBundle/Entity/Product.php
/**
* @ORM\Entity
* @ORM\Table(name="products")
*/
class Product
{
...
/**
* @ORM\Column(name="file_uuid", unique=true, nullable=true)
* @ORM\OneToOne(targetEntity="Acme\DemoBundle\Entity\File")
* @ORM\JoinColumn(name="file_uuid", referencedColumnName="uuid")
*
*/
protected $file;
/**
* @ORM\OneToMany(targetEntity="Acme\DemoBundle\Entity\Image", mappedBy="product")
*
* @var ArrayCollection Collection of Image entities
*/
protected $images;
...
}
文件
// src/Acme/DemoBundle/Entity/File.php
/**
* @ORM\Entity
* @ORM\Table(name="product_files")
*/
class File
{
/**
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue
*/
protected $id;
/**
* @ORM\Column(name="uuid", type="string", length=64)
*/
protected $uuid;
/**
* @ORM\Column(name="product_id")
*/
protected $product;
...
}
图片
// src/Acme/DemoBundle/Entity/Image.php
/**
* @ORM\Entity
* @ORM\Table(name="product_images")
*/
class Image extends File
{
/**
* @ORM\ManyToOne(targetEntity="Acme\DemoBundle\Entity\Product", inversedBy="images")
* @ORM\JoinColumn(name="product_id", referencedColumnName="id")
*/
protected $product;
/**
* @ORM\Column(name="extension", type="string")
*/
protected $extension;
...
}
我的 sql 正确:
product_files
+-------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| uuid | varchar(64) | NO | | NULL | |
| product_id | varchar(255) | NO | | NULL | |
+-------------+--------------+------+-----+---------+----------------+
product_images
+-------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| uuid | varchar(64) | NO | | NULL | |
| product_id | varchar(255) | NO | | NULL | |
| extension | varchar(64) | NO | | NULL | |
+-------------+--------------+------+-----+---------+----------------+
但是当我运行我的应用程序时,我遇到了一个错误:
An exception has been thrown during the rendering of a template ("Notice: Undefined index: product in .../vendor/doctrine/orm/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php line 1575") in AcmeDemoBundle:Product:edit.html.twig at line 24.
我哪里错了?
在 edit.html.twig 中,我只是想显示产品图片。
我可以避免从$product的文件实体注释中删除此问题:
@ORM\Column(name="product_id")
但是,在学说:模式:更新之后,我在product_images表中丢失了我的product_id字段。
我想要什么:
- 每个产品有一个文件
- 每个产品有多个图像