php - Symfony FOS Rest Bundle Api Call Many to Many Relationship -


i seem have issue. , i'm sure has simple fix. have api uses fos rest bundle. have call sends post request postcreatetimesheetaction( request $request ) creates empty row in timesheet table. have call patchtimesheetaction laters adds data table. okay well. have call patchcareoptionaction( timesheet $timesheet ) creates row in table has many many relationship built careoptions.php(below) , timesheet.php(below). api calls work , function should. post them below here:

creates blank new timesheet row in timesheet table: (timesheet.php)

curl -h "accept: application/json" -h "content-type: application/json" -i -x post -d '{"homecare_homecarebundle_timesheet":""}' www.hc-timesheets-demo.com/app_dev.php/api/v1/create/timesheet

updates timesheet row data:

curl -i -h "accept: application/json" -h "content-type: application/json" -d '{"homecare_homecarebundle_timesheet":{"agency":"157","recipient":"154","pca":"16","service":"6"}}' -x patch www.hc-timesheets-demo.com/app_dev.php/api/v1/timesheet/31

and creates many many relationship:

curl -i -h "accept: application/json" -h "content-type: application/json" -d '{"homecare_homecarebundle_timesheet":{"careoption":[456,457] }}' -x patch www.hc-timesheets-demo.com/app_dev.php/api/v1/careoption/31

notice 31 @ end of patch request. id of timesheet created in table first api call. lets question! whenever call 3rd api call create many many rows in table. when call again not replace old rows new ones. adds more rows table. want many many table rows updated , old ones gone. need delete them out first? let me explain little more. if see in 3rd api call i'm adding 2 careoptions timesheet: (careoptions 456 , 457). if call again, , lets want add 458 , 459. want 456 , 457 automatically delete , gone. please me out??!!

here owning side of many many relationship

<?php //src/homecare/homecarebundle/entity/careoptions.php namespace homecare\homecarebundle\entity;  use doctrine\orm\mapping orm; use doctrine\common\collections\arraycollection;  /**  * caregoals  *  * @orm\table(name="careoptions")  * @orm\entity(repositoryclass="homecare\homecarebundle\entity\repository\careoptionsrepository")  */ class careoptions { /**  * @var integer  *  * @orm\column(name="id", type="integer")  * @orm\id  * @orm\generatedvalue(strategy="auto")  */ private $id;  /**  * @orm\onetomany(targetentity="careoptionstimesheets", mappedby="careoption")      */         private $care_options_timesheets;   /**  * @var string  *  * @orm\column(name="care_option", type="string", length=255)  */     private $careoption;       /**     * @orm\manytomany(targetentity="caregoals", mappedby="careoption")     */     private $caregoals;          /**     * @orm\manytomany(targetentity="timesheet", inversedby="careoption", cascade={"persist"})     */     private $timesheet;         public function __construct()      {          $this->care_options_timesheets = new arraycollection();                   $this->timesheet = new arraycollection();       }    /**  * id  *  * @return integer   */ public function getid() {     return $this->id; }            //add string method object can displayed in twig template     /*     public function __tostring()             {                 return $this->getcaregoal();             }     */   /**  * set careoption  *  * @param string $careoption  * @return careoptions  */ public function setcareoption($careoption) {     $this->careoption = $careoption;      return $this; }  /**  * careoption  *  * @return string   */ public function getcareoption() {     return $this->careoption; }  /**  * add care_options_timesheets  *  * @param \homecare\homecarebundle\entity\careoptions_timesheets $careoptionstimesheets  * @return careoptions  */ public function addcareoptionstimesheet(\homecare\homecarebundle\entity\careoptionstimesheets $careoptionstimesheets) {     $this->care_options_timesheets[] = $careoptionstimesheets;      return $this; }  /**  * remove care_options_timesheets  *  * @param \homecare\homecarebundle\entity\careoptions_timesheets $careoptionstimesheets  */ public function removecareoptionstimesheet(\homecare\homecarebundle\entity\careoptionstimesheets $careoptionstimesheets) {     $this->care_options_timesheets->removeelement($careoptionstimesheets); }  /**  * care_options_timesheets  *  * @return \doctrine\common\collections\collection   */ public function getcareoptionstimesheets() {     return $this->care_options_timesheets; }  /**  * add caregoals  *  * @param \homecare\homecarebundle\entity\caregoals $caregoals  * @return careoptions  */ public function addcaregoal(\homecare\homecarebundle\entity\caregoals $caregoals) {     $this->caregoals[] = $caregoals;      return $this; }  /**  * remove caregoals  *  * @param \homecare\homecarebundle\entity\caregoals $caregoals  */ public function removecaregoal(\homecare\homecarebundle\entity\caregoals $caregoals) {     $this->caregoals->removeelement($caregoals); }  /**  * caregoals  *  * @return \doctrine\common\collections\collection   */ public function getcaregoals() {     return $this->caregoals; }        public function __tostring() {         return $this->getcareoption();     }    /**  * add timesheet  *  * @param \homecare\homecarebundle\entity\timesheet $timesheet  * @return careoptions  */ public function addtimesheet(\homecare\homecarebundle\entity\timesheet $timesheet) {     $this->timesheet[] = $timesheet;      return $this; }  /**  * remove timesheet  *  * @param \homecare\homecarebundle\entity\timesheet $timesheet  */ public function removetimesheet(\homecare\homecarebundle\entity\timesheet $timesheet) {     $this->timesheet->removeelement($timesheet); }  /**  * timesheet  *  * @return \doctrine\common\collections\collection   */ public function gettimesheet() {     return $this->timesheet; } } 

