php - Persist a clone -


in problem deep cloning thought issue due deep/shallow copy.

i have vainly tested clone() , unserialize(serialize()) methods.

so tried write own clone function using setters/getters , realized issue, persisting one.

the fact succeeded in persisting clone of entity, in context.

the main difference between 2 situations in 1 case original object managed doctrine (this case i'm blocked), , in second case, original object persisted, don't have called flush() yet (and it's works fine).

so situation when persist do not persist many many relations :

public function duplicatecourseaction(request $request) {     if ($this->getrequest()->isxmlhttprequest() == false) {         return new response("bad request", 405);     }      $em = $this->getdoctrine()->getmanager();     $parameters = $request->request->all();     $course = $em->getrepository('enttimebundle:course')->findonebyid($parameters['id']);     $duplicate = clone $course;     $duplicate->setid(null);     $duplicate->setdate(new \datetime($parameters['date']));     $em->persist($duplicate);     $em->flush();     return new response("200"); } 

and situation whe it's works charm

        $em->persist($obj);         while ($new_date < $up_to) {             if ($this->isavailable($holidays, $new_date)) {                 $new_course = clone $obj;                 $new_course->setdate($new_date);                 $new_course->setuuid($uuid);                 $new_date = clone $new_date;                 $em->persist($new_course);             }             $new_date->modify($modifier);         }         $em->flush(); 

why working 1 situation ? there identical...

edit 1 : entities mapping

-course :

class course {  /**  * @orm\id  * @orm\column(type="integer")  * @orm\generatedvalue(strategy="auto")  */ protected $id;  /**  * @orm\column(type="string", length=50, unique=true, nullable=true)  */ protected $name;  /** * @orm\joincolumn(ondelete="cascade") * @orm\manytoone(targetentity="ent\homebundle\entity\campus", inversedby="courses") * @assert\notblank **/ protected $campus;  /** * @orm\joincolumn(ondelete="cascade") * @orm\manytoone(targetentity="ent\homebundle\entity\room", inversedby="courses") * @assert\notblank **/ protected $room;  /**  * @orm\manytomany(targetentity="ent\userbundle\entity\user", inversedby="courses", cascade={"persist"})  * @orm\jointable(name="course_teacher",  *  joincolumns={@orm\joincolumn(name="course_id", referencedcolumnname="id", ondelete="cascade")},  *  inversejoincolumns={@orm\joincolumn(name="teacher_id", referencedcolumnname="id", ondelete="cascade")}  * )  * @assert\notblank  */ private $teachers;  /** * @orm\joincolumn(ondelete="cascade") * @orm\manytoone(targetentity="matter", inversedby="courses") * @assert\notblank **/ protected $matter;  /** * @orm\joincolumn(ondelete="cascade") * @orm\manytoone(targetentity="\ent\userbundle\entity\grade", inversedby="courses") * @assert\notblank **/ protected $grade;  /** * @orm\column(type="datetime") * @assert\notblank **/ protected $date;  /** * @orm\column(type="time") * @assert\notblank **/ protected $duration;  /**  * @orm\column(type="string", length=30, nullable=true)  */ protected $uuid;  /**  * @orm\manytomany(targetentity="ent\timebundle\entity\course", mappedby="courses")  * @exclude */ protected $alerts;  public function __tostring() {     if (empty($this->getname())) {         $string = $this->getmatter().' - '.$this->getroom().' - ';         foreach ($this->getteachers() $count => $teacher) {             $string = $string . $teacher;             if ($count < count($this->getteachers()) - 1) {                 $string = $string . ', ';             }         }         return $string;     } else {         return $this->getname().' - '.$this->getroom();     } } /**  * constructor  */ public function __construct() {     $this->teachers = new arraycollection();     $this->alerts = new arraycollection(); }  public function __clone() {     // $this->id = null;     // $this->teachers = clone $this->teachers; } } 

-user (teacher) :

class user implements userinterface, \serializable { /**  * @orm\column(type="integer")  * @orm\id  * @orm\generatedvalue(strategy="auto")  */ protected $id;  /**  * @orm\column(type="string", length=30)  * @assert\notblank  */ protected $firstname;  /**  * @orm\column(type="string", length=30)  * @assert\notblank  */ protected $lastname;  /**  * @orm\column(type="string", length=70, unique=true)  * @assert\notblank  */ protected $username;  /**  * @gedmo\slug(fields={"username"}, updatable=false)  * @orm\column(length=50, unique=true)  */ protected $slug;  /**  * @orm\column(type="string", length=32)  * @exclude  */ protected $salt;  /**  * @orm\column(type="string", length=40)  * @exclude  */ protected $password;  /**  * @orm\column(type="string", length=255, nullable=true)  */ protected $picture_path;  /**  * @assert\file(maxsize="10m", mimetypesmessage="please upload valid image")  */ protected $picture;  /**  * @orm\column(type="string", length=60, unique=true)  * @exclude  * @assert\notblank  */ protected $email;  /**  * @orm\column(name="is_active", type="boolean")  */ protected $isactive;  /**  * @orm\manytoone(targetentity="group", inversedby="users")  * @orm\joincolumn(name="role_group", referencedcolumnname="role", ondelete="cascade")  */ protected $group;  /** * @orm\manytomany(targetentity="ent\homebundle\entity\campus", inversedby="users") * @exclude **/ protected $campuses;  /**  * @orm\onetomany(targetentity="\ent\newsbundle\entity\news", mappedby="user")  * @exclude  */ protected $news;  /**  * @orm\manytomany(targetentity="\ent\timebundle\entity\matter", inversedby="teachers", cascade={"persist"})  * @orm\jointable(name="user_matter",  *  joincolumns={@orm\joincolumn(name="user_id", referencedcolumnname="id", ondelete="cascade")},  *  inversejoincolumns={@orm\joincolumn(name="matter_id", referencedcolumnname="id", ondelete="cascade")}  * )  */ protected $matters;  /** * @orm\manytomany(targetentity="ent\userbundle\entity\grade") * @assert\notblank * @exclude **/ protected $grades;  /** * @orm\manytomany(targetentity="ent\timebundle\entity\course", mappedby="teachers") * @exclude **/ protected $courses;  /** * @orm\onetomany(targetentity="\ent\timebundle\entity\alert", mappedby="teacher") * @exclude **/ protected $alerts;  protected $temp;  public function __construct() {     $this->isactive = true;     $this->salt = md5(uniqid(null, true)); }  public function __tostring() {     return $this->getfullname(); } } 

edit 2 : solution

thanks paul andrieux function made clone object :

public function course_deep_clone($course) {     $em = $this->getdoctrine()->getmanager();     $clone = clone $course;      $clone->setteachers(array());     $teachers = $course->getteachers();     foreach ($teachers $teacher) {         $clone->addteacher($teacher);     }      return $clone; } 

thing manytomany related entities not cloned, try that:

public function duplicatecourseaction(request $request) {     if ($this->getrequest()->isxmlhttprequest() == false) {         return new response("bad request", 405);     }      $em = $this->getdoctrine()->getmanager();     $parameters = $request->request->all();     $course = $em->getrepository('enttimebundle:course')->findonebyid($parameters['id']);     $duplicate = clone $course;      $teachers = $course->getteachers();     $duplicate->setteachers($teachers);      $duplicate->setid(null);     $duplicate->setdate(new \datetime($parameters['date']));     $em->persist($duplicate);     $em->flush();     return new response("200"); } 

this way, persisting new relationship , new join table between 2 entities

edit: maybe cascading problem, gives ? :

$teachers = $course->getteachers();  foreach ($teachers $teacher) {      $teacher->addcourse($duplicate);      $em->persist($teacher);  } 

Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -