gpt4 book ai didi

mysql - Symfony2中如何允许重复输入,SQLSTATE[23000]?

转载 作者:行者123 更新时间:2023-11-29 11:44:54 27 4
gpt4 key购买 nike

例如,我有产品[香蕉]和产品[橙色],我希望这两个产品在数据库中使用相同的图片。但是,当我尝试添加与第一个产品具有相同图片的第二个产品时,我收到此错误:

SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry'1' for key 'UNIQ_5A8A6C8D3DA5256D'

我的媒体实体:

 <?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;
}

}

最佳答案

就像评论中所说,一对一关联涉及您不能将两个Post与相同的Media关联。

要允许此操作,请将您的关联更改为:

/**
* @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;

关于mysql - Symfony2中如何允许重复输入,SQLSTATE[23000]?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35128636/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com