here other side of many many relationship:

<?php //src/homecare/homecarebundle/entity/timesheet.php namespace homecare\homecarebundle\entity;  use doctrine\orm\mapping orm; use jms\serializer\annotation\type; use jms\serializer\annotation\serializedname;  /**  * timesheet  *  * @orm\table(name="timesheet")  * @orm\entity(repositoryclass="homecare\homecarebundle\entity\repository\timesheetrepository")  */ class timesheet { /**  * @var integer  *  * @orm\column(name="id", type="integer")  * @orm\id  * @orm\generatedvalue(strategy="auto")      * @type("integer")      * @serializedname("id")  */ private $id;    /**  * @orm\manytoone(targetentity="agency", inversedby="actualtimesheets")      * @orm\joincolumn(name="agency_id", referencedcolumnname="id")      * @type("homecare\homecarebundle\entity\agency")      * @serializedname("agency")  */ private $agency;    /**  * @orm\manytoone(targetentity="recipient", inversedby="actualtimesheets")      * @orm\joincolumn(name="recipient_id", referencedcolumnname="id")      * @type("homecare\homecarebundle\entity\recipient")      * @serializedname("recipient")  */ private $recipient;   /**  * @orm\manytoone(targetentity="pca", inversedby="actualtimesheets")      * @orm\joincolumn(name="pca_id", referencedcolumnname="id")      * @type("homecare\homecarebundle\entity\pca")      * @serializedname("pca")  */ private $pca;   /**  * @orm\manytoone(targetentity="services", inversedby="actualtimesheets")      * @orm\joincolumn(name="service_id", referencedcolumnname="id")      * @type("homecare\homecarebundle\entity\services")      * @serializedname("service")  */ private $service;          /**     * @orm\column(name="continuetimesheetnumber",type="integer",nullable=true)     * @type("integer")     * @serializedname("continuetimesheetnumber")     */     private $continuetimesheetnumber;          /**     * @orm\manytoone(targetentity="visitratios", inversedby="timesheets")     * @type("homecare\homecarebundle\entity\visitratios")     * @serializedname("visitratio")     */     private $visitratio;      /**  * @orm\manytoone(targetentity="timein", inversedby="timesheets")      * @type("homecare\homecarebundle\entity\timein")      * @serializedname("timein")  */ private $timein;     /**  * @orm\manytoone(targetentity="timeout", inversedby="timesheets")      * @type("homecare\homecarebundle\entity\timeout")      * @serializedname("timeout")  */ private $timeout;      /**  * @orm\manytoone(targetentity="files", inversedby="timesheets")      * @type("homecare\homecarebundle\entity\files")      * @serializedname("file")  */ private $file;        /**     * @orm\manytomany(targetentity="careoptions", mappedby="timesheet", cascade={"persist"})     * @type("homecare\homecarebundle\entity\careoptions")     * @serializedname("careoption")     */     private $careoption;     /**  * id  *  * @return integer   */ public function getid() {     return $this->id; }     /**  * set agency  *  * @param \homecare\homecarebundle\entity\agency $agency  * @return timesheet  */ public function setagency(\homecare\homecarebundle\entity\agency $agency = null) {     $this->agency = $agency;      return $this; }  /**  * agency  *  * @return \homecare\homecarebundle\entity\agency   */ public function getagency() {     return $this->agency; }  /**  * set recipient  *  * @param \homecare\homecarebundle\entity\recipient $recipient  * @return timesheet  */ public function setrecipient(\homecare\homecarebundle\entity\recipient $recipient = null) {     $this->recipient = $recipient;      return $this; }  /**  * recipient  *  * @return \homecare\homecarebundle\entity\recipient   */ public function getrecipient() {     return $this->recipient; }  /**  * set pca  *  * @param \homecare\homecarebundle\entity\pca $pca  * @return timesheet  */ public function setpca(\homecare\homecarebundle\entity\pca $pca = null) {     $this->pca = $pca;      return $this; }  /**  * pca  *  * @return \homecare\homecarebundle\entity\pca   */ public function getpca() {     return $this->pca; }  /**  * set service  *  * @param \homecare\homecarebundle\entity\services $service  * @return timesheet  */ public function setservice(\homecare\homecarebundle\entity\services $service = null) {     $this->service = $service;      return $this; }  /**  * service  *  * @return \homecare\homecarebundle\entity\services   */ public function getservice() {     return $this->service; }     /**  * set continuetimesheetnumber  *  * @param integer $continuetimesheetnumber  * @return timesheet  */ public function setcontinuetimesheetnumber($continuetimesheetnumber) {     $this->continuetimesheetnumber = $continuetimesheetnumber;      return $this; }  /**  * continuetimesheetnumber  *  * @return integer   */ public function getcontinuetimesheetnumber() {     return $this->continuetimesheetnumber; }  /**  * set visitratio  *  * @param \homecare\homecarebundle\entity\visitratios $visitratio  * @return timesheet  */ public function setvisitratio(\homecare\homecarebundle\entity\visitratios $visitratio = null) {     $this->visitratio = $visitratio;      return $this; }  /**  * visitratio  *  * @return \homecare\homecarebundle\entity\visitratios   */ public function getvisitratio() {     return $this->visitratio; }  /**  * set timein  *  * @param \homecare\homecarebundle\entity\timein $timein  * @return timesheet  */ public function settimein(\homecare\homecarebundle\entity\timein $timein = null) {     $this->timein = $timein;      return $this; }  /**  * timein  *  * @return \homecare\homecarebundle\entity\timein   */ public function gettimein() {     return $this->timein; }  /**  * set timeout  *  * @param \homecare\homecarebundle\entity\timeout $timeout  * @return timesheet  */ public function settimeout(\homecare\homecarebundle\entity\timeout $timeout = null) {     $this->timeout = $timeout;      return $this; }  /**  * timeout  *  * @return \homecare\homecarebundle\entity\timeout   */ public function gettimeout() {     return $this->timeout; }  /**  * set file  *  * @param \homecare\homecarebundle\entity\files $file  * @return timesheet  */ public function setfile(\homecare\homecarebundle\entity\files $file = null) {     $this->file = $file;      return $this; }  /**  * file  *  * @return \homecare\homecarebundle\entity\files   */ public function getfile() {     return $this->file; } /**  * constructor  */ public function __construct() {     $this->careoption = new \doctrine\common\collections\arraycollection(); }     /**  * add careoption  *  * @param \homecare\homecarebundle\entity\careoptions $careoption  * @return timesheet  */ public function addcareoption(\homecare\homecarebundle\entity\careoptions $careoption) {            $careoption->addtimesheet( $this );      $this->careoption[] = $careoption;      return $this; }  /**  * remove careoption  *  * @param \homecare\homecarebundle\entity\careoptions $careoption  */ public function removecareoption(\homecare\homecarebundle\entity\careoptions $careoption) {     $this->careoption->removeelement($careoption); }  /**  * careoption  *  * @return \doctrine\common\collections\collection   */ public function getcareoption() {     return $this->careoption; } } 

