我正在使用CakePHP 2.3.6。在我的项目中,我必须使用一些值/ data /参数搜索用户,这些值位于几个表中,并且这些表是关联的。
协会是:
模型User.php
中的:
public $hasMany=array('Education'=>array(
'className'=>'Education
'foreignKey'=>'user_id'
),
'Experience'=>array(
'className'=>'Experience',
'foreignKey'=>'user_id'
),
'Employment'=>array(
'className'=>'Employment',
'foreignKey'=>'user_id'
),
'ProfessionalQualification'=>array(
'className'=>'ProfessionalQualification',
'foreignKey'=>'user_id'
)
)
模型Education.php
中的:
public $belongsTo=array('User'=>array(
'className'=>'User',
'foreignKey'=>'user_id'
)
)
模型Experience.php
中的:
public $belongsTo=array('User'=>array(
'className'=>'User',
'foreignKey'=>'user_id'
)
)
模型Employment.php
中的:
public $belongsTo=array('User'=>array(
'className'=>'User',
'foreignKey'=>'user_id'
)
)
在模型ProfessionalQualification.php中:
public $belongsTo=array('User'=>array(
'className'=>'User',
'foreignKey'=>'user_id'
)
)
现在,在搜索表单(View / Users / search.ctp)中:
echo $this->Form->create('User');
echo $this->Form->input('name',array('type'=>'text'));
echo $this->Form->input('username',array('type'=>'text'));
echo $this->Form->input('address',array('type'=>'textarea'));
echo $this->Form->input('phone',array('type'=>'tel'));
echo $this->Form->input('degree',array('type'=>'text'));
echo $this->Form->input('passing_year',array('type'=>'text'));
echo $this->Form->input('title',array('type'=>'text'));
echo $this->Form->input('title.description',array('type'=>'textarea'));
echo $this->Form->input('company',array('type'=>'text'));
echo $this->Form->input('company.description',array('type'=>'textarea'));
echo $this->Form->input('certificate',array('type'=>'text'));
echo $this->Form->input('certificate.description',array('type'=>'textarea'));
echo $this->Form->submit('Search');
echo $this->Form->end();
<{1>}控制器中的:
UsersController.php
我认为它会从数组中的所有关联表中检索我要查找的所有数据。但是,它给出了一个错误:
public function search(){
$this->set('title_for_layout','Search User');
if($this->request->is('post')){
$conditions=array();
$data=$this->request->data;
if(!empty($data['name']))
$conditions['name']="%".$data['name']."%";
if(!empty($data['username']))
$conditions['username']=$data['username'];
if(!empty($data['address']))
$conditions['address']='%'.$data['address'].'%';
if(!empty($data['phone']))
$conditions['phone']=$data['phone'];
if(!empty($data['degree']))
$conditions['degree']="%".$data['degree']."%";
.
.
.
if(!empty($data)){
$users=$this->User->find('all',array('conditions'=>$conditions));
if(!empty($users)){
$this->Session->setFlash("Searching Users Successful");
$this->set('users',$users);
}else
$this->Session->setFlash("No user found");
}else
$this->Session->setFlash("You didn't give any info to search for");
}
}
在这里,&#34; degree&#34;来自表格&#34;教育&#34;。
然后,我使用Error: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'degree' in 'where clause'
SQL Query: SELECT `User`.`id`, `User`.`name`, `User`.`username`, `User`.`phone`, `User`.`address`, `User`.`created`, `User`.`modified` FROM `db_name`.`users` AS `User` WHERE `degree` = 'M.Sc.'
。在ContainableBehavior
模型中:
User.php
在public $actsAs=array('Containable');
:
UsersController.php
当我使用它时,结果数组是:
$this->User->find('all',array('contain'=>array('Education'=>array(
'conditions'=>array('Education.institution ='=>"Institute 1")
),
'Employment'=>array(
'conditions'=>array('Employment.employer ='=>"Employer 1")
),
'ProfessionalQualification'=>array(
'conditions'=>array('ProfessionalQualification.certificate ='=>"Certificate 1")
),
'Experience'=>array(
'conditions'=>array('Experience.title ='=>"Title 1")
)
)
)
);
在这里,请注意,我只需要2号。数组,没有别的,但它给了我所有不相关的数据。
你能告诉我问题在哪里吗?我该怎么办 ?我想,我必须在特定字段中明确地给出表格名称,例如:ModelName.0.field。你觉得怎么样?答案 0 :(得分:0)
试试这个
public function global_search() {
$condition = array(
'User.role' => array('U', 'P'),
'User.user_status' => array('active', 'lead', 'inactive')
);
if ($this->request->is('post') || $this->request->is('put')) {
$or = array(
'PersonalInformation.first_name' => $this->request->data['User']['first_name'],
'PersonalInformation.last_name' => $this->request->data['User']['last_name'],
'PersonalInformation.primary_phone' => $this->request->data['User']['primary_phone'],
'PersonalInformation.dob' => $this->request->data['User']['dob'],
'User.email' => $this->request->data['User']['email'],
);
//==========This Function allow remove blank element from array=========//
$or = array_filter($or);
//=====End============//
$condition = array(
'User.role' => array('U', 'P'),
'User.user_status' => array('active', 'lead', 'inactive'),
'OR' => $or
);
$data = $this->User->find('first', array(
'conditions' => $condition)
);
// pr($data); exit;
$this->set('allusers', $data);
}
$data = $this->User->find('all', array(
'conditions' => $condition)
);
//pr($data); exit;
$this->set('allusers', $data);
$this->layout = 'admin';
}