2
votes

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

1 Answers

2
votes

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