php - Symfony Not able to save manyToOne bidirectional Entity -


i have entity called survey , have entity called creditcards

creditcard linked survey using manytoone

survey linked creditcard using onetomany

the survey has bunch of financial related questions gets saved in survey table , asks user creditcard related details gets saved in creditcard table, since user can have more 1 card can add multiple card details , manytoone , onetomany comes play.

this creditcard.orm.yml

examplebundle\entity\creditcards:     type: entity     table: creditcards     id:         id:             type: integer             id: true             generator:                 strategy: auto     fields:         surveyid:             type: integer             column: survey_id         balance:             type: integer         amountdue:             type: integer             column: amount_due         interestrate:             type: integer             column: interest_rate     manytoone:         survey:             targetentity: examplebundle\entity\survey             inversedby: creditcards             joincolumn:                 name: survey_id                 referencedcolumnname: id                 ondelete: cascade     lifecyclecallbacks: {   } 

this creditcard entity

use doctrine\orm\mapping orm; use doctrine\common\collections\arraycollection; /**  * creditcards  */ class creditcards {     /**      * @var integer      */     private $id;      /**      * @var integer      */     private $surveyid;      /**      * @var integer      */     private $balance;      /**      * @var integer      */     private $amountdue;      /**      * @var integer      */     private $interestrate;      /**      * @var \examplebundle\entity\survey      */     private $survey;     /**      * id      *      * @return integer       */     public function getid()     {         return $this->id;     }      /**      * set surveyid      *      * @param integer $surveyid      * @return integer      */     public function setsurveyid($surveyid)     {         $this->surveyid = $surveyid;          return $this;     }      /**      * survey      *      * @return integer       */     public function getsurveyid()     {         return $this->surveyid;     }      /**      * set balance      *      * @param integer $balance      * @return creditcards      */     public function setbalance($balance)     {         $this->balance = $balance;          return $this;     }      /**      * balance      *      * @return integer       */     public function getbalance()     {         return $this->balance;     }      /**      * set amountdue      *      * @param integer $amountdue      * @return creditcards      */     public function setamountdue($amountdue)     {         $this->amountdue = $amountdue;          return $this;     }      /**      * amountdue      *      * @return integer       */     public function getamountdue()     {         return $this->amountdue;     }      /**      * set interestrate      *      * @param integer $interestrate      * @return creditcards      */     public function setinterestrate($interestrate)     {         $this->interestrate = $interestrate;          return $this;     }      /**      * interestrate      *      * @return integer       */     public function getinterestrate()     {         return $this->interestrate;     }        /**      * set survey      *      * @param \examplebundle\entity\survey $survey      * @return creditcards      */     public function setsurvey(\examplebundle\entity\survey $survey = null)     {         $this->survey = $survey;          return $this;     }      /**      * survey      *      * @return \examplebundle\entity\survey      */     public function getsurvey()     {         return $this->survey;     }     public function addsurvey(\examplebundle\entity\survey $survey)     {         $survey->addcreditcard($this);          $this->survey->add($survey);     } } 

this survey.orm.yml

its pretty long remove part of keep related question

examplebundle\entity\survey:     type: entity     table: survey     id:         id:             type: integer             id: true             generator:                 strategy: auto     fields:         homeloanmonthlyrepayments:             type: integer             column: home_loan_monthly_repayments             nullable: true         homeloantotaloutstanding:             type: integer             column: home_loan_total_outstanding             nullable: true         carloanmonthlyrepayments:             type: integer             column: car_loan_monthly_repayments             nullable: true         carloantotaloutstanding:             type: integer             column: car_loan_total_outstanding             nullable: true         personalloanmonthlyrepayments:             type: integer             column: personal_loan_monthly_repayments             nullable: true         personalloantotaloutstanding:             type: integer             column: personal_loan_total_outstanding             nullable: true         otherloanmonthlyrepayments:             type: integer             column: other_loan_monthly_repayments             nullable: true         otherloantotaloutstanding:             type: integer             column: other_loan_total_outstanding             nullable: true          userid:             type: integer             column: user_id             nullable: true      onetoone:         user:             targetentity: userbundle\entity\user             inversedby: survey             joincolumn:                 name: user_id                 referencedcolumnname: id      onetomany:         creditcards:             targetentity: examplebundle\entity\creditcards             mappedby: survey     lifecyclecallbacks: {  } 

