我在 MVC 中没有得到 ORM

发布于 2024-11-14 17:38:33 字数 2107 浏览 2 评论 0原文

我正在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

橘和柠 2024-11-21 17:38:33

模型级验证应该位于模型中,而不是控制器中。并且与视图相关的内容应该在视图模型中隔离,而不是控制器:)

所以您的操作应该类似于:

public function action_signup()
{
    $errors = array();
    $user = new Model_User;

    if ($post = $this->request->post())
    {
        try
        {
            $user->values($post)
                ->create();

            $this->request->redirect('somewhere_over_the_rainbow');
        }
        catch (ORM_Validation_Exception $e)
        {
            $errors += $e->errors();
        }
    }

    $this->view->errors = $errors;
    $this->view->user = $user;
}

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:

public function action_signup()
{
    $errors = array();
    $user = new Model_User;

    if ($post = $this->request->post())
    {
        try
        {
            $user->values($post)
                ->create();

            $this->request->redirect('somewhere_over_the_rainbow');
        }
        catch (ORM_Validation_Exception $e)
        {
            $errors += $e->errors();
        }
    }

    $this->view->errors = $errors;
    $this->view->user = $user;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文