1
我有以下兩個實體:小問題與堅持()
<?php
namespace Site\AnnonceBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Site\UserBundle\Entity\User;
/**
* Site\AnnonceBundle\Entity\Sujet
*
* @ORM\Table()
* @ORM\Entity(repositoryClass="Site\AnnonceBundle\Entity\SujetRepository")
*/
class Sujet
{
/**
* @var integer $id
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
//Some code...
/**
*
* @ORM\ManyToOne(targetEntity="Site\UserBundle\Entity\User")
*/
private $user;
//getter/setter....
用戶實體(FOSUserBundle):
<?php
namespace Site\UserBundle\Entity;
use FOS\UserBundle\Entity\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
* @ORM\Table()
*
*/
class User extends BaseUser{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
public function getId() {
return $this->id;
}
}
當我創建了一個 「Sujet」,我做了(在SujetController。 PHP):
$em = $this->getDoctrine()->getEntityManager();
$sujet->setResolu(false);
$em->persist($sujet);
$em->flush();
其作品,但插入的「Sujet」數據庫是指用戶零...所以在第二個版本我做了這個:
$em = $this->getDoctrine()->getEntityManager();
$sujet->setResolu(false);
$sujet->setUser(new User($this->get('session')->get('user_id'))) ;//the user is already in the DB
$em->persist($sujet);
$em->flush();
,但我得到這個錯誤:
A new entity was found through the relationship 'Site\AnnonceBundle\Entity\Sujet#user' that was not configured to cascade persist operations for entity: . Explicitly persist the new entity or configure cascading persist operations on the relationship. If you cannot find out which entity causes the problem implement 'Site\UserBundle\Entity\User#__toString()' to get a clue.
我不明白,我已經與其他ORM(JPA)的工作,並以這種方式工作...... 如何辨別真假「Sujet 「關於什麼與數據庫中已存在的實體有關?
(抱歉,如果我的英語不好)
編輯:它的工作對我來說:
$user = $this->get('security.context')->getToken()->getUser();
$sujet->setUser($user);
$em->persist($sujet);
$em->flush();