lavavel 在 post 表单的时候,有没有与 model 字段与 数据库表字段的映射的配置,这样避免根据表单域,直接拼出数据库表

2014-04-08 22:28:33 +08:00
 yakczh
3891 次点击
所在节点    PHP
11 条回复
yakczh
2014-04-08 22:46:18 +08:00
$rules= array(
'username' => 'required',
'password' => 'required|min:8',
'email' => 'required|email|unique:users'
);

比如表单是email, 数据库青字段可能是 users_mail 这种的, 提交的时候提示
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'email' in 'where clause' (SQL: select count(*) as aggregate from `users` where `email` = tablecell@sohu.com)
ooh
2014-04-08 22:55:12 +08:00
不开debug会有这样的提示?
huafang
2014-04-08 23:11:34 +08:00
可以不一致,在控制器里
yakczh
2014-04-08 23:27:06 +08:00
@huafang 怎么写
比如 表单是 f1 => users_login
f2 =>users_pass
f3 =>users_mail

$rules= array(
'f1' => 'required',
'f2' => 'required|min:8',
'f3' => 'required|email|unique:users'
);
这里怎么配置?
huafang
2014-04-09 00:16:52 +08:00
是input获取上进行控制
yangqi
2014-04-09 00:37:36 +08:00
$user = array(
'user_email' => Input::get('email'),
'user_password' => Input::get('password'),
);

$rule = array(
'User_password' => 'required|min:8',
'user_email' => 'required|email|unique:users'
);
imzoke
2014-04-09 07:21:17 +08:00
仔细看了下,你这是在做唯一性验证时出错吧,unique第一个参数是表名,第二个参数就是字段名,用“,”号分隔。你只需要在验证规则里指定自定义字段名就行了。

至于字段映射,官方文档里是没有的,而且操作方式的不同,应该是没有这个功能。
yakczh
2014-04-09 09:23:47 +08:00
没有映射,耦合性就太强了,要么在表单域的名字全部与数据库表字段一致,要么在controll/route里面挨个赋值 $data = array(
'user_login' => Input::get('f1'),
'user_password' => Input::get('f3'),
'user_email' => Input::get('f2'),

);
然后按表字段来验证
$rules= array(
'user_login' => 'required',
'user_password' => 'required|min:8',
'user_email' => 'required|email|unique:users'
);
$validator = Validator::make($data, $rules);
但这样验证出错信息里是表字段为键
Array ( [*messages] => Array ( [user_login] => Array ( [0] => The user login field is required. ) [user_password] => Array ( [0] => The user password field is required. ) [user_email] => Array ( [0] => The user email field is required. ) ) [*format] => :message )
前台在显示错误提示的时候 只显示The following errors have occurred: 但不会显示具体那个字段出错了,因为找不到对应的键,除非用
{{ $errors->first('user_login', '<li>:message</li>') }}
{{ $errors->first('user_password', '<li>:message</li>') }}
{{ $errors->first('user_email', '<li>:message</li>') }} 再把数据库表名全部列上去,这样就跟第一种方式一样了
yakczh
2014-04-09 12:01:27 +08:00
这种技术应该叫数据库表页面透明化
icyflash
2014-04-09 12:13:15 +08:00
MVVM么
yakczh
2014-04-09 19:47:39 +08:00
数据库表字段是 user_email user_pass ... 这种 登录里

$user=array('user_email' => 'xx@oo.com', 'user_pass' => 'xxoo');
if (Auth::attempt($user)){
这里打印Sql是 select * from `users` where `user_email` = ? and `user_pass` = ? limit 1
页面提示

ErrorException

Undefined index: password


换成
$user=array('user_email' => 'xx@oo.com', 'password' => 'xxoo');
if (Auth::attempt($user)){

这时页面不报错了,但是打印的Sql里没有password条件了,只有 select * from `users` where `user_email` = ? limit 1
提示 {"login_status":"invalid"}

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/107739

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX