duanguai2781 2015-05-29 12:56
浏览 38

注册不冲洗的公式。 Symfony 2.5.6

I have e little problem. I followed this tutorial to create a register formular but it doesn't persist the entity. I don't understand why. It doesn't create any mistake, it just... doesn't flush.

Here is the tutorial: http://symfony.com/fr/doc/2.5/cookbook/doctrine/registration_form.html

Here is the entity:

namespace theia\mainBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;

/**
 * UserMain
 *
 * @ORM\Table()
 * @ORM\Entity(repositoryClass="theia\mainBundle\Entity\UserMainRepository")
 */
class UserMain
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var string
     *
     * @ORM\Column(name="email", type="string", length=255, unique=true)
     * @Assert\NotBlank()
     * @Assert\Email()
     */
    private $email;

    /**
     * @var string
     *
     * @ORM\Column(name="password", type="string", length=255)
     * @Assert\NotBlank()
     */
    private $password;

    /**
     * @var array
     *
     * @ORM\Column(name="roles", type="array")
     */
    private $roles;


    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set email
     *
     * @param string $email
     * @return UserMain
     */
    public function setEmail($email)
    {
        $this->email = $email;

        return $this;
    }

    /**
     * Get email
     *
     * @return string 
     */
    public function getEmail()
    {
        return $this->email;
    }

    /**
     * Set password
     *
     * @param string $password
     * @return UserMain
     */
    public function setPassword($password)
    {
        $this->password = $password;

        return $this;
    }

    /**
     * Get password
     *
     * @return string 
     */
    public function getPassword()
    {
        return $this->password;
    }

    /**
     * Set roles
     *
     * @param array $roles
     * @return UserMain
     */
    public function setRoles($roles)
    {
        $this->roles = $roles;

        return $this;
    }

    /**
     * Get roles
     *
     * @return array 
     */
    public function getRoles()
    {
        return $this->roles;
    }

    public function __construct()
    {
        $this->roles = [ 'ROLE_USER' ];

    }
}

here is my "Security Controller":

namespace theia\mainBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Component\Security\Core\SecurityContextInterface;
use Symfony\Component\HttpFoundation\Request;

use theia\mainBundle\Form\Type\RegistrationType;
use theia\mainBundle\Form\Model\Registration;

class SecurityController extends Controller
{

    public function loginAction(Request $request)
    {
        $session = $request->getSession();
        if ($request->attributes->has(SecurityContextInterface::AUTHENTICATION_ERROR)) {
            $error = $request->attributes->get(
                SecurityContextInterface::AUTHENTIFICATION_ERROR
            );
        } elseif (null !== $session && $session->has(SecurityContextInterface::AUTHENTICATION_ERROR)) {
             $error = $session->get(SecurityContextInterface::AUTHENTICATION_ERROR);
             $session->remove(SecurityContextInterface::AUTHENTICATION_ERROR);
        } else {
            $error = null;
        }

    // last username entered by the user
        $lastEmail = (null === $session) ? '' : $session->get(SecurityContextInterface::LAST_USERNAME);

        return $this->render(
            'theiamainBundle::security/login.html.twig',
            array(
                // last username entered by the user
                'last_email' => $lastEmail,
                'error'         => $error,
            )
        );
    }

    public function loginCheckAction()
    {
    }

    public function logoutAction()
    {
    }

    public function registerAction()
    {
        $form = $this->createForm(new RegistrationType(), new Registration());

        return $this->render('theiamainBundle:security:register.html.twig', array('form' => $form->createView()));
    }

    public function createAction()
    {
        $em = $this->getDoctrine()->getEntityManager();

        $form = $this->createForm(new RegistrationType(), new Registration());

        $form->handleRequest($this->getRequest());

        if ($form->isValid()) {
            $registration = $form->getData();

            $em->persist($registration->getUser());
            $em->flush();

            return $this->redirect('theiamainBundle::security/login.html.twig');
        }

        return $this->render('theiamainBundle:security:register.html.twig', array('form' => $form->createView()));
    }
}

Here is what is in the directory Form: My UserMainType:

namespace theia\mainBundle\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

class UsermainType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('email', 'email');
        $builder->add('plainPassword', 'repeated', array(
           'first_name' => 'password',
           'second_name' => 'confirm',
           'type' => 'password',
        ));
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'theia\mainBundle\Entity\UserMain'
        ));
    }

    public function getName()
    {
        return 'user';
    }
}

Here is the RegistrationType

namespace theia\mainBundle\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;

class RegistrationType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('user', new UsermainType());
        //$builder->add('terms', 'checkbox', array('property_path' => 'termsAccepted'));
    }

    public function getName()
    {
        return 'registration';
    }
}

Here is Registration

namespace theia\mainBundle\Form\Model;

use Symfony\Component\Validator\Constraints as Assert;

use theia\mainBundle\Entity\UserMain;

class Registration
{
    /**
     * @Assert\Type(type="theia\mainBundle\Entity\User")
     */
    protected $user;

    /*
     * @Assert\NotBlank()
     * @Assert\True()
     *
    protected $termsAccepted;
*/
    public function setUser(User $user)
    {
        $this->user = $user;
    }


    public function getUser()
    {
        return $this->user;
    }
/*
    public function getTermsAccepted()
    {
        return $this->termsAccepted;
    }

    public function setTermsAccepted($termsAccepted)
    {
        $this->termsAccepted = (Boolean) $termsAccepted;
    }
*/
}

Thanks you for your helps

  • 写回答

1条回答 默认 最新

  • duanqin2026 2015-05-29 16:53
    关注

    I am not quite sure but I think the problem is that you haven't set

    'data_class' => 'theia\mainBundle\Entity\Registration'
    

    In the setDefaultOptions of your RegistrationType, therefore the RegistrationType will not be mapped to an entity, therefore the UserMain cannot be persisted since it is used in the subform of RegistrationType.

    评论

报告相同问题?

悬赏问题

  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号