異なるが関連するテーブルの検索基準に基づいてアイテムのリストを取得する際に、進行中の問題が発生しています。この種の状況に対処する方法について明確な考えを持っている必要があります。これが私の最も簡単な例です:
エージェンシー モデル (r_num の主キーを持つ referral と呼ばれるテーブルと、agents と呼ばれるテーブルを使用するエージェント モデルを使用する場合、エージェント テーブルにはエージェンシーにリンクする r_num の外部キーが含まれます。エージェンシーは多くのエージェントを持つことができます。
表:紹介
r_num int -- primary key auto incremented
r_company varchar(50) -- the name of the agency
... other fields don't matter
表: エージェント
a_num int -- primary key auto incremented
r_num int -- foreign key to the referral table
a_lname varchar(50) -- agent's last name
a_fname varchar(50) -- agent's first name
a_email varchar(50) -- agent's email
モデル:
class Agency extends AppModel {
var $name = 'Agency';
var $useTable = 'referral';
var $primaryKey = 'r_num';
var $hasMany = array(
'Agent' => array(
'className' => 'Agent',
'foreignKey' => 'r_num',
'order' => 'Agent.a_lname DESC',
'dependent'=> true
)
);
}
class Agent extends AppModel {
var $name = 'Agent';
var $primaryKey = 'a_num';
}
$this->Agency->find('all') を使用して、エージェントの名、エージェントの姓、エージェントの電子メールを含む検索フォームの条件を使用してリストを返すだけです。代理店の会社名と ID のリストが必要ですが、検索基準は代理店に基づいています。これを使用するいくつかの異なるモデルがあるため、これがどのように機能するかについてのドキュメントが必要です。オンラインで見つけたものに基づいて私が試したのは、これが機能する AgencyController ですが、私のより複雑なケースのいくつかでは機能しないハックです:複数の呼び出しを行わずにこれを行う必要があります。これらのクエリでこの手法を使用すると、数分かかります。
class AgenciesController extends AppController {
var $helpers = array ('Html','Javascript', 'Form', 'Paginator', 'Ajax', 'Phoneformat');
var $name = 'Agencies';
var $components = array('RequestHandler', 'RentalValidation');
var $uses = array('Agency', 'Agent');
function index() {
$criteria = $this->postConditions($this->data);
if (empty($criteria)){
$arrArgs = $this->passedArgs;
unset($arrArgs['page']);
$criteria = $arrArgs;
}
$searchcriteria = array();
foreach (array('Agency.r_state', 'Agency.r_company') as $item ) {
$itemvalue = '';
if (isset($criteria[$item])){
$itemvalue = $criteria[ $item];
if (!empty($itemvalue)) {
$searchcriteria[$item . " LIKE "] = '%' . $itemvalue .'%';
}
}
}
foreach (array('Agent.a_lname', 'Agent.a_email') as $item ) {
$itemvalue = '';
if (isset($criteria[$item])){
$itemvalue = $criteria[ $item];
if (!empty($itemvalue)) {
$agent_rnums = $this->Agent->find('list',
array('conditions' =>
array($item . " LIKE " => '%'. $itemvalue .'%'),
'fields' => 'Agent.r_num'));
$searchcriteria['r_num'] = $agent_rnums;
}
}
}
$this->set('passedCriteria', $criteria);
$data = $this->paginate('Agency', $searchcriteria);
if (count($data) == 1) {
$referralid = $data[0]['Agency']['id'];
$this->redirect(array('action' => 'edit', $referralid));
}
$this->set('agencies', $data);
}
}