2015-11-15 100 views
-1

我不能插入數據的形式在Symfony2的數據庫,我的代碼:無法插入表單數據到數據庫symfony2?

控制器

public function signupAction(Request $request) { 
    $user = new users(); 
    $form = $this->createForm(new usersType(), $user); 

    $form->handleRequest($request); 

    if ($form->isValid() && $form->isSubmitted()) { 

     $em = $this->getDoctrine()->getManager(); 
     $em->persist($user); 
     $em->flush(); 

    } 

形式(userstype)

public function buildForm(FormBuilderInterface $builder, array $options) { 
    $builder 
      ->add('email','text',array(
       'label' => 'Due Date', 
       'label_attr' => array(
        'class' => 'CUSTOM_LABEL_CLASS' 
       ), 
      )) 
      ->add('username' ,'text',array(
       'label' => 'Due Date', 
      )) 
      ->add('password', 'password', array(
       'attr' => array(
        'required' => false, 
        'class' => 'control-label', 
        'id' => 'password' 
       ) 
      )) 
      ->add('firstName') 
      ->add('lastName') 
      ->add('location') 
      ->add('rememberToken') 
      ->add('createdAt') 
      ->add('updatedAt') 
    ; 
} 

實體(用戶):

private $id; 

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

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

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

/** 
* @var string 
* 
* @ORM\Column(name="first_name", type="string", length=255, nullable=true) 
*/ 
private $firstName; 

/** 
* @var string 
* 
* @ORM\Column(name="last_name", type="string", length=255, nullable=true) 
*/ 
private $lastName; 

/** 
* @var string 
* 
* @ORM\Column(name="location", type="string", length=255, nullable=true) 
*/ 
private $location; 

/** 
* @var string 
* 
* @ORM\Column(name="remember_token", type="string", length=255, nullable=true) 
*/ 
private $rememberToken; 

/** 
* @var \DateTime 
* 
* @ORM\Column(name="created_at", type="datetime", nullable=true) 
*/ 
private $createdAt; 

/** 
* @var \DateTime 
* 
* @ORM\Column(name="updated_at", type="datetime", nullable=true) 
*/ 
private $updatedAt; 


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

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

    return $this; 
} 

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

/** 
* Set username 
* 
* @param string $username 
* 
* @return users 
*/ 
public function setUsername($username) 
{ 
    $this->username = $username; 

    return $this; 
} 

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

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

    return $this; 
} 

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

/** 
* Set firstName 
* 
* @param string $firstName 
* 
* @return users 
*/ 
public function setFirstName($firstName) 
{ 
    $this->firstName = $firstName; 

    return $this; 
} 

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

/** 
* Set lastName 
* 
* @param string $lastName 
* 
* @return users 
*/ 
public function setLastName($lastName) 
{ 
    $this->lastName = $lastName; 

    return $this; 
} 

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

/** 
* Set location 
* 
* @param string $location 
* 
* @return users 
*/ 
public function setLocation($location) 
{ 
    $this->location = $location; 

    return $this; 
} 

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

/** 
* Set rememberToken 
* 
* @param string $rememberToken 
* 
* @return users 
*/ 
public function setRememberToken($rememberToken) 
{ 
    $this->rememberToken = $rememberToken; 

    return $this; 
} 

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

/** 
* Set createdAt 
* 
* @param \DateTime $createdAt 
* 
* @return users 
*/ 
public function setCreatedAt($createdAt) 
{ 
    $this->createdAt = $createdAt; 

    return $this; 
} 

/** 
* Get createdAt 
* 
* @return \DateTime 
*/ 
public function getCreatedAt() 
{ 
    return $this->createdAt; 
} 

/** 
* Set updatedAt 
* 
* @param \DateTime $updatedAt 
* 
* @return users 
*/ 
public function setUpdatedAt($updatedAt) 
{ 
    $this->updatedAt = $updatedAt; 

    return $this; 
} 

/** 
* Get updatedAt 
* 
* @return \DateTime 
*/ 
public function getUpdatedAt() 
{ 
    return $this->updatedAt; 
} 

什麼可以喲,感謝所有:)

(*不重要*:只是可以發佈此,因爲它顯示我:它看起來像你的文章主要是代碼;請添加更多的細節。 :)

+0

問題是什麼?你有什麼特別的錯誤嗎? –

回答

0

我假設您尚未在您的FormBuilder中定義任何data_class,因此該實體不會被保留。
您可以定義data_class的defaultOptions內:

use Symfony\Component\OptionsResolver\OptionsResolver; 

public function configureOptions(OptionsResolver $resolver) 
{ 
    $resolver->setDefaults(array(
     'data_class' => 'AppBundle\Entity\Task', 
    )); 
} 

欲瞭解更多信息,看看在symfony-cookbook