我有一个嵌入在AdsList表单中的文件上传表单,当我提交时,当我允许该字段为空时,adslist_id在照片表中为空。但是,当我没有收到以下错误时:
An exception occurred while executing 'INSERT INTO Photos (photo_path,
thumbnail_path,adslist_id) VALUES (?, ?, ?)' with params ["louboutin-pigalle1.jpg",
null, null]:
SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'adslist_id'
cannot be null
如果你们没有线索我会很感激地指出一个教程来上传嵌入实体的多个上传文件。这应该是非常标准的,但我找不到。
这是我的obbex / AdsBundle / Entity / AdsList
<?php
namespace obbex\AdsBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* AdsList
*
* @ORM\Table()
* @ORM\Entity
* @ORM\Entity(repositoryClass="obbex\AdsBundle\Entity\AdsListRepository")
* @ORM\HasLifecycleCallbacks()
*/
class AdsList
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="username", type="string", length=255)
*/
private $username;
/**
* @var string
*
* @ORM\Column(name="email", type="string", length=255)
*/
private $email;
/**
* @var string
*
* @ORM\Column(name="telephone", type="string", length=255)
*/
private $telephone;
/**
* @var string
*
* @ORM\Column(name="display_phone", type="string", length=255)
*/
private $displayPhone;
/**
* @var string
*
* @ORM\Column(name="title", type="string", length=255)
*/
private $title;
/**
* @var string
*
* @ORM\Column(name="description", type="text")
*/
private $description;
/**
* @var string
*
* @ORM\Column(name="country", type="string", length=255)
*/
private $country;
/**
* @var string
*
* @ORM\Column(name="region", type="string", length=255)
*/
private $region;
/**
* @var string
*
* @ORM\Column(name="department", type="string", length=255)
*/
private $department;
/**
* @var string
*
* @ORM\Column(name="address", type="string", length=255)
*/
private $address;
/**
* @var string
*
* @ORM\Column(name="city", type="string", length=255)
*/
private $city;
/**
* @var string
*
* @ORM\Column(name="zip_code", type="string", length=255)
*/
private $zipCode;
/**
* @var string
*
* @ORM\Column(name="status_pro", type="string", length=255)
*/
private $statusPro;
/**
* @var \DateTime
*
* @ORM\Column(name="creationtime", type="datetime")
*/
private $creationtime;
/**
* @var \DateTime
*
* @ORM\Column(name="updatetime", type="datetime")
*/
private $updatetime;
/**
* @var boolean
*
* @ORM\Column(name="publication", type="boolean")
*/
private $publication;
/**
* @ORM\OneToMany(targetEntity="obbex\AdsBundle\Entity\Photos",mappedBy="adslist", cascade={"persist"})
*/
protected $photos;
/**
* Set photos
*
* @param \obbex\AdsBundle\Entity\Photos $photos
* @return AdsList
*/
public function setPhotos(\obbex\AdsBundle\Entity\Photos $photos = null)
{
$this->photos = $photos;
return $this;
}
/**
* Get photos
*
* @return \obbex\AdsBundle\Entity\Photos
*/
public function getPhotos()
{
return $this->photos;
}
public function __construct() {
$this->creationtime=new \Datetime;
$this->photos = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set username
*
* @param string $username
* @return AdsList
*/
public function setUsername($username)
{
$this->username = $username;
return $this;
}
/**
* Get username
*
* @return string
*/
public function getUsername()
{
return $this->username;
}
/**
* Set email
*
* @param string $email
* @return AdsList
*/
public function setEmail($email)
{
$this->email = $email;
return $this;
}
/**
* Get email
*
* @return string
*/
public function getEmail()
{
return $this->email;
}
/**
* Set telephone
*
* @param string $telephone
* @return AdsList
*/
public function setTelephone($telephone)
{
$this->telephone = $telephone;
return $this;
}
/**
* Get telephone
*
* @return string
*/
public function getTelephone()
{
return $this->telephone;
}
/**
* Set displayPhone
*
* @param string $displayPhone
* @return AdsList
*/
public function setDisplayPhone($displayPhone)
{
$this->displayPhone = $displayPhone;
return $this;
}
/**
* Get displayPhone
*
* @return string
*/
public function getDisplayPhone()
{
return $this->displayPhone;
}
/**
* Set title
*
* @param string $title
* @return AdsList
*/
public function setTitle($title)
{
$this->title = $title;
return $this;
}
/**
* Get title
*
* @return string
*/
public function getTitle()
{
return $this->title;
}
/**
* Set description
*
* @param string $description
* @return AdsList
*/
public function setDescription($description)
{
$this->description = $description;
return $this;
}
/**
* Get description
*
* @return string
*/
public function getDescription()
{
return $this->description;
}
/**
* Set country
*
* @param string $country
* @return AdsList
*/
public function setCountry($country)
{
$this->country = $country;
return $this;
}
/**
* Get country
*
* @return string
*/
public function getCountry()
{
return $this->country;
}
/**
* Set region
*
* @param string $region
* @return AdsList
*/
public function setRegion($region)
{
$this->region = $region;
return $this;
}
/**
* Get region
*
* @return string
*/
public function getRegion()
{
return $this->region;
}
/**
* Set department
*
* @param string $department
* @return AdsList
*/
public function setDepartment($department)
{
$this->department = $department;
return $this;
}
/**
* Get department
*
* @return string
*/
public function getDepartment()
{
return $this->department;
}
/**
* Set address
*
* @param string $address
* @return AdsList
*/
public function setAddress($address)
{
$this->address = $address;
return $this;
}
/**
* Get address
*
* @return string
*/
public function getAddress()
{
return $this->address;
}
/**
* Set city
*
* @param string $city
* @return AdsList
*/
public function setCity($city)
{
$this->city = $city;
return $this;
}
/**
* Get city
*
* @return string
*/
public function getCity()
{
return $this->city;
}
/**
* Set zipCode
*
* @param string $zipCode
* @return AdsList
*/
public function setZipCode($zipCode)
{
$this->zipCode = $zipCode;
return $this;
}
/**
* Get zipCode
*
* @return string
*/
public function getZipCode()
{
return $this->zipCode;
}
/**
* Set statusPro
*
* @param string $statusPro
* @return AdsList
*/
public function setStatusPro($statusPro)
{
$this->statusPro = $statusPro;
return $this;
}
/**
* Get statusPro
*
* @return string
*/
public function getStatusPro()
{
return $this->statusPro;
}
/**
* Set creationtime
*
* @param \DateTime $creationtime
* @return test
*/
public function setCreationtime($creationtime)
{
$this->creationtime = $creationtime;
return $this;
}
/**
* Get creationtime
*
* @return \DateTime
*/
public function getCreationtime()
{
return $this->creationtime;
}
/**
* Set updatetime
*
* @param \DateTime $updatetime
* @return Test
*/
public function setUpdatetime($updatetime)
{
$this->updatetime = $updatetime;
return $this;
}
/**
* Get updatetime
*
* @return \DateTime
*/
public function getUpdatetime()
{
return $this->updatetime;
}
/**
* Set publication
*
* @param boolean $publication
* @return test
*/
public function setPublication($publication)
{
$this->publication = $publication;
return $this;
}
/**
* Get publication
*
* @return boolean
*/
public function getPublication()
{
return $this->publication;
}
// Callback
/**
* @ORM\PreUpdate
*/
public function udpateDate()
{
$this->setUpdatetime(new \DateTime());
}
/**
* Add photos
*
* @param \obbex\AdsBundle\Entity\Photos $photos
* @return AdsList
*/
public function addPhoto(\obbex\AdsBundle\Entity\Photos $photos)
{
$this->photos[] = $photos;
return $this;
}
/**
* Remove photos
*
* @param \obbex\AdsBundle\Entity\Photos $photos
*/
public function removePhoto(\obbex\AdsBundle\Entity\Photos $photos)
{
$this->photos->removeElement($photos);
}
}
这是obbex / AdsBundle / Entity / Photos类:
<?php
namespace obbex\AdsBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\HttpFoundation\File\UploadedFile;
/**
* Photos
*
* @ORM\Table()
* @ORM\Entity(repositoryClass="obbex\AdsBundle\Entity\PhotosRepository")
* @ORM\HasLifecycleCallbacks
*/
class Photos
{
/**
* @ORM\ManyToOne(targetEntity="obbex\AdsBundle\Entity\AdsList", inversedBy="photos")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="adslist_id", referencedColumnName="id")
* })
*/
protected $adslist;
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="photo_path", type="string", length=255)
*/
private $photoPath;
/**
* @var string
*
* @ORM\Column(name="thumbnail_path", type="string", length=255)
*/
private $thumbnailPath;
private $file;
private $tempFilename;
public function setFile(UploadedFile $file)
{
$this->file = $file;
if (null !== $this->photoPath) {
$this->tempFilename = $this->photoPath;
// reset values
}
}
public function getFile()
{
return $this->file;
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set photoPath
*
* @param string $photoPath
* @return Photos
*/
public function setPhotoPath($photoPath)
{
$this->photoPath = $photoPath;
return $this;
}
/**
* Get photoPath
*
* @return string
*/
public function getPhotoPath()
{
return $this->photoPath;
}
/**
* Set thumbnailPath
*
* @param string $thumbnailPath
* @return Photos
*/
public function setThumbnailPath($thumbnailPath)
{
$this->thumbnailPath = $thumbnailPath;
return $this;
}
/**
* Get thumbnailPath
*
* @return string
*/
public function getThumbnailPath()
{
return $this->thumbnailPath;
}
/**
*
* @ORM\PrePersist()
* @ORM\PreUpdate()
*/
public function preUpload()
{
if($this->file === null){
return;
}
//$this->extension = $this->file->guessExtention(); // In my case I don't need it
$this->photoPath = $this->file->getClientOriginalName();
}
/**
* @ORM\PostPersist()
* @ORM\PostUpdate()
*/
public function upload()
{
if($this->file === null){
return;
}
if(null !== $this->tempFilename)
{
$oldFile = $this->getUploadRootDir().'/'.$this->tempFilename;
if(file_exists($oldFile)){
unlink($oldFile);
}
}
$this->file->move(
$this->getUploadRootDir(),
$this->id.'.'.$this->photoPath
);
}
/**
* @ORM\PreRemove()
*/
public function PreRemoveUpload()
{
$this->tempFilename = $this->getUploadRootDir().'/'.$this->id.''.$this->photoPath;
}
/**
* @ORM\PostRemove()
*/
public function removeUpload()
{
if(filex_exists($this->tempFilename))
{
unlink($this->tempFilename);
}
}
public function getUploadDir()
{
return '/upload/img';
}
public function getUploadRootDir()
{
return __DIR__.'/../../../../web'.$this->getUploadDir();
}
public function getWebPath()
{
return $this->getUploadDir().'/'.$this->getId().'.'.$this->getPhotoPath();
}
public function getMyWebPath()
{
return $this->getUploadDir().'/'.$this->getPhotoPath();
}
/**
* Set adslist
*
* @param \obbex\AdsBundle\Entity\AdsList $adslist
* @return Photos
*/
public function setAdslist(\obbex\AdsBundle\Entity\AdsList $adslist = null)
{
$this->adslist = $adslist;
return $this;
}
/**
* Get adslist
*
* @return \obbex\AdsBundle\Entity\AdsList
*/
public function getAdslist()
{
return $this->adslist;
}
}
答案 0 :(得分:0)
我在addPhotos方法中添加了这个:
public function addPhoto(\obbex\AdsBundle\Entity\Photos $photos)
{
$this->photos[] = $photos;
$photos->setAdslist($this); // I added this line, now the photo adslist_id is correctly set
return $this;
}