0

Heres what im trying to do

Ive got table that holds many different types of data for items. the model name for this is 'Object'

for example :

row 1 : item_type = event
row 2 : item_type = news
row 3 : item_type = booking

i want to write a controller 'event_controller' and use the 'Object' model with it and only deal with item_types of event. and another controller for news (news_controller) and use the same model.

how do i do this on cakephp.

Im coming into cake from codeigniter and in CI we can load any model we want into any controller can i do something similar with cake?

4

3 に答える 3

2

var $usesこれは、CakePHP 1.3+ で使用するには不適切な形式であると考えられています。App::import に置き換えられました (以下を参照) 。

User モデルの Users コントローラーと、Comment モデルの Comments コントローラーがあるとします。受け入れられるパターンは次のとおりです。

関連付けの使用

モデルを論理的に関連付けることができる場合は、これが最善の策です。

// models/user.php
Class User extends AppModel {
    public $hasMany = array('Comment');
}

// controllers/users_controller.php
Class UsersController extends AppController {
    function allComments() {
        $this->User->Comment->find('all'); // You can use this across multiple models (e.g. $this->User->Comment->NestedComment->find('all');
    }
}

モデル オブジェクトのインスタンス化

これにより、モデル ファイルが読み込まれ、インスタンスが CakePHP のオブジェクト マップに追加され、インスタンスが返されます。

// models/user.php
Class User extends AppModel {}

// models/comment.php
Class Comment extends AppModel {}

// controllers/users_controller.php
Class UsersController extends AppController {
    function allComments() {
        $Comments =& ClassRegistry::init('Comment');

        $Comments->find('all');
    }
}

$this->loadModel の使用

内部的にこれは ClassRegistry::init を使用し、モデルをコントローラーのプロパティとして追加します。

// models/user.php
Class User extends AppModel {}

// models/comment.php
Class Comment extends AppModel {}

// controllers/users_controller.php
Class UsersController extends AppController {
    function allComments() {
        $this->loadModel('Comment');

        $this->Comment->find('all'); // using load model allows you to access it via $this->Model
    }
}

アプリ::インポート

これは、ファイルを要求する CakePHP の方法です。オブジェクトをインスタンス化する必要があります。

// models/user.php
Class User extends AppModel {}

// models/comment.php
Class Comment extends AppModel {}

// controllers/users_controller.php
App::import('Model', 'Comment');
Class UsersController extends AppController {
    function allComments() {
        $Comment = new Comment();

        $Comment->find('all');
    }
}

これが役立つことを願っています。

編集: コントローラー内でモデル オブジェクトをグローバルに使用する場合は、beforeFilter().

于 2011-09-02T04:49:38.107 に答える
1

$uses 声明を出さないことをお勧めします。その代わりに$this->Model->M0del1->....->someFunc();、関係が存在する場合のようにモデルの関係を使用できます。

モデル間の関係が存在しない場合は$this->loadModel('ModelName');、必要な場所で特定の関数を使用するだけです。

varを使用する$uses = array('Object');と、コントローラーに対してグローバルになり、必要かどうかに関係なく、コントローラーのすべてのアクションに対してそのモデルがロードされます。これはパフォーマンスに影響します。

特定の関数で使用する場合$this-LoadModel('ModelName');、すべてのアクションではなく、その関数でのみロードされます。

于 2011-09-02T02:01:04.943 に答える
0

コントローラを宣言し、次のように$uses変数を宣言します。

var $uses = array('Object');
于 2011-09-02T00:21:08.893 に答える