我知道这个问题已被多次询问,但没有人正确回答,似乎没有任何工作。
我的错误
执行'INSERT INTO pre_order_image时发生异常 (image_name,updatedAt,preOrder_id)VALUES(?,?,?)'与params [null,“2018-01-17 05:24:27”,6]:
SQLSTATE [23000]:完整性约束违规:1048列 'image_name'不能为空
我的配置
prOrder_image:
uri_prefix: /images/uploads/preOrders
upload_destination: %kernel.root_dir%/../web/images/uploads/preOrders
namer: vich_uploader.namer_uniqid
inject_on_load: false
delete_on_update: true
delete_on_remove: true
我的实体
* @var File
*
* @Vich\UploadableField(mapping="prOrder_image", fileNameProperty="imageName")
*/
private $imageFile;
* @var string
*
* @ORM\Column(name="image_name", type="string", length=255)
*/
private $imageName;
/**
* @var \DateTime
*
* @ORM\Column(name="updatedAt", type="datetime")
*/
private $updatedAt;
/**
* @ORM\ManyToOne(targetEntity="PreOrder", inversedBy="images")
* @ORM\JoinColumn(name="preOrder_id", referencedColumnName="id")
*/
private $preOrder;
private $remove;
/**
* If manually uploading a file (i.e. not using Symfony Form) ensure an instance
* of 'UploadedFile' is injected into this setter to trigger the update. If this
* bundle's configuration parameter 'inject_on_load' is set to 'true' this setter
* must be able to accept an instance of 'File' as the bundle will inject one here
* during Doctrine hydration.
*
* @param File|\Symfony\Component\HttpFoundation\File\UploadedFile $image
*
* @return PreOrderImage
*/
public function setImageFile(File $image = null)
{
$this->imageFile = $image;
if ($image) {
// It is required that at least one field changes if you are using doctrine
// otherwise the event listeners won't be called and the file is lost
$this->updatedAt = new \DateTimeImmutable();
}
return $this;
}
/**
* @return File|null
*/
public function getImageFile()
{
return $this->imageFile;
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set imageName
*
* @param string $imageName
* @return PreOrderImage
*/
public function setImageName($imageName)
{
$this->imageName = $imageName;
return $this;
}
/**
* Get imageName
*
* @return string
*/
public function getImageName()
{
return $this->imageName;
}
/**
* Set updatedAt
*
* @param \DateTime $updatedAt
* @return PreOrderImage
*/
public function setUpdatedAt($updatedAt)
{
$this->updatedAt = $updatedAt;
return $this;
}
/**
* Get updatedAt
*
* @return \DateTime
*/
public function getUpdatedAt()
{
return $this->updatedAt;
}
/**
* Set preOrder
*
* @param \AppBundle\Entity\PreOrder $preOrder
* @return PreOrderImage
*/
public function setPreOrder(\AppBundle\Entity\PreOrder $preOrder = null)
{
$this->preOrder = $preOrder;
$preOrder->addImage($this);
return $this;
}
/**
* Get preOrder
*
* @return \AppBundle\Entity\PreOrder
*/
public function getPreOrder()
{
return $this->preOrder;
}
/**
* @return mixed
*/
public function getRemove()
{
return $this->remove;
}
/**
* @param mixed $remove
*/
public function setRemove($remove)
{
$this->remove = $remove;
}
我的控制器
if($request->isMethod('POST')){
$em = $this->getDoctrine()->getManager();
// create preOrder ad object
$preOrder = new PreOrder();
$preOrder->setSeller($this->getUser()->getSeller())
->setName($request->get('preOrder_title'))
->setPrice($request->get('preOrder_price'))
->setQuantity($request->get('preOrder_quantity'))
->setDescription01($request->get('preOrder_description01'))
->setDescription02($request->get('preOrder_description02'))
->setDescription03($request->get('preOrder_description03'))
->setDescription04($request->get('preOrder_description04'))
->setDescription05($request->get('preOrder_description05'))
->setSubLocation($this->getRepository('SubLocation')->find($request->get('preOrder_sub_location')))
->setSubCategory($subCategory)
->setContactNo(empty($request->get('preOrder_contactNo')) ? $this->getUser()->getMobile() : $request->get('preOrder_contactNo'))
->setEmail(empty($request->get('preOrder_email')) ? $this->getUser()->getEmail() : $request->get('preOrder_email'))
->setDiscount(!empty($request->get('preOrder_discount')) ? $request->get('preOrder_discount') : null);
// filter out null images
$imageArray = array_filter($request->files->all());
// create image objects
foreach($imageArray as $item){
$image = new PreOrderImage();
$image->setImageFile($item);
$image->setPreOrder($preOrder);
}
$em->persist($preOrder);
$em->flush();