我没有在MVC中获得ORM

时间:2023-01-13 20:05:15

I'm creating authorization system on Kohana 3.1. It's for educational reasons.

我正在Kohana 3.1上创建授权系统。这是出于教育原因。

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!

现在我尝试学习ORM。我理解我可以用ORM做什么,但我不知道如何在MVC结构中实现它...现在一切正常,但它似乎非常错误!

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...

这是我的ORM模型......

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!

这太可怕了,对吧?我知道控制器必须很小,买模型可以像他们想要的那样胖......但是。帮助我,或者,更好的,给我一个例子!

1 个解决方案

#1


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:

所以你的行动应该是这样的:

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;
}

#1


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:

所以你的行动应该是这样的:

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;
}