确定这里的ID是我的问题的冲突相关数据搜索yii2
我在一个GridView 都来自同一个外部表 显示2分相关的列我正在显示的名字和姓氏的全名 和电子邮件作为另一列 所有的3个数据是来自同一个表
'columns' => [
['class' => 'yii\grid\CheckboxColumn'],
[
'attribute' => 'event_id',
'label' => 'Event Title',
'value' => 'event.title'
],
[
'attribute' => 'user_id',
'label' => 'Name',
'value' => 'users.fullname',
],
[
'attribute' => 'user_id',
'label' => 'Email',
'value' => 'users.email',
],
正如你可以看到我有给属性作为user_id
如果我想让它搜索 这里是我的search
模型
$query->joinWith(['event', 'users']);
$query->andFilterWhere(['like', 'event.title', $this->event_id]);
$query->andFilterWhere(['like', 'user.firstname', $this->user_id]);
$query->andFilterWhere(['like', 'user.email', $this->user_id]);
现在的问题是对电子邮件搜索工作正常,我的意思是,当我在电子邮件栏搜索框输入任何数据就呈现user_id
不错,但它像where email=blah blah AND firstname=blah blah
实际上会自动创建查询我还没有在fullname
输入任何数据栏搜索框
,当我在fullname
搜索栏中输入任何数据它不EVET发现表
我应该怎么解决这个矛盾的user_id
??? 喔,我的数据库结构是这样的 这样的GridView是对此我显示所有谁签入到特定事件 的用户,以便有2个表event
和users
CREATE TABLE IF NOT EXISTS `event` (
`id` int(11) NOT NULL,
`organiser_id` int(11) NOT NULL,
`interest_id` int(11) NOT NULL,
`title` varchar(255) NOT NULL,
`description` varchar(500) NOT NULL,
`location` varchar(255) NOT NULL,
`is_active` bit(1) NOT NULL,
)
CREATE TABLE IF NOT EXISTS `user` (
`id` int(11) NOT NULL,
`firstname` varchar(255) DEFAULT NULL,
`lastname` varchar(255) DEFAULT NULL,
`dob` datetime DEFAULT NULL,
`email` varchar(100) DEFAULT NULL,
`username` varchar(100) DEFAULT NULL,
`password` varchar(100) DEFAULT NULL,
`area_intrest` varchar(250) NOT NULL,
)
CREATE TABLE IF NOT EXISTS `checkin` (
`id` int(11) NOT NULL,
`user_id` int(11) NOT NULL,
`event_id` int(11) NOT NULL,
`user_type` enum('competitor','fan') NOT NULL,
)
这里是checkin
表我的搜索功能代码
class CheckinSearch extends Checkin
{
public $fullName;
/**
* @inheritdoc
*/
public function rules()
{
return [
[['id', 'created_by', 'updated_by'], 'integer'],
[['user_type', 'user_id', 'event_id', 'created_date', 'updated_date','fullName'], 'safe'],
];
}
/**
* @inheritdoc
*/
public function scenarios()
{
// bypass scenarios() implementation in the parent class
return Model::scenarios();
}
/**
* Creates data provider instance with search query applied
*
* @param array $params
*
* @return ActiveDataProvider
*/
public function search($params)
{
if(!isset($_GET['id'])){
$id='';
}
else{
$id=$_GET['id'];
}
$query = Checkin::find()->where(['event_id'=> $id]);
$query->joinWith(['event', 'users']);
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
$this->load($params);
if (!$this->validate()) {
// uncomment the following line if you do not want to return any records when validation fails
// $query->where('0=1');
return $dataProvider;
}
$query->andFilterWhere([
'id' => $this->id,
'created_date' => $this->created_date,
'created_by' => $this->created_by,
'updated_date' => $this->updated_date,
'updated_by' => $this->updated_by,
]);
$query->andFilterWhere(['like', 'event.title', $this->event_id]);
$query->andFilterWhere(['like', 'user.firstname', $this->fullName]);
$query->andFilterWhere(['like', 'user.lastname', $this->fullName]);
$query->andFilterWhere(['like', 'user.email', $this->user_id]);
$query->andFilterWhere(['like', 'user_type', $this->user_type]);
return $dataProvider;
}
}
如果我同时添加名字和姓氏过滤器会生成这样
SELECT COUNT(*) FROM `checkin` LEFT JOIN `event` ON `checkin`.`event_id` = `event`.`id`
LEFT JOIN `user` ON `checkin`.`user_id` = `user`.`id` WHERE
((`event_id`='11') AND (`user`.`firstname` LIKE '%text%')) AND
(`user`.`lastname` LIKE '%text%')
我想用生成这样的查询和或或过滤
SELECT * FROM `checkin` LEFT JOIN `event` ON `checkin`.`event_id` = `event`.`id`
LEFT JOIN `user` ON `checkin`.`user_id` = `user`.`id` WHERE
((`event_id`='11') AND (`user`.`firstname` LIKE '%text%')) OR
((`event_id`='11') AND (`user`.`lastname` LIKE '%text%'))
谢谢
您必须更改上面的'user_id'attribute之一到其他名称。 –
@InsaneSkull是的,你是对的,但是'email'来自'firstname'和'lastname'的同一张表,所以我应该用什么来代替'user_id'? –
显示你的表格。 –