here formbuilder timesheettype.php

<?php //src/homecare/homecarebundle/form/timesheettype.php namespace homecare\homecarebundle\form;  use symfony\component\form\abstracttype; use symfony\component\form\formbuilderinterface; use symfony\component\optionsresolver\optionsresolverinterface;   class timesheettype extends abstracttype { /**  * @param formbuilderinterface $builder  * @param array $options  */ public function buildform(formbuilderinterface $builder, array $options) {     $builder         ->add('agency')         ->add('recipient')         ->add('pca')         ->add('service')                     ->add('continuetimesheetnumber')                     ->add('visitratio')                     ->add('timein')                     ->add('timeout')     ;              $builder->add('careoption', 'entity', array(                 'class' => 'homecarehomecarebundle:careoptions',                 'property' => 'careoption',                     'expanded' => true,                     'multiple' => true,                     'label' => false,                     'by_reference' => false,              ));  }  /**  * @param optionsresolverinterface $resolver  */ public function setdefaultoptions(optionsresolverinterface $resolver) {     $resolver->setdefaults(array(         'data_class' => 'homecare\homecarebundle\entity\timesheet',                     'csrf_protection'   => false,     )); }  /**  * @return string  */ public function getname() {     return 'homecare_homecarebundle_timesheet'; } } 

