简体   繁体   English

Slim 3-getParsedBody()不正确的整数值:'NULL'

[英]Slim 3 - getParsedBody() Incorrect integer value: 'NULL'

This is my first attempt on Slim 3. I am trying to make a simple web application, but whenever I am trying to retrieve a NULL/Empty request parameter it's always a string, and trying to save the value into a numeric column will result in 这是我对Slim 3的首次尝试。我试图制作一个简单的Web应用程序,但是每当我尝试检索NULL / Empty请求参数时,它始终是字符串,并且尝试将值保存到数字列中会导致

General error: 1366 Incorrect integer value: 'NULL' for column 'last_login' at row 1 一般错误:1366错误的整数值:第1行的'last_login'列的'NULL'

Validating each parameter seems like an exhausting task. 验证每个参数似乎是一项艰巨的任务。 I would like to know if that's my only option or there's something I'm missing. 我想知道这是否是我唯一的选择,或者我缺少什么。

I also had some issues trying to save an object with private fields into the database, so I would really appreciate any comments on my approach. 在尝试将带有私有字段的对象保存到数据库中时,我也遇到了一些问题,因此,对于我对方法的任何评论,我将不胜感激。

Table Design 餐桌设计

CREATE TABLE `users` (
  `user_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(50) NOT NULL,
  `last_login` datetime DEFAULT NULL,
  PRIMARY KEY (`user_id`)
)

User Model 用户模型

class User implements JsonSerializable
{

    private $user_id;
    private $name;
    private $last_login;

    public function __construct($user_id = null, $name = null, $last_login = null)
    {
        if ($user_id !== null) $this->user_id = $user_id;
        if ($name !== null) $this->name = $name;
        if ($last_login !== null) $this->last_login = $last_login;
    }

    /**
     *  Getters and setters
     */

    function jsonSerialize()
    {

        return get_object_vars($this);
    }
}

UserDAO 的UserDAO

class UserDAO
{

    public function addUser(User $user)
    {

        $query = $this->connection->prepare('INSERT INTO ' . TABLE_USERS . ' VALUE (:user_id, :name, :last_login)');
        $result = $query->execute((array)$user->jsonSerialize());

        return $result;
    }
}

Not calling jsonSerialize() on execute will result in 不执行时调用jsonSerialize()将导致

'PDOException' with message 'SQLSTATE[HY093]: Invalid parameter number: parameter was not defined 消息“ SQLSTATE [HY093]”的“ PDOException”:无效的参数号:未定义参数

I assume PDO can't access model's private fields, I could be wrong tho. 我认为PDO无法访问模型的私有字段,我可能是错的。


Routes 路线

$app->post('/user/add', function (Request $request, Response $response) {

    $input = $request->getParsedBody();

    $userDAO = new UserDAO();

    $id = $input['user_id'];
    $name = $input['name'];
    $last = $input['last_login'];

    // Set ID as NULL to show the difference dump between ID and Last_login
    $user = new User(null, $name, $last);

    var_dump($input);
    var_dump($user);

    $userDAO->addUser($user);
});

Dump Result 转储结果

C:\wamp64\www\Rest\app\core\Routes.php:61:
array (size=3)
  'user_id' => string 'NULL' (length=4)
  'name' => string 'B' (length=1)
  'last_login' => string 'NULL' (length=4)

C:\wamp64\www\Rest\app\core\Routes.php:62:
object(User)[59]
  private 'user_id' => null
  private 'name' => string 'B' (length=1)
  private 'last_login' => string 'NULL' (length=4)

I guess that's the whole issue, how to turn this string 'NULL' into NULL in an efficient way. 我想这就是整个问题,如何有效地将字符串'NULL'转换为NULL


The insert method works just fine after adding jsonSerialize() on excute. 在执行时添加jsonSerialize()后,insert方法可以正常工作。 Tried calling addUser() from index.php and data get inserted into database. 尝试从index.php调用addUser(),并将数据插入数据库。

Sorry for the long post, but I've tried to remove a lot of fields for simplicity without leaving any relative code out. 抱歉,很长的帖子,但是为了简化起见,我尝试删除了很多字段,而没有留下任何相关代码。

All data in the Request object are strings. Request对象中的所有数据都是字符串。 Hence, if it's string "NULL" you need to manually convert it to null . 因此,如果它是字符串"NULL" ,则需要手动将其转换为null

The easiest way to solve this problem is to stop inserting user_id as it's an auto increment field. 解决此问题的最简单方法是停止插入user_id因为它是一个自动递增字段。 ie the correct SQL statement to prepare is: 即要准备的正确的SQL语句是:

$query = $this->connection->prepare('INSERT INTO ' . TABLE_USERS 
    . ' (name, last_login) VALUE (:name, :last_login)');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM