- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
所以我使用的是 Symfony 2,有时在调用 updateAction 时,我会从 symfony 中收到以下 500 错误:
"An exception occurred while executing 'INSERT INTO topic_sources (platform, authors, keywords, rss_feeds, topic_id) VALUES (?, ?, ?, ?, ?)' with params ["facebook", "153213781413350", null, "https://www.facebook.com/HuffPostWomen", 4641]: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '4641-facebook' for key 'PRIMARY'"...etc
public function updateAction($id) {
$topic = $this->getTopic($id);
if (!$topic) {
throw $this->createNotFoundException('No topic found for id ' . $id);
}
$originalTopicSources = new ArrayCollection();
// Grab all the topic sources on the page
foreach ($topic->getTopicSources() as $topicSource) {
$originalTopicSources->add($topicSource);
}
// Get all the Request Data
$request = $this->getRequest();
$requestData = $request->request->all();
$imageData = $request->get("topic-image");
$em = $this->getDoctrine()
->getManager();
// Bind the form for validation
$editTopicForm = $this->createForm(new TopicType($em), $topic);
$editTopicForm->bind($request);
if ($editTopicForm->isValid()) {
// Grab image and decode
if ($imageData) {
list($type, $imageData) = explode(';', $imageData);
list(, $imageData) = explode(',', $imageData);
$data = base64_decode($imageData);
$filename = "$id." . explode("/", $type)[1];
$topic->setImage($filename);
file_put_contents($this->get('kernel')->getRootDir() . "/../web/uploads/topics/$filename", $data);
}
set_time_limit(10);
// Compare the original topic sources with the newly passed in form and remove any topic sources that aren't common between the two
foreach ($originalTopicSources as $topicSource) {
if (false === $topic->getTopicSources()->contains($topicSource)) {
$em->remove($topicSource);
}
}
$em->persist($topic);
$em->flush();
$this->addFlash('success','The topic has been updated successfully :)');
}
$project = $topic->getProject();
$projectForm = $this->createForm(new ProjectType(array("user" => $this->getUser())), $project);
$newTopic = new Topic();
$newTopicForm = $this->createForm(new NewTopicType(), $newTopic);
// Get tag names
$em = $this->getDoctrine()
->getEntityManager();
$response = array();
$tags = $em->getRepository('AppBundle:Tag')->findAll();
$tagNames = array();
foreach ($tags as $tag) {
array_push($tagNames, $tag->getName());
}
return $this->render('AppBundle:Topic:edit.html.twig', array(
'project' => $topic->getProject(),
'projectForm' => $projectForm->createView(),
'newTopicForm' => $newTopicForm->createView(),
'tags' => $tagNames,
'topic' => $topic,
'editTopicForm' => $editTopicForm->createView(),
'facebook_app_id' => $this->container->getParameter('facebook_app_id_'.$this->get('kernel')->getEnvironment()),
'facebook_app_secret' => $this->container->getParameter('facebook_app_secret_'.$this->get('kernel')->getEnvironment()),
'facebook_app_access_token' => $this->container->getParameter('facebook_app_access_token_'.$this->get('kernel')->getEnvironment())
));
}
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\ORM\Event\LifecycleEventArgs;
use Symfony\Component\HttpFoundation\File\File;
use Symfony\Component\Validator\Constraints as Assert;
use JMS\Serializer\Annotation\Exclude;
use JMS\Serializer\Annotation\MaxDepth;
/**
* Topic
*
* @ORM\Table(name="topics", indexes={@ORM\Index(name="project_id", columns={"project_id"})})
* @ORM\Entity(repositoryClass="AppBundle\Entity\Repository\TopicRepository")
* @ORM\HasLifecycleCallbacks()
*/
class Topic {
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="name", type="string", length=255, nullable=false)
* @Assert\NotBlank(message="Listeners require a name")
*
*/
private $name;
/**
* @var string
*
* @ORM\Column(name="description", type="text")
*
*/
private $description;
/**
* Image file
*
* @var File
*
* @Assert\File(
* maxSize = "5M",
* mimeTypes = {"image/jpeg", "image/gif", "image/png", "image/tiff"},
* maxSizeMessage = "The maxmimum allowed file size is 5MB.",
* mimeTypesMessage = "Only the filetypes image are allowed."
* )
*/
protected $imageFile;
/**
* @ORM\Column(type="string", length=255, name="image_file")
*
* @var string $image
*/
protected $image;
/**
* @var boolean
*
* @ORM\Column(name="exclude_from_api", type="boolean", nullable=true, options={"default": false})
*/
protected $excludeFromApi = false;
/**
* @var integer
*
* @ORM\Column(name="feed_size", type="integer", options={"default": 50})
* @Assert\GreaterThanOrEqual(
* message = "A listener's feed size cannot be less than 0",
* value = 0
* )
*
*/
private $feedSize = 50;
/**
* @var boolean
*
* @ORM\Column(name="include_parents", type="boolean", nullable=true, options={"default": false})
*/
private $includeParents = false;
/**
* @var boolean
*
* @ORM\Column(name="track_urls", type="boolean", nullable=true, options={"default": false})
*/
private $trackUrls = false;
/**
* @var boolean
*
* @ORM\Column(name="active", type="boolean", nullable=true, options={"default": true})
*/
private $active = true;
/**
* @var \DateTime
*
* @ORM\Column(name="updated_at", type="datetime", nullable=false)
*/
private $updatedAt;
/**
* @var \AppBundle\Entity\Project
*
* @ORM\ManyToOne(targetEntity="AppBundle\Entity\Project", inversedBy="topics")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="project_id", referencedColumnName="id")
* })
*
* @Exclude
*
*/
private $project;
/**
* @var \Doctrine\Common\Collections\Collection
*
* @ORM\ManyToMany(targetEntity="AppBundle\Entity\Rule", mappedBy="topics", cascade={"all"})
* @ORM\JoinTable(name="topics_rules",
* joinColumns={
* @ORM\JoinColumn(name="topic_id", referencedColumnName="id")
* },
* inverseJoinColumns={
* @ORM\JoinColumn(name="rule_id", referencedColumnName="id")
* }
* )
*/
private $rules;
/**
* @var \AppBundle\Entity\TopicSource
*
* @ORM\OneToMany(targetEntity="AppBundle\Entity\TopicSource", mappedBy="topic", cascade={"all"})
*/
private $topicSources;
/**
* @var \AppBundle\Entity\Historical
*
* @ORM\OneToMany(targetEntity="AppBundle\Entity\Historical", mappedBy="topic", cascade={"all"})
*
* @MaxDepth(1)
*
*/
private $historicals;
/**
* @var \Doctrine\Common\Collections\Collection
*
* @ORM\ManyToMany(targetEntity="AppBundle\Entity\Entry", inversedBy="topics")
* @ORM\JoinTable(name="entries_topics",
* joinColumns={
* @ORM\JoinColumn(name="topic_id", referencedColumnName="id")
* },
* inverseJoinColumns={
* @ORM\JoinColumn(name="entry_id", referencedColumnName="id"),
* @ORM\JoinColumn(name="platform", referencedColumnName="platform")
* }
* )
*/
private $entries;
/**
* @var \Doctrine\Common\Collections\Collection
*
* @ORM\ManyToMany(targetEntity="AppBundle\Entity\Tag", inversedBy="topics", cascade={"persist"})
* @ORM\JoinTable(name="topics_tags",
* joinColumns={
* @ORM\JoinColumn(name="topic_id", referencedColumnName="id")
* },
* inverseJoinColumns={
* @ORM\JoinColumn(name="tag_id", referencedColumnName="id")
* }
* )
*/
private $tags;
/**
* Constructor
*/
public function __construct() {
$this->topicSources = new \Doctrine\Common\Collections\ArrayCollection();
$this->historicals = new \Doctrine\Common\Collections\ArrayCollection();
$this->rules = new \Doctrine\Common\Collections\ArrayCollection();
$this->entries = new \Doctrine\Common\Collections\ArrayCollection();
$this->tags = new \Doctrine\Common\Collections\ArrayCollection();
$this->setUpdatedAt(new \DateTime());
}
/**
* @ORM\PreUpdate
*/
public function setUpdatedAtValue() {
$this->setUpdatedAt(new \DateTime());
}
/**
* Get id
*
* @return integer
*/
public function getId() {
return $this->id;
}
/**
* Set name
*
* @param string $name
* @return Topic
*/
public function setName($name) {
$this->name = $name;
return $this;
}
/**
* Get name
*
* @return string
*/
public function getName() {
return $this->name;
}
/**
* Get Exclude from API
*
* @return boolean
*/
public function getExcludeFromApi() {
return $this->excludeFromApi;
}
/**
* Get description
*
* @return string
*/
public function getDescription() {
return $this->description;
}
/**
* Set description
*
* @param string $description
* @return Topic
*/
public function setDescription($description) {
$this->description = $description;
return $this;
}
/**
* Set Exclude From API
*
* @param boolean $excludeFromApi
* @return Topic
*/
public function setExcludeFromApi($excludeFromApi) {
$this->excludeFromApi = $excludeFromApi;
return $this;
}
/**
* Set feedSize
*
* @param integer $feedSize
* @return Topic
*/
public function setFeedSize($feedSize) {
$this->feedSize = $feedSize;
return $this;
}
/**
* Get feedSize
*
* @return integer
*/
public function getFeedSize() {
return $this->feedSize;
}
/**
* Set includeParents
*
* @param boolean $includeParents
* @return Topic
*/
public function setIncludeParents($includeParents) {
$this->includeParents = $includeParents;
return $this;
}
/**
* Get includeParents
*
* @return boolean
*/
public function getIncludeParents() {
return $this->includeParents;
}
/**
* Set trackUrls
*
* @param boolean $trackUrls
* @return Topic
*/
public function setTrackUrls($trackUrls) {
$this->trackUrls = $trackUrls;
return $this;
}
/**
* Get trackUrls
*
* @return boolean
*/
public function getTrackUrls() {
return $this->trackUrls;
}
/**
* Set active
*
* @param boolean $active
* @return Topic
*/
public function setActive($active) {
$this->active = $active;
return $this;
}
/**
* Get active
*
* @return boolean
*/
public function getActive() {
return $this->active;
}
/**
* Set updatedAt
*
* @param \DateTime $updatedAt
* @return Topic
*/
public function setUpdatedAt($updatedAt) {
$this->updatedAt = $updatedAt;
return $this;
}
/**
* Get updatedAt
*
* @return \DateTime
*/
public function getUpdatedAt() {
return $this->updatedAt;
}
/**
* Set project
*
* @param \AppBundle\Entity\Project $project
* @return Topic
*/
public function setProject(\AppBundle\Entity\Project $project = null) {
$this->project = $project;
# ~EN (2015): feed size is not set properly when topic is init'd, only updating $project would update this topic's feed size
if($feedSize = $this->project->getFeedSize()){
$this->feedSize = $feedSize;
}
return $this;
}
/**
* Get project
*
* @return \AppBundle\Entity\Project
*/
public function getProject() {
return $this->project;
}
/**
* Add rule
*
* @param \AppBundle\Entity\Rule $rule
* @return Topic
*/
public function addRule(\AppBundle\Entity\Rule $rule) {
$rule->addTopic($this);
$this->rules[] = $rule;
return $this;
}
/**
* Remove rule
*
* @param \AppBundle\Entity\Rule $rule
*/
public function removeRule(\AppBundle\Entity\Rule $rule) {
$this->rules->removeElement($rule);
}
/**
* Get rules
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getRules() {
return $this->rules;
}
/**
* Add topicSource
*
* @param \AppBundle\Entity\TopicSource $topicSource
* @return Topic
*/
public function addTopicSource(\AppBundle\Entity\TopicSource $topicSource) {
$topicSource->setTopic($this);
$this->topicSources[] = $topicSource;
return $this;
}
/**
* Remove topicSource
*
* @param \AppBundle\Entity\topicSource $topicSource
*/
public function removeTopicSource(\AppBundle\Entity\TopicSource $topicSource) {
$this->topicSources->removeElement($topicSource);
}
/**
* Get topicSources
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getTopicSources() {
return $this->topicSources;
}
/**
* @var \Doctrine\Common\Collections\Collection
*/
private $topicSource;
/**
* Get topicSource
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getTopicSource() {
return $this->topicSource;
}
/**
* Add historical
*
* @param \AppBundle\Entity\Historical $historical
* @return Historical
*/
public function addHistorical(\AppBundle\Entity\Historical $historical) {
$this->historicals[] = $historical;
return $this;
}
/**
* Remove historical
*
* @param \AppBundle\Entity\Historical $historical
*/
public function removeHistorical(\AppBundle\Entity\Historical $historical) {
$this->historicals->removeElement($historical);
}
/**
* Get historicals
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getHistoricals() {
return $this->historicals;
}
/**
* Add entry
*
* @param \AppBundle\Entity\Entry $entry
* @return Topic
*/
public function addEntry(\AppBundle\Entity\Entry $entry) {
$this->entries[] = $entry;
return $this;
}
/**
* Remove entry
*
* @param \AppBundle\Entity\Entry $entry
*/
public function removeEntry(\AppBundle\Entity\Entry $entry) {
$this->entries->removeElement($entry);
}
/**
* Get entries
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getEntries($limit = null) {
$to_return = array();
$max_entries = ($limit != null)?$limit:$this->getFeedSize();
foreach ($this->entries as $entry) {
if (--$max_entries <= 0)
break;
$to_return[] = $entry;
}
return $to_return;
//return new ArrayObject(array_slice((array) $this->entries, 0, $this->getFeedSize()));
}
/**
* Add tags
*
* @param \AppBundle\Entity\Tag $tags
* @return Topic
*/
public function addTag(\AppBundle\Entity\Tag $tags) {
$this->tags[] = $tags;
return $this;
}
/**
* Remove tags
*
* @param \AppBundle\Entity\Tag $tags
*/
public function removeTag(\AppBundle\Entity\Tag $tags) {
$this->tags->removeElement($tags);
}
/**
* Get tags
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getTags() {
return $this->tags;
}
/**
* Sets imageFile.
*
* @param File $imageFile
*/
public function setImageFile(File $imageFile = null) {
$this->imageFile = $imageFile;
// check if we have an old image path
if (isset($this->image)) {
// store the old name to delete after the update
$this->temp = $this->image;
$this->image = null;
} else {
$this->image = 'initial';
}
}
/**
* @return File
*/
public function getImageFile() {
return $this->imageFile;
}
/**
* @param string $imageName
*/
public function setImage($image) {
$this->image = $image;
}
/**
* @return string
*/
public function getImage() {
return $this->image;
}
/**
* Called before saving the entity
*
* @ORM\PrePersist()
* @ORM\PreUpdate()
*/
public function preUpload() {
$filename = sha1(uniqid(mt_rand(), true));
if (null !== $this->imageFile) {
$this->image = $filename . '.' . $this->imageFile->guessExtension();
}
}
/**
* Called before entity removal
*
* @ORM\PreRemove()
*/
public function removeUpload() {
// if ($file = $this->getAbsolutePath()) {
// unlink($file);
// }
}
/**
* Called after entity persistence
*
* @ORM\PostPersist()
* @ORM\PostUpdate()
*/
public function upload() {
// The file property can be empty if the field is not required
if (null === $this->imageFile) {
return;
}
// Use the original file name here but you should
// sanitize it at least to avoid any security issues
// move takes the target directory and then the
// target filename to move to
$this->imageFile->move(
"%kernel.root_dir%/../web/uploads/topics", $this->image
);
// Set the path property to the filename where you've saved the file
//$this->path = $this->file->getClientOriginalName();
// Clean up the file property as you won't need it anymore
$this->imageFile = null;
}
}
最佳答案
这是因为数据库中的字段之一是空的。因此,尝试删除表中的所有内容并再次生成 command
.
关于php - Symfony 2 : SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32227952/
我正在查看 SQL Server 2008 的 AdventureWorks 示例数据库,我在他们的创建脚本中看到他们倾向于使用以下内容: ALTER TABLE [Production].[Prod
我目前正在使用 PostgreSQL 9.5,想知道是否有可能在 ON CONFLICT ON CONSTRAINT 语句中包含 2 个约束的名称。我的sql如下 INSERT INTO LIVE.T
使用 htmlhelpers 可以限制你的助手将绑定(bind)到什么类型 public static HtmlString DatePicker(this HtmlHelper html,
我使用的是 Symfony 2.5,我的 Model 类如下: /** * @UserAssert\UserPasswordReset */ class ResetPassword { /** *
我有 3 个 View :A、B、C。 (A 和 B 的高度相等)开始时 B 的可见性消失,C 的顶部约束是 A 的底部,因此 C 出现在 A 下方。一段时间后,我将 A 的可见性更改为消失,将 B
在 Dojo NumberTextBox 的文档中,措辞引用了“Dojo 约束语言”,甚至包括有用的 link .不幸的是,链接指向的页面仅显示 this document has been depr
在我的表中,我有一个唯一的约束。在 hibernate 中,当我添加一个违反该约束的项目时,我想捕获它,因此它将更新而不是创建一个项目。 当我没有设置 try-catch block 时 up
我正在尝试在“或”UILabel 附近添加两条 1 像素线(由 UIViews 组成)。 除了我从 Interface Builder 中的第一张图片收到警告外,一切看起来都很好并且按预期工作: Le
我已经开始学习安卓了。我正在尝试使用 Google Map API。每次我尝试启动我的应用程序时,它都会崩溃,经过调查,我在 build.gradle 文件中发现了一个通知。 Please refer
我有自定义约束: @Target({FIELD, METHOD}) @Retention(RetentionPolicy.RUNTIME) @ConstraintComposition(Composi
我正在将 Graphql 服务器与 Prisma 一起使用。但是当我尝试运行代码时出现此错误我正在使用 const { GraphQLServer } = require('graphql-yoga'
更新到 com.android.support.constraint:constraint-layout:1.1.0 之后 约束布局崩溃说: All children of constraint la
我在 Xcode 10 中工作,在尝试向我的 View 添加一些非常简单的约束时遇到了一些错误。 我有一个 UICollectionViewCell,我正在向其添加一个 UIStackView。我调整
尝试在 Laravel 上创建一个待办事项列表应用程序,但是当我尝试单击按钮创建一个新的待办事项列表时,出现此错误: SQLSTATE[23000]: Integrity constraint vio
我正在编写一个基于网格的 View ,使用以下代码动态添加 NSLayoutConstraints for (x, column) in enumerate(board) { for (y,
我正在尝试使用 Constraint composition并希望为每个复合约束定义组,如下例所示:- 复合约束 @Target({ ElementType.FIELD, Elemen
我有一些添加了外键约束的表。它们与代码生成一起使用,以在生成的存储过程中设置特定的联接。 是否可以通过在事务中调用多个删除来覆盖这些约束,特别是 C# 中的“TransactionScope”,或者绝
我需要向现有 SQL Server 表添加约束,但前提是该表尚不存在。 我使用以下 SQL 创建约束。 ALTER TABLE [Foo] ADD CONSTRAINT [FK_Foo_Bar] FO
这是我的总输出: Executing SQL script in server ERROR: Error 1215: Cannot add foreign key constraint CREATE
我正在增加 Facebook SDK 登录按钮 (FBSDKLoginButton) 的大小。 Facebook SDK 源代码向 FBSDKLoginButton 添加了一个约束,height =
我是一名优秀的程序员,十分优秀!