swoft-cloud / swoft

🚀 PHP Microservice Full Coroutine Framework
https://swoft.org
Apache License 2.0
5.58k stars 788 forks source link

[DB] 实体生成工具生成属性的默认值为空字符串导致属性类型错误 #152

Closed CoderPoet closed 6 years ago

CoderPoet commented 6 years ago
Q A
Bug report? yes
Feature request? no
Swoft version 1.0.0
Swoole version 2.1.1
PHP version 7.2.2
Runtime environment Mac

Details

数据库字段为非字符串类的时候(如INT、TIMESTAMP之类的),DB插入的时候,如果这些非字符串的字段为空,插入会false。

// 表结构,test字段为varchar,test2字段为int,时间戳为timestamp
CREATE TABLE `test` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `test` varchar(32) DEFAULT NULL,
  `test2` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

Provide minimal script to reproduce the issue

// 插入一行记录,test2没有set,结果会报false。
  public function testSave()
    {
        $test = new Test();
        $test->setTest('test');
        $deferTest = $test->save();
        $result = $deferTest->getResult();
        return [$result];
    }
应该是默认缺省值为string,和数据库的INT字段不匹配导致的。
stelin commented 6 years ago

你实体代码贴出来 @CoderPoet

CoderPoet commented 6 years ago

@stelin


namespace App\Models\Entity;

use Swoft\Db\Model;
use Swoft\Db\Bean\Annotation\Column;
use Swoft\Db\Bean\Annotation\Entity;
use Swoft\Db\Bean\Annotation\Id;
use Swoft\Db\Bean\Annotation\Required;
use Swoft\Db\Bean\Annotation\Table;
use Swoft\Db\Types;

/**
 * 
 *
 * @Entity()
 * @Table(name="test")
 * @uses      Test
 * @version   2018年03月19日
 */
class Test extends Model
{
    /**
     * @var int $id 
     * @Id()
     * @Column(name="id", type=Types::INT)
     */
    private $id = '';

    /**
     * @var string $test1 
     * @Column(name="test1", type=Types::STRING, length=32)
     */
    private $test1 = '';

    /**
     * @var int $test2 
     * @Column(name="test2", type=Types::INT)
     */
    private $test2 = '';

    /**
     * setId
     *
     * @param int $value
     *
     * @return $this
     */
    public function setId(int $value)
    {
        $this->id = $value;

        return $this;
    }

    /**
     * setTest1
     *
     * @param string $value
     *
     * @return $this
     */
    public function setTest1(string $value): self
    {
        $this->test1 = $value;

        return $this;
    }

    /**
     * setTest2
     *
     * @param int $value
     *
     * @return $this
     */
    public function setTest2(int $value): self
    {
        $this->test2 = $value;

        return $this;
    }

    /**
     * getId
     *
     * @return int
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * getTest1
     *
     * @return string
     */
    public function getTest1()
    {
        return $this->test1;
    }

    /**
     * getTest2
     *
     * @return int
     */
    public function getTest2()
    {
        return $this->test2;
    }

}
CoderPoet commented 6 years ago

@stelin 表之前删掉了 ,重新建了一个, 脚手架生成了一下。

stelin commented 6 years ago

private $test2 = ''; 是int,但是你默认值空字符串 @CoderPoet

CoderPoet commented 6 years ago

@stelin 啊 没注意看脚手架生成的实体。感谢提醒!

CoderPoet commented 6 years ago

@stelin 建议脚手架生成的实体属性 不要设置默认值 直接 private $test2 就好啦!

stelin commented 6 years ago

已修复,db组件请更新至v1.1.4