php - Symfony3 Entity properties reset to null when flush() -
i have react app symfony api. send data react app symfony api. here code in symfony api
/** * creates new user entity. * * @route("/post", name="user_new") * @method({"get", "post", "options"}) */ function newaction(request $request ) { $body = $request->getcontent(); $body = str_replace('"', '"', $body); $body = json_decode($body,true); $username = $body['username']; $user = new user(); $user->setusername($username); $em = $this->getdoctrine()->getmanager(); $em->persist($user); $em->flush(); return new response(var_dump($user),200); }
if comment line
$em->flush();
this returned var_dump($user)
when react app sends "john" username.
object(appbundle\entity\user)#264 (2) { ["id":"appbundle\entity\user":private]=> null ["username":"appbundle\entity\user":private]=> string(4) "john" }
however $em->flush()
there internal server error. here error
[2017-07-28 12:02:56] request.critical: uncaught php exception doctrine\dbal\exception\notnullconstraintviolationexception: "an exception occurred while executing 'insert user (username) values (?)' params [null]: sqlstate[23000]: integrity constraint violation: 1048 column 'username' cannot null" @ c:\xampp\htdocs\simple-api\vendor\doctrine\dbal\lib\doctrine\dbal\driver\abstractmysqldriver.php line 112 {"exception":"[object] (doctrine\dbal\exception\notnullconstraintviolationexception(code: 0): exception occurred while executing 'insert user (username) values (?)' params [null]:\n\nsqlstate[23000]: integrity constraint violation: 1048 column 'username' cannot null @ c:\xampp\htdocs\simple-api\vendor\doctrine\dbal\lib\doctrine\dbal\driver\abstractmysqldriver.php:112, doctrine\dbal\driver\pdoexception(code: 23000): sqlstate[23000]: integrity constraint violation: 1048 column 'username' cannot null @ c:\xampp\htdocs\simple-api\vendor\doctrine\dbal\lib\doctrine\dbal\driver\pdostatement.php:93, pdoexception(code: 23000): sqlstate[23000]: integrity constraint violation: 1048 column 'username' cannot null @ c:\xampp\htdocs\simple-api\vendor\doctrine\dbal\lib\doctrine\dbal\driver\pdostatement.php:91)"} []
this means object has username
set john
, - when flush - property reset null
.
here user entity
<?php namespace appbundle\entity; use doctrine\orm\mapping orm; /** * user * * @orm\table(name="user") * @orm\entity(repositoryclass="appbundle\repository\userrepository") */ class user { /** * @var int * * @orm\column(name="id", type="integer") * @orm\id * @orm\generatedvalue(strategy="auto") */ private $id; /** * @var string * * @orm\column(name="username", type="string", length=255) */ private $username; /** * id * * @return int */ public function getid() { return $this->id; } /** * set username * * @param string $username * * @return user */ public function setusername($username) { $this->username = $username; return $this; } /** * username * * @return string */ public function getusername() { return $this->username; } }
i have seen other questions before include foreign keys , relationships between tables. don't have this. have entity 1 property. reason be?
Comments
Post a Comment