2

I updated my entity file to include relationship mapping.

Persist worked before the update now it doesn't.

Maybe it's something I forgot to do.

namespace classes\classBundle\Entity; use Doctrine\ORM\Mapping as ORM; /** * advisersplans * * @ORM\Table() * @ORM\Entity */ class advisersPlans { /** * * @ORM\ManyToOne(targetEntity="plans", inversedBy="adviserPlans") * @ORM\JoinColumn(name="planid", referencedColumnName="id") */ public $plan; /** * @var integer * * @ORM\Column(name="id", type="integer") * @ORM\Id * @ORM\GeneratedValue(strategy="AUTO") */ public $id; /** * @var integer * * @ORM\Column(name="userid", type="integer") * * */ public $userid; /** * @var integer * * @ORM\Column(name="adviserid", type="integer") * * */ public $adviserid; /** * @var integer * * @ORM\Column(name="planid", type="integer") * * */ public $planid; /** * @var string * * @ORM\Column(name="participantLoginWebsiteAddress", type="string", length=255) */ public $participantLoginWebsiteAddress; public function __construct() { $class_vars = get_class_vars(get_class($this)); foreach ($class_vars as $key => $value) { if ($key != "plan") $this->$key = ""; } } }

Perist returns error saying planid is null. If I remove the following it works.

 /** * * @ORM\ManyToOne(targetEntity="plans", inversedBy="adviserPlans") * @ORM\JoinColumn(name="planid", referencedColumnName="id") */ 

Here is my code while persisting.

 $adviserPlan = new advisersPlans(); $adviserPlan->planid = $planid; $adviserPlan->userid = $this->userid(); $adviserPlan->adviserid = $session->get("editadviserid"); $em->persist($adviserPlan); 

Am I supposed to populate the plan field and not the planid field or is my entity file coded wrong.

1 Answer 1

2

You shouldn't set ids. You should set entities:

$adviserPlan = new advisersPlans(); // You should retrieve the plan before doing this, of course. $adviserPlan->setPlan($plan); $plans->addAdviserPlan(§adviserPlan); $em->persist($adviserPlan); 

The methods for adding an entity to a collection should be generated by doctrine when you run:

php app/console doctrine:generate:entities YourBundle 
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! It ends up doing setting the userid after the call like I wanted. Seems a little inefficient that you need the whole object to update the userid field (have to query for the object itself, and copy the object over). However, it is definitely a lot less error prone.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.