指导试图使瘦的控制器&脂肪模型在CakePHP [英] Guidance trying to make skinny controllers & fat models in CakePHP

查看:94
本文介绍了指导试图使瘦的控制器&脂肪模型在CakePHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Cake的新手,一般来说是MVC的。我想建立好的习惯从去。好的习惯是保持控制器精益,使模型脂肪。但是对于像我这样的noob来说,它是一个移动的目标。如果我需要从一个模型传递信息到另一个,我只是将所有这些转储到控制器?尝试让它在一个模型中工作?

I'm new to Cake, and to MVC's in general. I want to establish good habits from the get go. Among the good habits are keeping controllers lean, and making the models fat. But it's a bit of a moving target for a noob like me. If I need to pass info from one model to another, do I just dump all that into the controller? Try to make it work in a model?

这是一个动作,这是一种混乱的主要例子,我想解决。

Here's an action that is a prime example of the kind of confusion I'm trying to sort out.

似乎应该在控制器中,但我可能是错误的。此操作将获取成员列表,将其发送到视图。在视图中,我可以勾选我想要的帐户的成员激活。没有ACL,只是简单的认证。我确保子管理员只通过使用db字段client_id来查看他们允许管理的用户。我使用的两个模型是用户和客户端。

Everything seems like it should be in the controller, but I'm likely wrong. This action gets a list of members, sends that to a view. In the view I can tick members whose accounts I want "activate." No ACL, just simple auth. I make sure that "sub-admins" only see the users they're allowed to manage by use of a db field, client_id. The two models I'm using are User and Client.

public function activate() {
    if ($this->request->is('get')) {
        $id = $this->Auth->user('id');
        $this->User->id = $id; //make sure current User is the logged in user
        $currentClient = $this->User->field('client_id'); // get client_id based on logged in user
        $members = $this->User->Client->find('first', array( // find users that have the same client_id
            'conditions' => array('id' => $currentClient),
            'recursive' => 1
        ));
        $this->set('clients', $members); // send the users to the view                  
    } else if ($this->request->is('post') || $this->request->is('put')) {
        $members = $this->request->data['Members']; // grab players submitted from push form
        $memberIds = array(); // this will hold the selected users
        foreach($members as $a){
            $memberIds[$a['id']] = $a['id']; // loop over user's that were selected
        }
        $usersToActivate = $this->User->find('all', array( //find user records, based on the array of id's
            'conditions' => array(
                "User.id" => $memberIds
            )
        ));
        $this->Ticket->bulkActivate($usersToActivate); // send array of members into model for processing
        $this->Session->setFlash('Activations sent.', 'default', array('class' => 'success'));
        $this->redirect(array('action' => 'index'));
    }
}

对我来说, ...我已经在模型中进行了一些处理(如实际上接受用户记录的bulkActivate所示,并生成激活票)。

To my eye, it doesn't look drastically wrong... and I'm already doing some processing in the model (as seen with the bulkActivate that actually takes the user records, and generates activation tickets).

但是我

推荐答案

我不认为你想得到只有一个客户端?

I do not think you want to get only ONE client?

$members = $this->User->Client->find('first', array

我想这应该可以找到所有我已经改进了这个使用分页的情况下有很多用户。我可能是错了这一点,但是你的协会或真正的目标是清楚的我看看这个代码。我不知道如何例如Ticket模型与任何其他数据相关联,但我想你使用控制器::使用,你不应该这样做,但通过他们的关联访问相关的模型。

I guess this should find all. I've improved this to use pagination in the case there are lots of users. I might be wrong with this but neither your associations nor the real goal is clear to me by looking at this code. I don't know how for example the Ticket model is associated with any other data. But I guess you're using Controller::uses, you should not do that but access related models through their associations.

不要使用可怕的变量名称,如$ a,只是吸引,知道这意味着在一个更大的代码块或应用程序。你还命名了一个包含客户端数据$ member的数组,为什么不是$ clients?请阅读:清洁代码和CakePHP我建议您按照 CakePHP编码标准

Do not use horrible variable names like $a, that just sucks and nobody will ever know what that means in a larger code block or application. You also named an array containing client data $members, why not $clients? Read this: Clean Code and for CakePHP I suggest you to follow the CakePHP coding standards.

描述目标,这可以重构甚至更好我想。如果您想要激活客户端以获取票据(这是什么样子)为什么您没有在票证或客户端控制器/模型中进行?

Describe the goal and this could be refactored even better I think. If you want to activate clients to have access to a ticket (thats what it looks like) why have you not done it in the ticket or client controller/model?

这个大量的内联注释只是导致更多的混乱,它的帮助。编写干净,可读的代码,代码本身就是代码。你没有做任何超级复杂的代码或超级复杂的数学。再次,我可以推荐你阅读清洁代码,在我看来,它是每个开发人员必须阅读的。

Also this huge amount of inline comments is just causing more mess than it helps. Write clean and readable code and the code will speak for itself. You have not done any super complex code or super complex math there. Again, I can just recomment you to read "Clean Code", it is in my opinion a "must read" for every developer.

<?php
    // UsersController.php
    public function activate() {
        if ($this->request->is('post') || $this->request->is('put')) {
            $this->User->activate($this->request->data);
            $this->Session->setFlash('Activations sent.', 'default', array('class' => 'success'));
            $this->redirect(array('action' => 'index'));
        }

        this->Paginator->settings['Client'] = array(
            'conditions' => array('id' => $this->Auth->('current_id')),
            'contain' => array(
                'OnlyModelsYouNeedHere'));
        $this->set('clients', $this->Paginator->paginate($this->User->Client)); 
    }
?>

<?php
    // User.php - the model
    public function activate($data) {
        $memberIds = array();
        foreach($data['Members']members as $member) {
            $memberIds[$member['id']] = $member['id'];
        }
        $usersToActivate = $this->find('all', array(
            'conditions' => array(
                'User.id' => $memberIds)));
        return $this->Ticket->bulkActivate($usersToActivate);
    }
?>

传递id也可以减少更多,但嘿, :)把这作为你的代码的粗略的重构,想想我已经改变了,更重要的是为什么。

Passing the ids could be also trimmed down more, but hey, its late here now. :) Take this as a rough refactor of your code and think about what I have changed and more important why.

如果你想看到瘦的控制器和脂肪模型检查我们的插件,用户插件UsersController和模型可能会给您一个更大的图片。

If you want to see proper examples of skinny controllers and fat models check our plugins out, the users plugins UsersController and Model might give you a bigger picture.

这篇关于指导试图使瘦的控制器&amp;脂肪模型在CakePHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