1

我在处理根据不同但相关表上的搜索条件获取项目列表时遇到了一个持续的问题。我需要清楚地知道如何处理这种情况。这是我最简单的例子:

如果我有一个代理模型(使用主键为 r_num 的称为引用的表和使用称为代理的表的代理模型,代理表包含链接到代理的外键 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 中的这个,它可以工作,但它是一个 hack,不适用于我的一些更复杂的案例:我需要在不进行多次调用的情况下执行此操作,因为有些我的表将返回大量行,并且在这些查询上使用这种技术需要几分钟。

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);
   }    
}
4

1 回答 1

0

你不能用别的方法吗?

  • 获取所有符合所需条件的代理
  • 做一个 Set::combine 来获取 agent_ids (并使其唯一化)
  • 使用 IN Agency_ids 进行查询

这样只会有 2 个 sql 调用

于 2011-02-17T22:50:48.243 回答