Symfony - Insertar datos a BD en foreign keys

   
Vista:

Insertar datos a BD en foreign keys

Publicado por estibaliz (1 intervención) el 18/11/2014 16:17:12
Hola,

Tengo un problema con mis entities en symfony, he creado un formulario de registro de usuario y las tablas (entities) User y Roles. El campo 'Privileges' es una clave foranea, foreign key, dentro de user que hace referencia al campo id de la entity Roles. Solo hay dos roles, 1 y 2 (admin y usuario). El problema es que cuando intento insertar los datos, me acepta todos (name, surname, phone, email, username, password) excepto el campo privileges que me lo toma null aunque le indique 1 o 2, ahi va el error:

An exception occurred while executing 'INSERT INTO user (name, surname, phone, email, username, password, privileges) VALUES (?, ?, ?, ?, ?, ?, ?)' with params ["maria", "fernandez", 654334455, "maria.lahga@gmail.com", "maria123", "maria", null]:

SQLSTATE[23000]: Integrity constraint violation: 1048 Le champ 'privileges' ne peut être vide (null)
500 Internal Server Error - DBALException
1 linked Exception: PDOException »

Aqui pongo las entities:
<?php

namespace PARIS\QuestBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;

/**
* User
*
* @ORM\Table(name="user")
* @ORM\Entity(repositoryClass="PARIS\QuestBundle\Entity\UserRepository")
*/
class User
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;

/**
* @ORM\OneToMany(targetEntity="Questusers", mappedBy="userqq")
*/

protected $userq;

/**
* @ORM\OneToMany(targetEntity="Answers", mappedBy="ansuser")
*/

protected $userans;

public function __construct() {

$this->userans = new ArrayCollection();
$this->userq = new ArrayCollection();
}
/**
* @return ArrayCollection
*/
public function getUserans()
{
return $this->Userans;
}

/**
* Get userq
*
* @return ArrayCollection
*/
public function getUserq()
{
return $this->userq;
}


/**
*
* @ORM\ManyToOne(targetEntity="User", inversedBy="eroles")
* @ORM\JoinColumn(name="privileges", referencedColumnName="id")
* @return integer
*/
protected $userid;

/**
* Set userid
* @param Roles $userid
* @return User
*/

public function setUserid(Roles $userid)
{
$this->userid = $userid;
return $this;
}

/**
*
* @return Roles
*/

public function getUserid()
{
return $this->userid;
}

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

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

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

/**
* @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 integer
*
* @ORM\Column(name="privileges", type="integer")
*/
private $privileges;


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

/**
* Set name
*
* @param string $name
* @return User
*/
public function setName($name)
{
$this->name = $name;

return $this;
}

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

/**
* Set surname
*
* @param string $surname
* @return User
*/
public function setSurname($surname)
{
$this->surname = $surname;

return $this;
}

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

/**
* Set phone
*
* @param integer $phone
* @return User
*/
public function setPhone($phone)
{
$this->phone = $phone;

return $this;
}

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

/**
* Set email
*
* @param string $email
* @return User
*/
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 User
*/
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 User
*/
public function setPassword($password)
{
$this->password = $password;

return $this;
}

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

/**
* Set privileges
*
* @param integer $privileges
* @return Roles
*/
public function setPrivileges($privileges)
{
$this->privileges = $privileges;

return $this;
}

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

Roles

<?php

namespace PARIS\QuestBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
//use Doctrine\Common\Collections\ArrayCollection;

/**
* Roles
*
* @ORM\Table()
* @ORM\Entity(repositoryClass="PARIS\QuestBundle\Entity\RolesRepository")
*/
class Roles
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
*
* @ORM\OneToMany(targetEntity="User", mappedBy="userid")
*/

protected $eroles;

public function __construct() {
$this->eroles = new ArrayCollection();
}

/**
* Get eroles
*
* @return ArrayCollection
*/
public function getEroles()
{
return $this->eroles;
}

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

/**
* Get id
*
* @return integer
*/

public function getId()
{
return $this->id;
}

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

return $this;
}

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


}
UserRegistration Form

<?php
namespace PARIS\QuestBundle\Form;

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

class UserRegistration extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('name', 'text')
->add('surname', 'text')
->add('phone')
->add('email', 'email')
->add('username', 'text')
->add('password', 'password')
->add('privileges', 'choice', array(
'choices' => array(
'Expert' => '1',
'Saltuser' => '2'
),
'required' => false,
'empty_value' => 'Choose type of user',
'empty_data' => '1'))
->getForm();
}
public function getName()
{
return 'user_form';
}
}
Funcion del controlador:

public function newuserAction()
{

$request = $this->getRequest();
$users = new User();
$form = $this->createForm(new UserRegistration(), $users);
if($request->getMethod() == 'POST')
{
$form->bind($request);
//if($form->isValid())
// {
$em = $this->getDoctrine()->getManager();
$em->persist($users);
$em->flush();
return $this->redirect($this->generateURL('_quest_login'));
// }
}
return $this->render('PARISQuestBundle:Default:register.html.twig', array('form' => $form->createView()));

}
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder