Reputation: 63
I have a user object I am trying to insert via Doctrine.
The key fields are
/**
* @ORM\Column(type="integer", name="id")
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $userId;
/**
* @ORM\Id
* @ORM\Column(type="string", name="login_name")
*/
private $loginName;
/**
* @ORM\Column(type="string", name="display_name")
*/
private $name;
In my controller code I can echo out the value of the $loginName field via the getLoginName() method on the object.
/**
* @param mixed $loginName
*/
public function setLoginName($loginName)
{
$this->loginName = $loginName;
}
/**
* @return mixed
*/
public function getLoginName()
{
return $this->loginName;
}
You can see the Controller code to do the insert here.
if ($request->getMethod() == 'POST') {
$form->bind($request);
$login = $form->getData();
$factory = $this->get('security.encoder_factory');
echo($login->getLoginName());
$encoder = $factory->getEncoder($login);
$login->setPassword($encoder->encodePassword($login->getPassword(), $login->getSalt()));
$em = $this->getDoctrine()->getManager();
$em->persist($login);
$em->flush();
$this->get('session')->setFlash(
'success',
'Account Successfully Created'
);
However, when I call persist and flush on my entity, the login_name field is getting '' put into it (empty space). I can't understand why the value is getting nulled out (I changed a DB setting to error when it comes in as non-unique).
There are associations against this class, but this is the primary so I am creating it first.
Any thoughtS?
Upvotes: 1
Views: 2417
Reputation: 105888
I don't get what you're doing here. You want table.id
to be an auto-generated ID but you want table.login_name
to be the primary key? Because that's how you have it setup
What I personally think you should want is for table.id
to be the primary key, and for table.login_name
to be unique
/**
* @ORM\Column(type="integer", name="id")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $userId;
/**
* @ORM\Column(type="string", name="login_name", length=255, unique=true)
*/
private $loginName;
Upvotes: 1
Reputation: 63
So in testing I identified that you can't apply a generatedValue attribute to an Non-ID field. Once I removed that from the userID field (and allowed Mysql to handle that on it's own) things started to work again.
Upvotes: 0