Symfony2:尝试插入数据库时​​未捕获的PHP Exception Doctrine

时间:2015-02-04 09:28:36

标签: symfony doctrine-orm

我的网站正在制作中,有时候某些人会出现以下错误:

[2015-02-04 09:05:24] request.CRITICAL:未捕获PHP异常Doctrine \ DBAL \ Exception \ NotNullConstraintViolationException:"执行' INSERT INTO查询时出现异常(查询,谷歌,scpo,jstor,cairn,worldcat,convertion,date)VALUES(?,?,?,?,?,?,?,?)' with params [null,0,0,0,0,0,0," 2015-02-04 09:05:24"]:SQLSTATE [23000]:完整性约束违规:1048列&#39 ;查询'不能为空" at /home/biblishazj/www/biblishare/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/AbstractMySQLDriver.php第112行{"例外":" [对象](Doctrine \ DBAL \ Exception \ NotNullConstraintViolationException(code:0):执行' INSERT INTO查询(查询,google,scpo,jstor,cairn,worldcat,convertion,date)VALUES(?,?,?,?,?)时发生异常,?,?,?)' with params [null,0,0,0,0,0,0,\" 2015-02-04 09:05:24 \"]: \ n \ n SQLSTATE [23000]:完整性约束违规:1048列'查询'不能为/ home / biblishazj / www / biblishare / vendor / doctrine / dbal / lib / Doctrine / DBAL / Driver / AbstractMySQLDriver .php:112,Doctrine \ DBAL \ Driver \ PDOException(代码:23000):SQLSTATE [23000]:完整性约束违规:1048列'查询'不能为/ home / biblishazj / www / biblishare / vendor / doctrine / dbal / lib / Doctrine / DBAL / Driver / PDOStatement.php:93,PDOException(代码:23000):SQLSTATE [23000]:完整性约束违规:1048列'查询&# 39;不能在/home/biblishazj/www/biblishare/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/PDOStatement.php:91)"} []

这是我的控制器代码:

$research = $_POST['query'];

    $session = $request->getSession();

    $session->set('query', $research);

    $query = new Query();
        $query->setQuery($research);
        if (isset($_POST['google'])){
            $query->setGoogle(1);
        } else {
            $query->setGoogle(0);
        }

        if (isset($_POST['scpo'])){
            $query->setScpo(1);
        } else {
            $query->setScpo(0);
        }

        if (isset($_POST['jstor'])){
            $query->setJstor(1);
        } else {
            $query->setJstor(0);
        }

        if (isset($_POST['cairn'])){
            $query->setCairn(1);
        } else {
            $query->setCairn(0);
        }

        if (isset($_POST['worldcat'])){
            $query->setWorldcat(1);
        } else {
            $query->setWorldcat(0);
        }

        $query->setConvertion(0);
        $query->setDate(new \DateTime());
        $em = $this->getDoctrine()->getManager();
        $em->persist($query);
        $em->flush();

我该如何解决? 谢谢

UPDATe:这是问题的3行:

[2015-02-04 16:09:27] security.INFO:使用匿名令牌填充SecurityContext [] [] [2015-02-04 16:09:27] request.CRITICAL:未捕获PHP异常Doctrine \ DBAL \ Exception \ NotNullConstraintViolationException:"执行' INSERT INTO查询时发生异常(查询,google,scpo, jstor,cairn,worldcat,convertion,date)VALUES(?,?,?,?,?,?,?,?)' with params [null,0,0,0,0,0,0," 2015-02-04 16:09:27"]:SQLSTATE [23000]:完整性约束违规:1048列&#39 ;查询'不能为空" at /home/biblishazj/www/biblishare/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/AbstractMySQLDriver.php第112行{"例外":" [对象](Doctrine \ DBAL \ Exception \ NotNullConstraintViolationException(code:0):执行' INSERT INTO查询(查询,google,scpo,jstor,cairn,worldcat,convertion,date)VALUES(?,?,?,?,?)时发生异常,?,?,?)' with params [null,0,0,0,0,0,0,\" 2015-02-04 16:09:27 \"]: \ n \ n SQLSTATE [23000]:完整性约束违规:1048列'查询'不能为/ home / biblishazj / www / biblishare / vendor / doctrine / dbal / lib / Doctrine / DBAL / Driver / AbstractMySQLDriver .php:112,Doctrine \ DBAL \ Driver \ PDOException(代码:23000):SQLSTATE [23000]:完整性约束违规:1048列'查询'不能为/ home / biblishazj / www / biblishare / vendor / doctrine / dbal / lib / Doctrine / DBAL / Driver / PDOStatement.php:93,PDOException(代码:23000):SQLSTATE [23000]:完整性约束违规:1048列'查询&# 39; /home/biblishazj/www/biblishare/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/PDOStatement.php:91)"} []不能为空 [2015-02-04 16:09:27] security.DEBUG:在会话中写出SecurityContext [] []

也许关于security.yml?

# you can read more about security in the related section of the documentation
# http://symfony.com/doc/current/book/security.html
security:
    encoders:
        FOS\UserBundle\Model\UserInterface: sha512

    role_hierarchy:
        ROLE_ADMIN:       ROLE_USER
        ROLE_SUPER_ADMIN: ROLE_ADMIN

    providers:
        fos_userbundle:
            id: fos_user.user_provider.username

    # the main part of the security, where you can set up firewalls
    # for specific sections of your app

    firewalls:
        main:
            pattern: ^/
            form_login:
                provider: fos_userbundle
                csrf_provider: form.csrf_provider
                success_handler: utilisateurs_utilisateurs.listener.authentication_success_handler
            logout:       true
            anonymous:    true

    access_control:
        - { path: ^/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/register, role: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/resetting, role: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/admin/, role: ROLE_ADMIN }

2 个答案:

答案 0 :(得分:1)

尝试var_dump($ research)只是为了测试它是否为null。否则,如果将查询属性设置为null并不会对您的逻辑产生任何问题,那么您必须在Query实体中添加nullable:true或执行测试:

if ($research != null){
    $query->setQuery($research);
}
else {
    //$default_value used when $research is null
    $query->setQuery($default_value);
}

如果为null,则将其设置为默认值。

答案 1 :(得分:-2)

您必须设置查询,因为数据库不允许空列值。在数据库表中允许null或在参数中未获得任何查询时将其设置为某个默认值。

  $query->setQuery("query is missing ");
相关问题