2013-06-03 25 views
0

我正在构建一个CakePHP应用程序。不同的用户具有不同的特性,所以我使用两个对象来存储例如用户动态填充PHP Cake中的下拉菜单。从另一个模型访问模型?

  • 用户hasOne学生/学生属于用户
  • 用户hasOne讲师/讲师属于用户

简档编辑页面将允许用户编辑这两个对象的所有详细信息。我已经设置了表单并使用saveAll保存了两个对象。我的问题是根据用户拥有的角色动态填充下拉菜单。

例如县的字段。管理员没有地址,而学生和讲师则有。我已经设置了我的国家模式,找到我所有的县,并将它们放入选择框中的选择组(按照此处显示的国家/地区Dropdown list with dyanmic optgroup

我可以在学生/ LecturersController内部做到这一点,因为他们允许我当我设置$ uses变量时访问Country模型。我不想在UsersController内部执行此操作,因为并非所有用户角色都有地址,并且地址永远不会存储在User对象内。我尝试将代码放入模型中,但后来我不知道如何访问模型中的其他模型。到目前为止,我在构建应用程序时没有任何问题,我觉得我可能在某个地方做出了糟糕的设计决定,或者有些事情我没有正确理解。

基本上我在问如何实现下面的setForForm()函数。

public function edit() { 
    //get user 
    $user = $this->Auth->user(); 
    //get role 
    $role = $user['User']['role']; 
    if ($this->request->is('post') || $this->request->is('put')) { 
     //get IDs 
     $userID = $user['User']['id']; 
     $roleID = $user[$role]['id']; 
     //set IDs 
     $this->request->data[$role]['user_id'] = $userID; 
     $this->request->data[$role]['id'] = $roleID; 
     $this->request->data['User']['id'] = $userID; 
     //delete data for role that is not theirs 
     foreach ($this->request->data as $key => $value) { 
      if($key !== 'User' && $key !== $role) { 
       unset($this->request->data[$key]); 
      } 
     } 
     if ($this->User->saveAll($this->request->data)) { 
      //update logged in user 
      $this->Auth->login($this->User->$role->findByUserId($userID)); 
      $this->Session->setFlash('Changes saved successfully.'); 
     } else { 
      $this->Session->setFlash(__('Please try again.')); 
     } 
    } 
    //set role for easy access 
    $this->set(compact('role')); 
    //sets required variables for role form 
    $this->User->$role->setForForm(); 
    //fills in form on first request 
    if (!$this->request->data) { 
     $this->request->data = $user; 
    } 
    //render form depending on role 
    $this->render(strtolower('edit_' . $role)); 
} 

//this is the method I would like to implement in the Student/Lecturer model somehow 
public function setForForm() { 
    $counties = $this->Country->getCountiesByCountry(); 
    $homeCounties = $counties; 
    $termCounties = $counties; 
    $this->set(compact('homeCounties', 'termCounties')); 
} 

回答

0

不知道是否我得到你的问题是正确的,但我想你想要的是以下几点:

User hasOne Student/Student belongs to User 

Student hasOne Country/Country belongsTo Student 

(与同为Lectureres)

然后从你的UsersController你可以做:

$this->User->Student->Country->findCountiesByCountry(); 

希望帮助

- 编辑:

如果要想要使用$角色,而不是学生/讲师,你会做这样的:

$this->User->{$role}->Country->findCountiesByCountry(); 
+0

问题在于管理员没有国家,所以这不起作用 – Connel

0

尝试类似这样:

public function setForForm() { 
    App::import('model', 'Country'); 
    $Country = New Country(); 
    $counties = $Country->getCountiesByCountry(); 
    $homeCounties = $counties; 
    $termCounties = $counties; 
    $this->set(compact('homeCounties', 'termCounties')); 
} 

我不知道W该是最好的解决办法或没有,但它至少工作:)

编辑

这里是另一个错误。它不建议从模型设置变量视图,您可以返回将设置的数据,然后在编辑功能通常将其设置为视图,但无论如何,如果您需要从模型设置视图,您可以加载控制器类到您的模型使用App::import();并使用set函数。

0

最后,我决定只是在用户控制器中加载县,不管表单是否需要它们,因为Admin是唯一不需要它们的用户。

相关问题