1

2番目の1つのテーブルにデータを追加するために関数を呼び出すときに1つのコントローラーファイルから2つのテーブルにデータを追加したい..エラーが発生するエラー:非オブジェクトでメンバー関数save()を呼び出す

これが私のコントローラーファイルです

<?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.');
            }
        }
    }

}
?>
4

2 に答える 2

1

モデル チェーンを使用できます。私は市と国が関連していると仮定しています (おそらく中間モデルの州でしょうか?)。

だから、あなたの関係に応じて、$this->Country->City->save()またはそうなるでしょう。$this->Country->IntermediaryModel->City->save()

于 2012-11-08T06:14:13.867 に答える
0

var $ used = array('メインモデル名'、'2番目のモデル名'.......など);

例えば:

<?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)

}
?>  
于 2012-11-08T06:09:52.140 に答える