我是新来的蛋糕,并MVC的一般。 我想从一开始去建立良好的生活习惯。 在这些良好的生活习惯是保持控制器瘦,使得模型的脂肪。 但它是像我这样的小白有点运动目标。 如果我需要从一个模型传递信息到另一个,我只是倾倒所有到控制器? 尽量做到在一个模型?
这里的就是那种混乱我试图整理出一个最好的例子的动作。
一切似乎像它应该是在控制,但我可能是错误的。 这一行动得到成员列表,发送一个到一个视图。 在视图我可以勾选成员我想那些帐户“激活”。 没有ACL,只是简单的身份验证。 我要确保“子管理员”只能看到他们被允许使用一个数据库字段,CLIENT_ID来管理用户。 这两种型号我使用的是用户和客户端。
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看到)。
但我不禁觉得它不太100%呢。