Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

model fill 优化 #297

Closed
hujinhuaP opened this issue Jun 29, 2018 · 5 comments
Closed

model fill 优化 #297

hujinhuaP opened this issue Jun 29, 2018 · 5 comments

Comments

@hujinhuaP
Copy link

/**
 * @param array $attributes
 *
 * $attributes = [
 *     'name' => $value
 * ]
 *
 * @return \Swoft\Db\Model
 */
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;
}

ucfirst建议优化和生成实体类规则一致。字段很多下划线的

@limingxinleo
Copy link
Contributor

这个其实没啥问题,Entity中的私有变量已经是驼峰命名了,所有只需要用ucfirst就可以了

@huangzhhui
Copy link
Contributor

这里涉及到一个问题是fill方法是提供以属性名传参还是字段名传参的问题,我觉得大家可以探讨一下

@hujinhuaP
Copy link
Author

如果全传驼峰的进去肯定没问题得,如果传递原字段就有问题了!
输出是驼峰,防君子不防小人吧

@limingxinleo
Copy link
Contributor

@hujinhuaP 这里我越来越觉得,fill应该也是小写比较好。。

驼峰命名只让他活在toArray()里吧。

@huangzhhui
Copy link
Contributor

@limingxinleo 如果驼峰只活在toArray()里面的话,那接收参数的时候也会有问题,前端获取的数据模型不一致,应当从参数接收,Entity设置,返回结果整个流程使用的都是一致的

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants