当flush()

时间:2017-07-28 10:12:16

标签: php mysql symfony doctrine-orm

我已经与symfony api做出反应。我将反应应用程序的数据发送到symfony api。这是Symfony api中的代码

/**
     * Creates a 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);
    }

如果我评论这一行

$em->flush();

这是var_dump($user)当反应应用程序发送" John"作为用户名。

object(AppBundle\Entity\User)#264 (2) {
  ["id":"AppBundle\Entity\User":private]=>
  NULL
  ["username":"AppBundle\Entity\User":private]=>
  string(4) "John"
}

然而,$em->flush()存在内部服务器错误。这是错误

  

[2017-07-28 12:02:56] request.CRITICAL:未捕获PHP异常Doctrine \ DBAL \ Exception \ NotNullConstraintViolationException:"执行' INSERT INTO用户(用户名)VALUES时发生异常&#39(?); with params [null]:SQLSTATE [23000]:完整性约束违规:1048列'用户名'不能为空"在C:\ xampp \ htdocs \ simple-api \ vendor \ doctrine \ dbal \ lib \ Doctrine \ DBAL \ Driver \ AbstractMySQLDriver.php第112行{" exception":" [object](Doctrine \ DBAL \ Exception \ NotNullConstraintViolationException(code:0):执行' INSERT INTO用户(用户名)VALUES(?)'并使用params [null]时发生异常:\ n \ n SQLSTATE [23000]:完整性约束违规:1048列'用户名'不能为空:C:\ xampp \ htdocs \ simple-api \ vendor \ doctrine \ dbal \ lib \ Doctrine \ DBAL \ Driver \ AbstractMySQLDriver.php:112,Doctrine \ DBAL \ Driver \ PDOException(代码:23000):SQLSTATE [23000]:完整性约束违规:1048列'用户名'在C:\ xampp \ htdocs \ simple-api \ vendor \ doctrine \ dbal中不能为空\ lib \ Doctrine \ DBAL \ Driver \ PDOStatement.php:93,PDOException(代码:23000):SQLSTATE [23000]:完整性约束违规:1048列'用户名'不能为C:\ xampp \ htdocs \ simple-api \ vendor \ doctrine \ dbal \ lib \ Doctrine \ DBAL \ Driver \ PDOStatement.php:91)"} []

这意味着我的对象username设置为John,但是当我刷新它时,该属性会重置为null

这是我的用户实体

    <?php

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as 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;


    /**
     * Get 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;
    }

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

之前我已经看到过这样的其他问题,但它们包括外键和表之间的关系。我没有这样的事情。我有一个属性的实体。原因是什么?

0 个答案:

没有答案