1

モデル クエリ関数をコントローラーに呼び出す方法を解決できません。私は数え切れないほど多くの文書を調べてきました。たぶん、私がやろうとしていることは間違っていますか?引き続き MySQL エラーが発生します (以下のエラー)。

プラン :: モデル:

function getActive() 
{
$findParameters = array(
    'limit' => 10,
    'order' => array('Plan.monthly_cost' => 'asc'),
    'conditions' => array('PlanDetail.active' => 1)
);
return $this->find('all', $findParameters);
}

プラン :: コントローラ:

function search() {
    $this->Plan->recursive = 2; //*** Modified by Jason: Recursion needs to be corrected with better method. ***//
    $active = $this->Plan->getActive();
    $this->set('plans', $this->paginate($active));
}

注意 (8): 配列から文字列への変換 [ROOT.... 警告 (512): SQL エラー: 1054: 不明な列 'Plan' in 'where clause'

4

1 に答える 1

1

基本的に $this->paginate は、クエリの結果を最初のパラメーターとして受け入れません。モデルに DB ロジックを組み込みたい場合は、次のようにします。

モデル

function getActiveConditions() {
    retrun array(
       'limit' => 10,
       'order' => array('Plan.monthly_cost' => 'asc'),
       'conditions' => array('PlanDetail.active' => 1)
    );
}

あなたのコントローラーで:

function search() {
    $this->Plan->recursive = 2;
    $activeConditions = $this->Plan->getActiveConditions();
    $this->set('plans', $this->paginate($this->Plan, $activeConditions));
}

説明: paginate メソッドは、最初の引数として渡されたモデルをページ分割し (または null の場合はコントローラーのデフォルト モデルを取得します)、2 番目のパラメーターを使用して結果にいくつかの制限を適用します。

この recursive=2 の Containable 動作を確認するか、少なくともこれらの不要な関係のバインドを解除してください。

于 2011-01-21T22:52:24.160 に答える