0

我有点卡住了,我有很多 if else 语句处理,我需要在更多的无页面中使用它,所以在视图中处理它不是一个好主意(并且阅读它不是一个好主意)

所以我被困住了,我有 2 张桌子。

user
users_details

User table存储基本登录信息,并且用户details具有以下字段

user_id
first_name  
last_name   
company 
location    
experience  
compensation    
about   
gender  
year    
day 
month   
profile_image

我的关系

用户模型

public function detail()
{
   return $this->has_one('Detail');
}

详细型号

public function user()
{
   return $this->belongs_to('User');
}

所以我坚持的是如何处理对象到 return experience, compensation因为那些我需要在不止一个页面上的东西,起初我在我的控制器中做了这个

public function get_index($username = null)
{

   $user = User::get_profile($username);

   if (is_null($user))  return Response::error('404');

   switch ($user->detail->experience) {
    case 1:
        $user->detail->experience = "No experience";
    break;

    case 2:
        $user->detail->experience = "Some experience";
    break;

    case 3:
        $user->detail->experience = "Experienced";
    break;

    case 4:
        $user->detail->experience = "Very experienced";
    break;

   }

    switch ($user->detail->compensation) {
    case 1:
        $user->detail->compensation = "Any";
    break;

    case 2:
        $user->detail->compensation = "Depends on Assignment ";
    break;

    case 3:
        $user->detail->compensation = "Paid Assignments Only ";
    break;

    case 4:
        $user->detail->compensation = "Time for Print ";
    break;

   }

   $this->layout->title = 'Profile' . ' ' . $username ;

   $this->layout->content = View::make( 'user.profile' )->with( 'user', $user );

}    

而且我知道这是我应该提出的麦芽汁想法,因为它不能解决我的问题,无效,我仍然需要复制。

那么有人可以向我展示处理这些问题的示例和有效方法吗?

将不胜感激,谢谢

4

1 回答 1

2

嗯,这确实很奇怪。

但是,您有两种方法:

1) 为答案定义表并与您的用户表连接,因此 SQL 结果将返回字符串,而不是 ID

2)如果出于某种原因您仍然希望在代码级别将 ID 与 Text 相关联,您可以执行以下操作:

在细节模型中定义方法,如compensation_str() 将进行此切换,对于经验也是如此。在每个需要显示此文本的地方调用此方法 ($user->details->compensation_str())

于 2013-03-31T13:10:11.697 回答