我在 MVC 中没有得到 ORM
我正在 Kohana 3.1 上创建授权系统。这是出于教育原因。
现在我正在尝试学习ORM。我明白我可以用 ORM 做什么,但我不知道如何在 MVC 结构中实现它......现在一切正常,但它似乎非常错误!
这是我的控制器的操作:
public function action_signUp() {
if ( !$this->request->post() ) {
$view = new View_SignUp;
$view->title = 'Sign Up';
$this->response->body( $view->render() );
} else {
$validation =
Validation::factory( $this->request->post() )
->rule( 'token', 'not_empty' )
->rule( 'token', 'Security::check' )
->rule( 'username', 'not_empty' )
->rule( 'username', 'max_length', array( ':value', 32 ) )
->rule( 'username', 'alpha_dash', array( ':value', true ) ) // Alpha chars (from UTF-8), numbers, underscores and dashes...
->rule( 'password', 'not_empty' )
->rule( 'password', 'min_length', array( ':value', 6 ) )
->rule( 'password', 'max_length', array( ':value', 255 ) )
->rule( 'passwordRepeatedly', 'not_empty' )
->rule( 'passwordRepeatedly', 'matches', array( ':validation', 'passwordRepeatedly', 'password' ) )
->rule( 'email', 'not_empty' )
->rule( 'email', 'email' );
if ( $validation->check() ) {
$user = ORM::factory( 'User' );
$user->username = $this->request->post( 'username' );
$user->password = sha1( $this->request->post( 'password' ) ); // To add salt or something...
$user->email = $this->request->post( 'email' );
$user->save();
$this->request->redirect( 'sign-in' );
} else {
$view = new View_SignUp;
$view->title = 'Sign Up';
$view->haveErrors = true;
forEach ( $validation->errors( 'errorMessages' ) as $error ) {
$view->errors[] = array( 'error' => $error );
}
$this->response->body( $view->render() );
}
}
}
这是我的 ORM 模型...
class Model_User extends ORM {
//
}
这真的很糟糕,对吧?我知道控制器一定很小,购买模型可以想多胖就多胖……但是。帮助我,或者更好的是,给我举个例子!
I'm creating authorization system on Kohana 3.1. It's for educational reasons.
Right now I try to learn ORM. I understand what I can do with ORM, but I don't get how can I implement it in MVC structure... Right now all works, but it seems terribly wrong!
Here is my controller's action:
public function action_signUp() {
if ( !$this->request->post() ) {
$view = new View_SignUp;
$view->title = 'Sign Up';
$this->response->body( $view->render() );
} else {
$validation =
Validation::factory( $this->request->post() )
->rule( 'token', 'not_empty' )
->rule( 'token', 'Security::check' )
->rule( 'username', 'not_empty' )
->rule( 'username', 'max_length', array( ':value', 32 ) )
->rule( 'username', 'alpha_dash', array( ':value', true ) ) // Alpha chars (from UTF-8), numbers, underscores and dashes...
->rule( 'password', 'not_empty' )
->rule( 'password', 'min_length', array( ':value', 6 ) )
->rule( 'password', 'max_length', array( ':value', 255 ) )
->rule( 'passwordRepeatedly', 'not_empty' )
->rule( 'passwordRepeatedly', 'matches', array( ':validation', 'passwordRepeatedly', 'password' ) )
->rule( 'email', 'not_empty' )
->rule( 'email', 'email' );
if ( $validation->check() ) {
$user = ORM::factory( 'User' );
$user->username = $this->request->post( 'username' );
$user->password = sha1( $this->request->post( 'password' ) ); // To add salt or something...
$user->email = $this->request->post( 'email' );
$user->save();
$this->request->redirect( 'sign-in' );
} else {
$view = new View_SignUp;
$view->title = 'Sign Up';
$view->haveErrors = true;
forEach ( $validation->errors( 'errorMessages' ) as $error ) {
$view->errors[] = array( 'error' => $error );
}
$this->response->body( $view->render() );
}
}
}
And here is my ORM model...
class Model_User extends ORM {
//
}
It is really awful, huh? I know that controllers must be tiny, buy models can be as fat as they want... but. Help me, or, better, show me an example!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
模型级验证应该位于模型中,而不是控制器中。并且与视图相关的内容应该在视图模型中隔离,而不是控制器:)
所以您的操作应该类似于:
Model-level validation should be in your models, not controllers. And your view-related stuff should be isolated in your view model, still not controller :)
So your action should look something like: