2012-11-08 3 views
1

두 번째 테이블에서 데이터를 추가하는 함수를 호출 할 때 하나의 컨트롤러 파일에서 두 테이블의 데이터를 추가하고 싶습니다. 오류가 발생했습니다. 오류 : 멤버 함수 호출 개체가 아닌 여기에CakePhp에서 하나의 컨트롤러 파일을 사용하여 두 테이블에 데이터를 삽입하고 싶습니다.

()를 저장하는 VAR을 $ = 배열 ​​('메인 모델 이름', '2 차 모델 이름을'사용

<?php 
class CountryController extends AppController { 
    var $name = 'Country'; 
    var $helpers = array('Html', 'Form'); 


    // called index function 
    public function index() { 
     $this->render(); 
    } 

    // Function for add countries in database 
    public function addCountry() { 
     if (empty($this->data)) { 
      $this->render(); 
     } else { 
     // $this->cleanUpFields(); 
      if ($this->Country->save($this->data)) { 
       $this->Session->setFlash('The Counrty has been saved'); 
       $this->redirect('/country/index'); 
      } else { 
       $this->Session->setFlash('Please correct errors below.'); 
      } 
     } 
    } 

    public function addCity() { 
     $cities = $this->set('country', $this->Country->find('all')); 
     $this->set(compact('cities')); 
     if(empty($this->data)){ 
      $this->render(); 
     } else { 
      print_r($this->data);// die(); 
      if ($this->City->save($this->data)) { 
       $this->Session->setFlash('The City has been saved'); 
       $this->redirect('/country/index'); 
      } else { 
       $this->Session->setFlash('Please correct errors below.'); 
      } 
     } 
    } 

} 
?> 

답변

0

....... 그래서 내 컨트롤러 파일입니다);

예는 :

<?php 
class CountryController extends AppController { 
    var $name = 'Country'; 
    var $helpers = array('Html', 'Form'); 
    // $uses is where you specify which models this controller uses 
    var $uses = array('Country', 'City'); 

    // ... here go your controller actions (methods) 

} 
?> 
+0

답장을 보내 주셔서 감사합니다. .. 시도해보고 모델 파일 이름 city.php를 만들고 db라는 도시에 테이블을 만듭니다.하지만 이번에는 오류가 발생합니다. Notice (8) : 문자열을 문자열로 변환하는 배열 [CORE /Cake/Model/Datasource/DboSource.php, line 459] –

+0

입력 값을 확인하십시오. – Elby

+0

나중에 작동합니다 .. 이제 자신의 맞춤 함수로 시도했습니다 .. 이제 save() 작업을 시도합니다 ... 감사합니다 ..! cakePHp에서 db에 대한 사용자 정의 쿼리를 작성할 수있는 곳을 알려주십시오. –

1

당신은 모델 체인을 사용할 수 있습니다. 나는 도시와 국가가 관련되어 있다고 가정하고있다 (아마도 중간 모델 국가 일까?).

관계에 따라 $this->Country->City->save() 또는 $this->Country->IntermediaryModel->City->save()이됩니다.

관련 문제