开发者

How can I add a related entity to a user object at the point of creation in FOSUserBundle?

In Symfony2 RC3, I am trying to create a related entity on a User object (FOSUserBundle) at the point of user creation so that I can display the appropriate fields on an edit profile form. I am doing the following in the RegistrationFormHandler.

class RegistrationFormHandler { protected $request; protected $userManager; protected $form;

public function __construct(Form $form, Request $request, UserManagerInterface $userManager)
{
    $this->form = $form;
    $this->request = $request;
    $this->userManager = $userManager;
}

public function process($confirmation = null)
{
    $user = $this->userManager->createUser();
    $this->form->setData($user);

    if ('POST' == $this->request->getMethod()) {
        $this->form->bindRequest($this->request);

        if ($this->form->isValid()) {
            if (true === $confirmation) {
                $user->setEnabled(false);
            } else if (false === $confirmation) {
                $user->setConfirm开发者_如何学运维ationToken(null);
                $user->setEnabled(true);
            }
            $prog = new \MyBundle\CoreBundle\Entity\Programme();

            $prog->setStartDate(date_create());
            $prog->setEndDate(date_create());
            $prog->setWeeklyTarget(4);
            $prog->setGoal('');

            $user->addProgrammes($prog);
            $this->userManager->updateUser($user);

            return true;
        }
    }

    return false;
}

}

The programme record does get created in the database but with a null user_id so it seems the association isn't working correctly. Anyone know what might be causing this?


The solution was to do $programmes->setUser($this); in the addProgrammes method of my User entity

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