swoft-cloud / swoft

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

[db]Swoft\Db\Model的fill方法与数据库实体的set方法不兼容 #191

Closed heavenqq closed 6 years ago

heavenqq commented 6 years ago
Q A
Bug report? yes
Feature request? yes
Swoft version x.y.z
Swoole version x.y.z (by php --ri swoole)
PHP version x.y.z (by php -v)
Runtime environment Win10/Mac/CentOS 7/Ubuntu/Docker etc.

Details 当数据库存在下划线字段时,如:add_time,实体会生成 setAddTime方法,然后用Swoft\Db\Model的fill方法向数据库写入内容时,会与实体生成setAddTime方法不兼容,无法更新add_time字段

// paste output here
$data =[
     'add_time'=>time()
]
$member = new EwgMember();
$member->fill($data)->save()->getResult();

Provide minimal script to reproduce the issue

// paste code
实体的setAddTime方法
public function setAddTime(int $value): self
    {
        $this->add_time = $value;

        return $this;
    }
//Swoft\Db\Model的fill方法
public function fill(array $attributes): self
    {
        foreach ($attributes as $name => $value) {
            $methodName = sprintf('set%s', ucfirst($name));
            if (method_exists($this, $methodName)) {
                $this->$methodName($value);
            }
        }

        return $this;
    }
stelin commented 6 years ago

建议属性名称,统一定义成驼峰,再映射数据库字段,

inhere commented 6 years ago

他这是通过命令根据表生成的,应该在fill 时自动处理下