1

My website is in production and sometimes for some people there is the following error:

[2015-02-04 09:05:24] request.CRITICAL: Uncaught PHP Exception Doctrine\DBAL\Exception\NotNullConstraintViolationException: "An exception occurred while executing 'INSERT INTO query (query, google, scpo, jstor, cairn, worldcat, convertion, date) VALUES (?, ?, ?, ?, ?, ?, ?, ?)' with params [null, 0, 0, 0, 0, 0, 0, "2015-02-04 09:05:24"]: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'query' cannot be null" at /home/biblishazj/www/biblishare/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/AbstractMySQLDriver.php line 112 {"exception":"[object] (Doctrine\DBAL\Exception\NotNullConstraintViolationException(code: 0): An exception occurred while executing 'INSERT INTO query (query, google, scpo, jstor, cairn, worldcat, convertion, date) VALUES (?, ?, ?, ?, ?, ?, ?, ?)' with params [null, 0, 0, 0, 0, 0, 0, \"2015-02-04 09:05:24\"]:\n\nSQLSTATE[23000]: Integrity constraint violation: 1048 Column 'query' cannot be null at /home/biblishazj/www/biblishare/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/AbstractMySQLDriver.php:112, Doctrine\DBAL\Driver\PDOException(code: 23000): SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'query' cannot be null at /home/biblishazj/www/biblishare/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/PDOStatement.php:93, PDOException(code: 23000): SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'query' cannot be null at /home/biblishazj/www/biblishare/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/PDOStatement.php:91)"} []

Here is my controller code:

$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();

How can I fix that? Thank you

UPDATe : here are the 3 lines for the problem:

[2015-02-04 16:09:27] security.INFO: Populated SecurityContext with an anonymous Token [] [] [2015-02-04 16:09:27] request.CRITICAL: Uncaught PHP Exception Doctrine\DBAL\Exception\NotNullConstraintViolationException: "An exception occurred while executing 'INSERT INTO query (query, 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]: Integrity constraint violation: 1048 Column 'query' cannot be null" at /home/biblishazj/www/biblishare/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/AbstractMySQLDriver.php line 112 {"exception":"[object] (Doctrine\DBAL\Exception\NotNullConstraintViolationException(code: 0): An exception occurred while executing 'INSERT INTO query (query, google, scpo, jstor, cairn, worldcat, convertion, date) VALUES (?, ?, ?, ?, ?, ?, ?, ?)' with params [null, 0, 0, 0, 0, 0, 0, \"2015-02-04 16:09:27\"]:\n\nSQLSTATE[23000]: Integrity constraint violation: 1048 Column 'query' cannot be null at /home/biblishazj/www/biblishare/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/AbstractMySQLDriver.php:112, Doctrine\DBAL\Driver\PDOException(code: 23000): SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'query' cannot be null at /home/biblishazj/www/biblishare/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/PDOStatement.php:93, PDOException(code: 23000): SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'query' cannot be null at /home/biblishazj/www/biblishare/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/PDOStatement.php:91)"} [] [2015-02-04 16:09:27] security.DEBUG: Write SecurityContext in the session [] []

Maybe it's about the 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 }
5
  • 1
    I guess $research is empty also using symfony then use its request object to get desired values Commented Feb 4, 2015 at 9:32
  • I don't understand, the error was for a real request so $research should not be empty. It seems this occurs just sometimes and for few people Commented Feb 4, 2015 at 9:38
  • Should I use : $research = $request->get('query')? Commented Feb 4, 2015 at 9:45
  • It says 1048 Column 'query' cannot be null but I don't see you're handling it in your code. Something like ->setQuery('Whatever-it-is') is missing I guess. Commented Feb 4, 2015 at 9:51
  • I have $query->setQuery($research); after the New Query Commented Feb 4, 2015 at 10:03

2 Answers 2

1

Try to var_dump($research) just to test if it is null. Otherwise if setting query attribute as null doesn't make any probleme for you logic then you have to add nullable: true in the Query entity or do the test:

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

if it was null set it to a default value.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you but I need the query value and people are submiting a real query so It's a problem of Doctrine maybe?
-2

You have to set query because database doesn't allow empty column value. Either allow null in the database table or set it as some default value when you don't get any query in the parameter.

  $query->setQuery("query is missing ");

1 Comment

I did that but now there are some query with Null and all checkboxes set to 0... I appears that no data is persist to the database. I don't know why, the problem is just sometimes for some people

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.