Symfony-创建新实体时重复密钥

时间:2018-07-27 14:37:53

标签: php symfony key entity

好家伙,简单又快速的问题是,我如何创建多个新实体而又没有错误“键'UNIQ'重复输入...”,我这样做是这样的:

    $userInterest_1 = new UserInterest();
    $userInterest_2 = new UserInterest();
    $userInterest_3 = new UserInterest();

但是userInterest 1 2和3具有相同的ID密钥,所以我如何在没有相同密钥的情况下创建3个实体:(我试图在创建新的无效实体之前坚持并刷新)

如果您要的话,就这么简单,如果可以解决所有问题,我会把所有代码都放进去:)

$em_category = $this->getDoctrine()->getManager();
    $em = $this->getDoctrine()->getManager();

    $userInterest_1 = new UserInterest();
    $userInterest_1->setUser($user);
    $category_1 = $em_category->getRepository('AppBundle:Category')->findOneBy(array('id' => ($request->get('id_category_1'))));
    if ($category_1 === NULL) {
        return new JsonResponse([
            'success' => "false",
            'message' => "Categorie 1 non renseignée"
        ]);
    }
    $userInterest_1->setContent($request->get('content_1'));
    $userInterest_1->setCategory($category_1);
    $em->persist($userInterest_1);
    $em->flush();

    $userInterest_2 = new UserInterest();
    $userInterest_2->setUser($user);
    $category_2 = $em_category->getRepository('AppBundle:Category')->findOneBy(array('id' => ($request->get('id_category_2'))));
    if ($category_2 === NULL) {
        return new JsonResponse([
            'success' => "false",
            'message' => "Categorie 2 non renseignée"
        ]);
    }
    $userInterest_2->setContent($request->get('content_2'));
    $userInterest_2->setCategory($category_2);
    $em->persist($userInterest_2);
    $em->flush();


    $userInterest_3 = new UserInterest();
    $userInterest_3->setUser($user);
    $category_3 = $em_category->getRepository('AppBundle:Category')->findOneBy(array('id' => ($request->get('id_category_3'))));
    if ($category_3 === NULL) {
        return new JsonResponse([
            'success' => "false",
            'message' => "Categorie 3 non renseignée"
        ]);
    }
    $userInterest_3->setContent($request->get('content_3'));
    $userInterest_3->setCategory($category_3);
    $em->persist($userInterest_3);
    $em->flush();

我的json POST方法如下:

{
"username": "eTytyoftyooy6o7o",
"password": "Tsvyfviftypooyotô7uo",
"email": "ls.sivpdtrokt63@gmal.com",
"birth": "1999-04-26T18:25:43-05:00",
"id_category_1": "1",
"content_1": "Dog",
"id_category_2": "4",
"content_2": "Tennis",
"id_category_3": "5",
"content_3": "Netflix"

}

或者也许我应该做类似的事情(但是我不知道如何收钱):

{
"username": "eTytyoftyooy6o7o",
"password": "Tsvyfviftypooyotô7uo",
"email": "ls.sivpdtrokt63@gmal.com",
"birth": "1999-04-26T18:25:43-05:00",
"category": [{
    "id_category_1": "1",
    "content_1": "Chien",
},
    "id_category_2": "4",
    "content_2": "Tennis",
},
    "id_category_3": "5",
    "content_3": "Dora"
}]
}

ps:当我只刷新一个和第二个作为唯一值良好的id时,没有错误的代码与之前的代码相同:)(而且我的id在strategie = AUTO中)

(有人可能会在symfony论坛上看到我很多,但我正在尝试自己学习,所以你们是我的老师)

编辑:我添加实体:

UserInterest实体

 /**
 * UserInterest
 *
 * @ORM\Table(name="user_interest")
 * @ORM\Entity
 */
class UserInterest
{
/**
 * @var string
 *
 * @ORM\Column(name="content", type="string", length=255, nullable=false)
 */
private $content;

/**
 * @var integer
 *
 * @ORM\Column(name="id", type="integer", nullable=false)
 * @ORM\Id()
 * @ORM\GeneratedValue(strategy="AUTO")
 */
protected $id;

/**
 * @ORM\ManyToOne(targetEntity="category", inversedBy="intrests")
 */
private $category;

/**
 * @var \AppBundle\Entity\User
 *
 * @ORM\GeneratedValue(strategy="NONE")
 * @ORM\OneToOne(targetEntity="User")
 * @ORM\JoinColumns({
 *   @ORM\JoinColumn(name="user_id", referencedColumnName="id")
 * })
 */
private $user;

/**
 * @return string
 */
public function getContent()
{
    return $this->content;
}

/**
 * @param string $content
 */
public function setContent($content)
{
    $this->content = $content;
}

/**
 * @return int
 */
public function getId()
{
    return $this->id;
}

/**
 * @param int $id
 */
public function setId($id)
{
    $this->id = $id;
}

/**
 * @return User
 */
public function getUser()
{
    return $this->user;
}

/**
 * @param User $user
 */
public function setUser($user)
{
    $this->user = $user;
}

/**
 * @return mixed
 */
public function getCategory()
{
    return $this->category;
}

/**
 * @param mixed $category
 */
public function setCategory($category)
{
    $this->category = $category;
}

}

类别实体:

/**
* UserInterest
*
* @ORM\Table(name="category")
* @ORM\Entity
*/
class Category
{
/**
 * @var string
 *
 * @ORM\Column(name="content_c", type="string", length=255, nullable=false)
 */
private $content;

/**
 * @var integer
 *
 * @ORM\Column(name="id", type="integer", nullable=false)
 * @ORM\Id()
 * @ORM\GeneratedValue(strategy="AUTO")
 */
protected $id;

/**
 * @ORM\OneToMany(targetEntity="UserInterest", mappedBy="category")
 */
protected $interests;

/**
 * @return string
 */
public function getContent()
{
    return $this->content;
}

/**
 * @param string $content
 */
public function setContent($content)
{
    $this->content = $content;
}

/**
 * @return integer
 */
public function getId()
{
    return $this->id;
}

/**
 * @param User $id
 */
public function setId($id)
{
    $this->id = $id;
}

}

1 个答案:

答案 0 :(得分:0)

您应该不会遇到这个问题。我一直在使用该方法,并且始终创建具有不同ID的实体。 无论如何,您可以检查表的最后一个ID,并将下一个ID分配给新实体。那必须解决问题。