here apicontrollers:

<?php //src/homecare/homecareapibundle/controller/timesheetapicontroller.php namespace homecare\homecareapibundle\controller;  use symfony\bundle\frameworkbundle\controller\controller;  use fos\restbundle\controller\annotations\view; use symfony\component\httpfoundation\response; use symfony\component\httpfoundation\request; use homecare\homecarebundle\entity\timesheet; use homecare\homecarebundle\entity\services; use homecare\homecarebundle\entity\careoptions; use homecare\homecarebundle\form\timesheettype; use sensio\bundle\frameworkextrabundle\configuration\paramconverter; use jms\serializer\serializerbuilder; use fos\restbundle\view\view v; use doctrine\common\collections\arraycollection;   class timesheetapicontroller extends controller {     /** * @view() */ public function patchcareoptionaction( timesheet $timesheet ) {      return $this->updatetimesheetform( $timesheet );      }          /** * @view() */ public function postcreatetimesheetaction( request $request ) {          return $this->createtimesheetform( $request, new timesheet() );      }              /**     * @view()     */     public function patchtimesheetaction( timesheet $timesheet )     {          return $this->updatetimesheetform( $timesheet );          }                private function setmethod( $statuscode, $timesheet = null ){             switch( $statuscode ) {                  case 201:                 return array( 'method' => 'post' );                 break;                  case 204:                 return array(                      'action' => $this->generateurl('patch_timesheet', array('timesheet' => $timesheet->getid())),                     'method' => 'patch'                  );                 break;              }          }                    /**             * @view()             */             private function updatetimesheetform( timesheet $timesheet)                 {                        $statuscode = 204;                      $form = $this->createform(new timesheettype(), $timesheet, $this->setmethod( $statuscode, $timesheet ) );                             $form->handlerequest( $this->getrequest() );                       if ($form->isvalid()) {                                   $em = $this->getdoctrine()->getmanager();                                   //$careoption = $em->getrepository( "homecarehomecarebundle:careoptions" )->findonebyid(456);                                   //$timesheet->addcareoption( $careoption );                                  $em->persist( $timesheet );                     $em->flush();                          $response = new response();                         $response->setstatuscode( $statuscode );                           return $response;                     }                      return v::create($form, 400);                 }                              /**                     * @view()                     */                   private function createtimesheetform( $request, timesheet $timesheet )                      {                                 $statuscode = 201;                                  $form = $this->createform( new timesheettype(), $timesheet, $this->setmethod( $statuscode ) );                            $form->handlerequest( $this->getrequest() );                           if ( $form->isvalid() ) {                              //$user->save();                                           //$timesheet = $form->getdata();                               $response = new response();                              $response->setstatuscode(201);                                           $em = $this->getdoctrine()->getmanager();                                         $em->persist( $timesheet );                             $em->flush();                            //return v::create($form, 201);                              //return $response;                               //return array( $form, array("timesheet" => array("id" => $timesheet->getid() ) ) );         return v::create( array("createdtimesheet" => array("id" => $timesheet->getid() ) ), $statuscode );                                           // return array( $form, array("timesheet" => array("id" => $timesheet->getid() ) ) );                           }                           return v::create($form, 400);                      }        } 

you need either call second api delete 2 previous added options, or in updatetimesheet delete 2 previous added options, can select them query creating function in repository :

public function getlasttwocareoptions(){  $query = "select co.id careoptiontable co inner join timesheettable ts ts.id = timesheetid order co.id limit 2;";  $connection = $this->getentitymanager()->getconnection(); $prep_query = $connection->prepare($req); $result = $prep_query->fetchall(); } 

you can make limit parameter function if may need update 3 last care options someday or more.

then ids , remove them timesheet. solution use same query differently delete them sql query without having select , delete.


Comments

Popular posts from this blog

javascript - AngularJS custom datepicker directive -

javascript - jQuery date picker - Disable dates after the selection from the first date picker -