2010-07-25 4 views
0

어떻게 모델을 배열 형식으로 전달할 수 있습니까? 나는 컨트롤러에서보기에이 형식으로 모델을 전달하려는 : -배열 형식의 모델을 YIIY보기로 전달

사용자 [user_contact] = 연락 사용자 [user_contact] [contat_city] = 도시 사용자 [user_contact] [contact_state] = 상태

이를 나는 단지 $ 사용자가 액세스 할 수 있어요하지만 내가 $에 액세스 할 수 없습니다 해요

public function actionCreate() { 
    $user = new Users; 
    $presContact = new Contacts; 
    $presCity = new Cities; 
    $presState = new States; 
    $contactArr = array(); 
    // Uncomment the following line if AJAX validation is needed 
    // $this->performAjaxValidation($model); 
    if (isset($_POST['Users'])) { 
     $transaction = CActiveRecord::getDBConnection()->beginTransaction(); 
     $contactArr = CommonFunctions::saveContact($_POST['Users']['user_pres_contact'],'user_pres_contact',$errorArr); 
     $presContact = $contactArr['contact']; 
     $presCity = $contactArr['city']; 
     $presState = $contactArr['state']; 
     $user->attributes = $_POST['Users']; 
     $user->user_pres_contact_id = $presContact->contact_id; 
     if($user->save()){ 
      $transaction->commit(); 
      $this->redirect(array('view', 'id' => $user->user_id)); 
     } else { 
      $transaction->rollback(); 

     } 
    } 

    $this->render('createUser', array(
     'Users' => $user, 
     'Users[\'user_pres_contact\']'=>$presContact, 
     'Users[\'user_pres_contact\'][\'contact_city\']'=>$presCity, 
     'Users[\'user_pres_contact\'][\'contact_state\']'=>$presState, 
    )); 
} 

을 일을하고 있습니다 사용자 [ 'user_pres_contact'] 뷰

답변

2

에 그 그것들을 문자열로 지정하기 때문에 ... 올바른 방법은 (btw, 당신이 요구하는 것은 말 그대로 할 수 없다. 하나의 키에 2 개의 값을 할당하는 것은 불가능하다)

$user = array(
'user_press_contact' => array(
    'contact' => $presContact, 
    'city' => $presCity, 
    'state' => $presState, 
), 
); 


$this->render('createUser', array(
     'Users' => $user, 
    )); 

그것은 당신에게 줄 것이다는 [ 'user_press_contact'] [ '접촉'] 등 뷰의 이름에 대한

+0

사이드 노트로 : CActiveRecord 파생 클래스를 조사해야합니다. 사용자가 수행하는 작업은 심각한 우회로이며 Yii에서 훨씬 잘 처리 될 수 있습니다. – Blizz

0

당신이 사용할 수있는 사용자를 $ 유용한

0

$user->getAttributes() //it returns an array of data. 

희망 그것 모델 관계를 사용하여이를 해결할 수 있습니까? 당신은 (예를 들어 relation_to_city을 명명)시 모델 사용자 모델에서의 관계를 정의 할 수 있습니다

, 당신은 단지 컨트롤러

$this->render('view', 'user'=>$user); 

에서 사용자 모델을 할당 할 수 있으며, (보기에서) 도시에 액세스

$user->relation_to_city 
관련 문제