確定這裏的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'? –
顯示你的表格。 –