类Symfony \ Component \ Form \ Form不是有效实体或映射超类

时间:2012-08-23 15:39:54

标签: php symfony

我提交表单时出现此错误:

  

类Symfony \ Component \ Form \ Form不是有效实体或映射超类。

问题来自于我试图将数据持久保存在我的控制器中的数据库中。当我隐藏这部分代码时,我提交表单没有问题。我正在使用Symfony 2.0.16

这是实体

<?php

namespace Acme\FormsBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Acme\FormsBundle\Entity\UserAccount
 */
class UserAccount {
/**
 * @var integer $id
 */
private $id;

/**
 * @var string $user_id
 */
private $user_id;

/**
 * @var string $mail
 */
private $mail;

/**
 * @var string $password
 */
private $password;

/**
 * @var string $role
 */
private $role;

/**
 * @var string $pseudo
 */
private $pseudo;

/**
 * @var string $nom
 */
private $nom;

/**
 * @var string $prenom
 */
private $prenom;

/**
 * @var string $sexe
 */
private $sexe;

/**
 * @var text $freetext
 */
private $freetext;

/**
 * @var boolean $status
 */
private $status;

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

/**
 * Set user_id
 *
 * @param string $userId
 */
public function setUser_id($user_id) {
    $this -> user_id = $user_id;
}

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

/**
 * Set mail
 *
 * @param string $mail
 */
public function setMail($mail) {
    $this -> mail = $mail;
}

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

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

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

/**
 * Set role
 *
 * @param string $role
 */
public function setRole($role) {
    $this -> role = $role;
}

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

/**
 * Set pseudo
 *
 * @param string $pseudo
 */
public function setPseudo($pseudo) {
    $this -> pseudo = $pseudo;
}

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

/**
 * Set nom
 *
 * @param string $nom
 */
public function setNom($nom) {
    $this -> nom = $nom;
}

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

/**
 * Set prenom
 *
 * @param string $prenom
 */
public function setPrenom($prenom) {
    $this -> prenom = $prenom;
}

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

/**
 * Set sexe
 *
 * @param string $sexe
 */
public function setSexe($sexe) {
    $this -> sexe = $sexe;
}

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

/**
 * Set freetext
 *
 * @param text $freetext
 */
public function setFreetext($freetext) {
    $this -> freetext = $freetext;
}

/**
 * Get freetext
 *
 * @return text
 */
public function getFreetext() {
    return $this -> freetext;
}

/**
 * Set status
 *
 * @param boolean $status
 */
public function setStatus($status) {
    $this -> status = $status;
}

/**
 * Get status
 *
 * @return boolean
 */
public function getStatus() {
    return $this -> status;
}

}

这是控制器

namespace Acme\FormsBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Acme\FormsBundle\Entity\UserAccount;

class DefaultController extends Controller {

public function newUserAction(Request $request) {
    $newUser = new UserAccount();
    $newUser -> setUser_id('');
    $newUser -> setRole('');
    $newUser -> setNom('');
    $newUser -> setPrenom('');
    $newUser -> setSexe('');
    $newUser -> setFreetext('');
    $newUser -> setStatus('');

    $newUserForm = $this -> createFormBuilder($newUser)
                -> add('pseudo', 'text')
                -> add('mail', 'email')
                -> add('password', 'password')
                -> getForm();

    //if the form has been submitted
    if ($request->getMethod() == 'POST') {
        $newUserForm->bindRequest($request);

        if ($newUserForm->isValid()) {
            //Perform some actions
            $em = $this->getDoctrine()->getEntityManager();
            $em->persist($newUserForm);
            $em->flush();

            //Prepare confirmation message
            $this->get('session')->setFlash('notice','Account successfully created');

            //redirect user
            return $this->redirect($this->generateUrl('AcmeWelcomeBundle_homepage'));
        }
    }

    return $this -> render('AcmeFormsBundle:Default:newUser.html.twig', array('form' => $newUserForm -> createView(), ));
}
}

我无法弄清楚这一点。

1 个答案:

答案 0 :(得分:2)

您应该保留$newUser对象,而不是表单本身。