symfony - FOSUserBundle and persisting in database -
i have user entity class inherits fosuserbundle baseuser class. on other side avis entity class.
/** * avis * * @orm\table() * @orm\entity */ class avis { /** * @var integer * * @orm\column(name="id", type="integer") * @orm\id * @orm\generatedvalue(strategy="auto") */ private $id; /** * @var integer * * @assert\notblank(message = "merci d'attribuer une note", groups = {"default"}) * * @orm\column(name="note", type="smallint") */ private $note; /** * @var string * * @assert\notblank(message = "merci d'entrer un message", groups = {"default"}) * @assert\length(max = 500, maxmessage = "votre message doit contenir au maximum {{ limit }} caractères de longueur", groups = {"default"}) * * @orm\column(name="message", type="string", length=500) */ private $message; /** * @assert\type(type="lejardinebene\bundle\entity\user") * * @orm\onetoone(targetentity="lejardinebene\bundle\entity\user", cascade={"persist"}, inversedby="avis") * @orm\joincolumn(nullable=true) */ private $user; /** * user * * @orm\table(name="fos_user") * @orm\entity */ class user extends baseuser { /** * @var integer * * @orm\column(name="id", type="integer") * @orm\id * @orm\generatedvalue(strategy="auto") */ protected $id; /** * @assert\type(type="lejardinebene\bundle\entity\avis") * * @orm\onetoone(targetentity="lejardinebene\bundle\entity\avis", mappedby="user") * @orm\joincolumn(nullable=true) */ protected $avis;
so avis owner of bidirectional relation. in form display avis object fields , when validating form :
- avis persisted in database (that works)
- relation persisted => user_id in avis table , avis_id in fos_user table
here tried without success :
$user = $this->getuser(); $form = $this->createform('avis', $avis); $form->handlerequest($request); $avis = new avis();if ($form->isvalid()) { $entitymanager = $this->getdoctrine()->getmanager(); $entitymanager->persist($avis); $entitymanager->flush(); if($user) { $usermanager = $this->get('fos_user.user_manager'); $usermanager->updateuser($avis->getuser(), true); }
thanks. regards,
with one-to-one , many-to-many 1 table gets foreign-index column. in case "avis" table column user_id. have set $user property before persist. doctrine set $avis property in user class automatically after (re)loading.
$user = $this->getuser(); $avis = new avis(); $form = $this->createform('avis', $avis); $form->handlerequest($request); if ($form->isvalid()) { $entitymanager = $this->getdoctrine()->getmanager(); $avis->setuser($user); $entitymanager->persist($avis); $entitymanager->flush(); }
Comments
Post a Comment