gpt4 book ai didi

Symfony2 : Retrieve entity and modify it ==> SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '1-170' for key 'PRIMARY'

转载 作者:行者123 更新时间:2023-12-04 05:53:30 25 4
gpt4 key购买 nike

我有一个实体,我想更改一些值。

所以

  • 我找回了我的实体
  • 我更改值
  • 我坚持我的实体并刷新

  • ...但我有这个错误:
    SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '1-170' for key 'PRIMARY' 

    这里的代码
    /**
    *
    * @ORM\Table(name="entity")
    * @ORM\Entity(repositoryClass="App\Bundle\Entity\EntityRepository")
    * @ORM\OrderBy({"date" = "ASC"})
    */
    class Entity
    {
    /**
    * @var integer $id
    *
    * @ORM\Column(name="id", type="integer")
    * @ORM\Id
    * @ORM\GeneratedValue(strategy="AUTO")
    */
    private $id;
    // ...
    }

    我像这样检索我的实体:
    function get(){
    $query = $repository->createQueryBuilder('E')
    ->select ('E')
    ->where('(E.type=:type) AND (E.m = :id)')
    ->setParameter('type', $pType)
    ->setParameter('id', $pMediaId)
    ->getQuery();
    return $query->getResult();
    }

    并坚持
    $entity = $this->em->get(); // I have the correct value
    entity->setTitle('My new title');
    $this->em->persist($entity);

    $this->em->flush();

    请帮我

    拜拜

    山姆

    最佳答案

    您不需要持久化实体。您只需要刷新更改,持久化尝试创建新记录。

    编辑:

    直接来自 SF2 文档:

    http://symfony.com/doc/current/book/doctrine.html

    更新一个对象只涉及三个步骤:

  • 从 Doctrine 中获取对象;
  • 修改对象;
  • 在实体管理器上调用flush()

  • 请注意,不需要调用 $em->persist($product)。回想一下,这个方法只是告诉 Doctrine 管理或“监视” $product 对象。在本例中,由于您从 Doctrine 获取了 $product 对象,因此它已经被管理。

    我在编辑实体时遇到了类似的问题,取出持久化解决了这个问题。很抱歉这次不是这样。

    关于Symfony2 : Retrieve entity and modify it ==> SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '1-170' for key 'PRIMARY' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9789743/

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