this survey entity

use doctrine\orm\mapping orm; use userbundle\entity\user; use doctrine\common\collections\arraycollection;  /**  * survey  */ class survey {     /**      * @var integer      */     private $id;           /**      * id      *      * @return integer       */     public function getid()     {         return $this->id;     }       /**      * set homeloanmonthlyrepayments      *      * @param integer $homeloanmonthlyrepayments      * @return survey      */     public function sethomeloanmonthlyrepayments($homeloanmonthlyrepayments)     {         $this->homeloanmonthlyrepayments = $homeloanmonthlyrepayments;          return $this;     }      /**      * homeloanmonthlyrepayments      *      * @return integer       */     public function gethomeloanmonthlyrepayments()     {         return $this->homeloanmonthlyrepayments;     }      /**      * set homeloantotaloutstanding      *      * @param integer $homeloantotaloutstanding      * @return survey      */     public function sethomeloantotaloutstanding($homeloantotaloutstanding)     {         $this->homeloantotaloutstanding = $homeloantotaloutstanding;          return $this;     }      /**      * homeloantotaloutstanding      *      * @return integer       */     public function gethomeloantotaloutstanding()     {         return $this->homeloantotaloutstanding;     }      /**      * set carloanmonthlyrepayments      *      * @param integer $carloanmonthlyrepayments      * @return survey      */     public function setcarloanmonthlyrepayments($carloanmonthlyrepayments)     {         $this->carloanmonthlyrepayments = $carloanmonthlyrepayments;          return $this;     }      /**      * carloanmonthlyrepayments      *      * @return integer       */     public function getcarloanmonthlyrepayments()     {         return $this->carloanmonthlyrepayments;     }      /**      * set carloantotaloutstanding      *      * @param integer $carloantotaloutstanding      * @return survey      */     public function setcarloantotaloutstanding($carloantotaloutstanding)     {         $this->carloantotaloutstanding = $carloantotaloutstanding;          return $this;     }      /**      * carloantotaloutstanding      *      * @return integer       */     public function getcarloantotaloutstanding()     {         return $this->carloantotaloutstanding;     }      /**      * set personalloanmonthlyrepayments      *      * @param integer $personalloanmonthlyrepayments      * @return survey      */     public function setpersonalloanmonthlyrepayments($personalloanmonthlyrepayments)     {         $this->personalloanmonthlyrepayments = $personalloanmonthlyrepayments;          return $this;     }      /**      * personalloanmonthlyrepayments      *      * @return integer       */     public function getpersonalloanmonthlyrepayments()     {         return $this->personalloanmonthlyrepayments;     }      /**      * set personalloantotaloutstanding      *      * @param integer $personalloantotaloutstanding      * @return survey      */     public function setpersonalloantotaloutstanding($personalloantotaloutstanding)     {         $this->personalloantotaloutstanding = $personalloantotaloutstanding;          return $this;     }      /**      * personalloantotaloutstanding      *      * @return integer       */     public function getpersonalloantotaloutstanding()     {         return $this->personalloantotaloutstanding;     }      /**      * set otherloanmonthlyrepayments      *      * @param integer $otherloanmonthlyrepayments      * @return survey      */     public function setotherloanmonthlyrepayments($otherloanmonthlyrepayments)     {         $this->otherloanmonthlyrepayments = $otherloanmonthlyrepayments;          return $this;     }      /**      * otherloanmonthlyrepayments      *      * @return integer       */     public function getotherloanmonthlyrepayments()     {         return $this->otherloanmonthlyrepayments;     }      /**      * set otherloantotaloutstanding      *      * @param integer $otherloantotaloutstanding      * @return survey      */     public function setotherloantotaloutstanding($otherloantotaloutstanding)     {         $this->otherloantotaloutstanding = $otherloantotaloutstanding;          return $this;     }      /**      * otherloantotaloutstanding      *      * @return integer       */     public function getotherloantotaloutstanding()     {         return $this->otherloantotaloutstanding;     }      /**      * @var integer      */     private $userid;       /**      * set userid      *      * @param integer $userid      * @return survey      */     public function setuserid($userid)     {         $this->userid = $userid;          return $this;     }      /**      * userid      *      * @return integer      */     public function getuserid()     {         return $this->userid;     }     /**      * @var \userbundle\entity\user      */     private $user;      /**      * @var \exampletbundle\entity\creditcards      */     private $creditcards;       /**      * set user      *      * @param \userbundle\entity\user $user      * @return survey      */     public function setuser(user $user)     {         $this->user = $user;          return $this;     }      /**      * user      *      * @return \userbundle\entity\user       */     public function getuser()     {         return $this->user;     }      public function __tostring()     {         return (string) $this->getuser();     }      /**      * constructor      */     public function __construct()     {         $this->creditcards = new arraycollection();     }      /**      * add creditcards      *      * @param \exampletbundle\entity\creditcards $creditcards      * @return survey      */     public function addcreditcard(\examplebundle\entity\creditcards $creditcards)     {         if (!$this->creditcards->contains($creditcards)) {             $this->creditcards->add($creditcards);         }         return $this;     }      /**      * remove creditcards      *      * @param \exampletbundle\entity\creditcards $creditcards      */     public function removecreditcard(\examplebundle\entity\creditcards $creditcards)     {         $this->creditcards->removeelement($creditcards);     }      /**      * creditcards      *      * @return \doctrine\common\collections\collection       */     public function getcreditcards()     {         return $this->creditcards;     } } 

