3

静かで単純なエラーがあると思いますが、解決できません... 2 つのテーブルを関連付けようとしています。

  • プロジェクトには多くのキーワードがあります
  • キーワード belogsプロジェクトへ

私のコード:

<?php
class KeywordsController extends AppController{
    public function add(){
        $projects = $this->Keyword->Project->find('list'); //***here is the error***
        //pr($projects);

        if ($this->request->is('post')) {
            $this->Keyword->save($this->request->data);
            $this->redirect('/keywords');   
        }
    }
}

<?php
class ProjectsController extends AppController{
    public function add(){
        if ($this->request->is('post')) {
            $this->Project->save($this->request->data);
            $this->redirect('/projects');   
        }
    }
}

<?php
class Project extends AppModel{
    public $hasMany = 'Keyword';
}

<?php
class Keyword extends AppModel{
    public $belongsTo = 'Project';

}

エラーメッセージ:

エラー: オブジェクト以外でメンバー関数 find() を呼び出します
ファイル: /Users/me/Localhost/cakephp/app/Controller/KeywordsController.php
行: 7

4

1 に答える 1

7

モデルのクラス宣言の上に以下の行を追加します。

App::uses('AppModel', 'Model');

nameクラス定義のプロパティも定義します。

public $name = 'Project';
public $name = 'Keyword';

プロジェクト モデルの場合

<?php
App::uses('AppModel', 'Model');
class Project extends AppModel{
    public $name = 'Project';
    public $hasMany = 'Keyword';
}

キーワード モデルの場合

<?php
App::uses('AppModel', 'Model');
class Keyword extends AppModel{
    public $name = 'Keyword';
    public $belongsTo = 'Project';

}

編集

$this->loadModel('Project');
$projects = $this->Project->find('list');
于 2013-09-10T09:10:14.613 に答える