例如我有产品[香蕉]和产品[橙色]我希望两个产品在数据库中使用相同的图片。但是当我试图添加第二个产品与相同的图片像第一个产品我得到这个错误:
SQLSTATE [23000]:完整性约束违规:1062重复条目 密钥'UNIQ_5A8A6C8D3DA5256D'的'1'
媒体:
<?php
namespace FLY\BookingsBundle\Entity;
use Application\Sonata\UserBundle\Entity\User;
use Doctrine\ORM\Mapping as ORM;
/**
* Media
*
* @ORM\Table("media")
* @ORM\Entity(repositoryClass="FLY\BookingsBundle\Entity\MediaRepository")
*/
class Media
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="path", type="string", length=255)
*/
private $path;
/**
* @var string
*
* @ORM\Column(name="alt", type="string", length=125)
*/
private $alt;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set path
*
* @param string $path
* @return Media
*/
public function setPath($path)
{
$this->path = $path;
return $this;
}
/**
* Get path
*
* @return string
*/
public function getPath()
{
return $this->path;
}
/**
* Set alt
*
* @param string $alt
* @return Media
*/
public function setAlt($alt)
{
$this->alt = $alt;
return $this;
}
/**
* Get alt
*
* @return string
*/
public function getAlt()
{
return $this->alt;
}
}
发表:
<?php
namespace FLY\BookingsBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Application\Sonata\UserBundle\Entity\User;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
use JMS\SecurityExtraBundle\Annotation\Secure;
/**
* Post
*
* @ORM\Table(name="post")
* @ORM\Entity(repositoryClass="FLY\BookingsBundle\Entity\PostRepository")
*/
class Post
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORM\OneToOne(targetEntity="FLY\BookingsBundle\Entity\Media", cascade={"persist","remove"})
* @ORM\JoinColumn(nullable=true)
*/
private $image;
/**
* @var string
*
* @ORM\Column(name="description", type="text" , length=125)
*/
private $description;
/**
* @var string
*
* @ORM\Column(name="airport", type="string")
*/
protected $airport;
/**
* @var string
*
* @ORM\Column(name="airport1", type="string")
*/
protected $airport1;
/**
* @var \DateTime
*
* @ORM\Column(name="departuredate", type="datetime")
*/
private $departuredate;
/**
* @var \DateTime
*
* @ORM\Column(name="arrivaldate", type="datetime")
*/
private $arrivaldate;
/**
* @var float
*
* @ORM\Column(name="pounds", type="float")
*/
private $pounds;
/**
* @var float
*
* @ORM\Column(name="price", type="float")
*/
private $price;
/**
*
*
* @ORM\ManyToOne(targetEntity="Application\Sonata\UserBundle\Entity\User")
* @ORM\JoinColumn(onDelete="CASCADE")
* @Security("user.getId() == post.getUser()")
*/
private $user;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set airport
*
* @param array $airport
* @return Post
*/
public function setAirport($airport)
{
$this->airport = $airport;
return $this;
}
/**
* Get airport
*
* @return string
*/
public function getAirport()
{
return $this->airport;
}
/**
* Set airport1
*
* @param array $airport1
* @return Post
*/
public function setAirport1($airport1)
{
$this->airport1 = $airport1;
return $this;
}
/**
* Get airport1
*
* @return string
*/
public function getAirport1()
{
return $this->airport1;
}
/**
* Set departuredate
*
* @param \DateTime $departuredate
* @return Post
*/
public function setDeparturedate($departuredate)
{
$this->departuredate = $departuredate;
return $this;
}
/**
* Get departuredate
*
* @return \DateTime
*/
public function getDeparturedate()
{
return $this->departuredate;
}
/**
* Set arrivaldate
*
* @param \DateTime $arrivaldate
* @return Post
*/
public function setArrivaldate($arrivaldate)
{
$this->arrivaldate = $arrivaldate;
return $this;
}
/**
* Get arrivaldate
*
* @return \DateTime
*/
public function getArrivaldate()
{
return $this->arrivaldate;
}
/**
* Set price
*
* @param float $price
* @return Post
*/
public function setPrice($price)
{
$this->price = $price;
return $this;
}
/**
* Get price
*
* @return float
*/
public function getPrice()
{
return $this->price;
}
/**
* @return User
*/
public function getUser()
{
return $this->user;
}
/*
* @param User $user
*/
public function setUser(User $user)
{
$this->user = $user;
return $this;
}
/**
* Set image
*
* @param \FLY\BookingsBundle\Entity\Media $image
* @return Post
*/
public function setImage(\FLY\BookingsBundle\Entity\Media $image)
{
$this->image = $image;
return $this;
}
/**
* Get image
*
* @return \FLY\BookingsBundle\Entity\Media
*/
public function getImage()
{
return $this->image;
}
/**
* Set description
*
* @param string $description
* @return Post
*/
public function setDescription($description)
{
$this->description = $description;
return $this;
}
/**
* Get description
*
* @return string
*/
public function getDescription()
{
return $this->description;
}
}
答案 0 :(得分:1)
如评论中所述,one-to-one
关联涉及您不能将两个Post
与Media
相关联。{/ p>
要允许此操作,请更改您的关联:
/**
* @ORM\OneToOne(targetEntity="FLY\BookingsBundle\Entity\Media", cascade={"persist","remove"})
* @ORM\JoinColumn(nullable=true)
*/
private $image;
致:
/**
* @ORM\ManyToOne(targetEntity="FLY\BookingsBundle\Entity\Media", cascade={"persist","remove"})
* @ORM\JoinColumn(nullable=true)
*/
private $image;