the form gets called in controller , passed twig

use symfony\component\form\abstracttype; use symfony\component\form\formbuilderinterface; use symfony\component\optionsresolver\optionsresolver;   class debttype extends abstracttype {     /**      * @param formbuilderinterface $builder      * @param array $options      */     public function buildform(formbuilderinterface $builder, array $options)     {         $builder             ->add('homeloanmonthlyrepayments')             ->add('homeloantotaloutstanding')             ->add('carloanmonthlyrepayments')             ->add('carloantotaloutstanding')             ->add('personalloanmonthlyrepayments')             ->add('personalloantotaloutstanding')             ->add('otherloanmonthlyrepayments')             ->add('otherloantotaloutstanding')                         ->add('creditcards', 'collection', array('type' => new creditcardstype(), 'allow_add' => true, 'by_reference' => false,             ));      }      /**      * @param optionsresolver $resolver      */     public function configureoptions(optionsresolver $resolver)    {         $resolver->setdefaults(array(             'data_class' => 'examplebundle\entity\survey'         ));     }      /**      * @return string      */     public function getname()     {         return 'examplebundle_debt';     } } 

finally debtaction processes form , tries save data

public function debtaction(request $request){     $em = $this->getdoctrine()->getmanager();     //get user id of logged in user     $userid = $this->getuser()->getid();      //get survey object of logged in user     $userexpensesinfo = $em->getrepository('examplebundle:survey')->findonebyuserid($userid);    $form = $this->createform(new debttype(), $userexpensesinfo);     $form->handlerequest($request);      if($request->ismethod('post')){         if($form->isvalid()){             $userexpensesinfo->setexpensefood($form->get('homeloanmonthlyrepayments')->getdata());             $userexpensesinfo->setexpensefood($form->get('homeloantotaloutstanding')->getdata());             $userexpensesinfo->setexpensehousing($form->get('carloanmonthlyrepayments')->getdata());             $userexpensesinfo->setexpensetransport($form->get('carloantotaloutstanding')->getdata());             $userexpensesinfo->setexpensefun($form->get('personalloanmonthlyrepayments')->getdata());             $userexpensesinfo->setexpenseclothing($form->get('personalloantotaloutstanding')->getdata());             $userexpensesinfo->setexpenseutil($form->get('otherloanmonthlyrepayments')->getdata());             $userexpensesinfo->setexpensepersonal($form->get('otherloantotaloutstanding')->getdata());             $userexpensesinfo->getcreditcards()->add($form->get('creditcards')->getdata());                             $em->flush();             $this->get('session')->getflashbag()->add(                 'notice',                 'your loan information has been saved'             );             return $this->render('examplebundle:default/dashboard:debt.html.twig', array(                 'form'=>$form->createview(),             ));         }     }     return $this->render('examplebundle:default/dashboard:debt.html.twig', array(         'form'=>$form->createview(),     )); } 

i have gone through step step through this article cant save work, when try save error get

a new entity found through relationship 'examplebundle\entity\survey#creditcards' not configured cascade persist operations entity: examplebundle\entity\creditcards@000000001bbd76da000000008bbf1e28. solve issue: either explicitly call entitymanager#persist() on unknown entity or configure cascade persist association in mapping example @manytoone(..,cascade={"persist"}). if cannot find out entity causes problem implement 'examplebundle\entity\creditcards#__tostring()' clue. 500 internal server error - orminvalidargumentexception 

the article refereed above talk @ this section, followed instruction , no matter cant passed error. on 6 hours trying solve , cant think straight more.

i appreciate if can here on this.

update:

imports:     - { resource: parameters.yml }     - { resource: security.yml }     - { resource: services.yml }  # put parameters here don't need change on each machine app deployed # http://symfony.com/doc/current/best_practices/configuration.html#application-related-configuration parameters:     locale: en  framework:     #esi:             ~     translator:      { fallbacks: ["%locale%"] }     secret:          "%secret%"     router:         resource: "%kernel.root_dir%/config/routing.yml"         strict_requirements: ~     form:            ~     csrf_protection: ~     validation:      { enable_annotations: true }     #serializer:      { enable_annotations: true }     templating:         engines: ['twig']         #assets_version: someversionscheme     default_locale:  "%locale%"     trusted_hosts:   ~     trusted_proxies: ~     session:         # handler_id set null use default session handler php.ini         handler_id:  ~     fragments:       ~     http_method_override: true  # twig configuration twig:     debug:            "%kernel.debug%"     strict_variables: "%kernel.debug%"  # assetic configuration assetic:     debug:          "%kernel.debug%"     use_controller: false     bundles:        [ examplebundle ]     #java: /usr/bin/java     filters:         cssrewrite: ~         #closure:         #    jar: "%kernel.root_dir%/resources/java/compiler.jar"         #yui_css:         #    jar: "%kernel.root_dir%/resources/java/yuicompressor-2.4.7.jar"  # doctrine configuration doctrine:     dbal:         driver:   pdo_mysql         host:     "%database_host%"         port:     "%database_port%"         dbname:   "%database_name%"         user:     "%database_user%"         password: "%database_password%"         charset:  utf8         # if using pdo_sqlite database driver:         #   1. add path in parameters.yml         #     e.g. database_path: "%kernel.root_dir%/data/data.db3"         #   2. uncomment database_path in parameters.yml.dist         #   3. uncomment next line:         #     path:     "%database_path%"      orm:         auto_generate_proxy_classes: "%kernel.debug%"         naming_strategy: doctrine.orm.naming_strategy.underscore         auto_mapping: true  # swiftmailer configuration swiftmailer:     transport: "%mailer_transport%"     host:      "%mailer_host%"     username:  "%mailer_user%"     password:  "%mailer_password%"     spool:     { type: memory } 

you'll need add cascade: ["persist"] onetomany mapping in survey yaml.

and way: class names should singular , capitalized.


Comments

Popular posts from this blog

java - Date formats difference between yyyy-MM-dd'T'HH:mm:ss and yyyy-MM-dd'T'HH:mm:ssXXX -

c# - Get rid of xmlns attribute when adding node to existing xml -