2015-01-29 2 views
-2

cakephp 컨트롤러에이 방법이 있습니다. 여기서 사용자가 사이트에 이미 등록되어 있는지 여부에 따라 구독 테이블에 사용자 구독 데이터를 저장하려고합니다. 두 양식 필드 NameEmail라는 있습니다. 따라서 사용자가 로그인하여 가입 버튼을 클릭하면 상자에 이름과 전자 메일이 이미 채워진 양식 팝업이 표시되고 제출하면 가입하게됩니다. 등록되지 않은 상태에서 로그인하지 않고 구독 만하려는 사용자는 빈 가입 양식을 작성해야합니다.cakephp에서 else if 조건을 최소화하기

나중에 등록 된 사용자 또는 등록되지 않은 사용자가 동일한 전자 메일을 구독하려는 경우 팝업에 "이미 등록되었습니다."라는 메시지가 표시됩니다. 지금까지 나는 이것을했다. 그것은 괜찮지 만 if와 else 조건이 많이 있습니다. 이 방법을 최소화하는 방법에 대한 아이디어는 도움이 될 것입니다. 나는 cakephp와이 모든 것들을 처음 접했습니다. 내 컨트롤러 메소드 코드는 다음과 같습니다.

function subscription_add() {  
     if(!empty($this->data)){    
      if($this->Session->check('User')){ 
       $is_subscribed = $this->Subscription->find('count', array('conditions'=>array('Subscription.email' => $this->data['Subscription']['email'])));   
        if($is_subscribed > 0){ 
         $this->Session->setFlash('You are already Subscribed !','default',array(),'E'); 
         $this->redirect(array('action' => 'index')); 
        } 
        else{ 
         $this->data['Subscription']['user_type'] = 1; 
         $this->data['Subscription']['user_id'] = $this->Session->read('User.id'); 
         $this->Subscription->create(); 
        if ($this->Subscription->save($this->data)) { 
        $this->Session->setFlash('Congrats ! You are Subscribed ', 'default', array(), 'S'); 
        $this->redirect(array('action' => 'index')); 
        } else { 
        $this->Session->setFlash('You are not subscribed. Please, try again.', 'default',array(),'E'); 
         } 
        } 
       } 
       else{ 
        $subscribed = $this->Subscription->find('count', array('conditions'=>array('Subscription.email' => $this->data['Subscription']['email'])));  
        if($subscribed > 0){ 
         $this->Session->setFlash('You are already Subscribed !','default',array(),'E'); 
         $this->redirect(array('action' => 'index')); 
        }else{ 
        $this->Subscription->create();      
        if ($this->Subscription->save($this->data)) { 
        $this->Session->setFlash('Congrats ! You are Subscribed ', 'default', array(), 'S'); 
        $this->redirect(array('action' => 'index')); 
        } else { 
        $this->Session->setFlash('You are not subscribed.. Please, try again.', 'default',array(),'E'); 
         } 
        } 
       } 


      } 
     } 

답변

0

개인적으로 전자 메일 필드의 모델 유효성 검사를 사용하여 다른 사람이 이미 등록되어 있는지 확인합니다. 이 코드의 절반은 프레임 워크에서 처리 할 필요가 없습니다. 당신이 나를 도와 주면 큰 도움이 될 것입니다 coding.It 통해 수행 할 수있는 경우

Documentation

+0

는 suggesting.But 고맙습니다 친구는 내가 원하는. @ mcgowan.b –

+0

find 메소드를 한 번 호출하고 find를 여러 번 호출하는 대신 사용할 수있는 변수에 결과를 저장할 수 있습니다 .......... –

관련 문제