CakePHP 3.x 使用分页查询许多 Table 个对象
CakePHP 3.x Query many Table objects with Pagination
上下文:
我正在使用 CakePHP 3.x,我正在尝试使用分页构建 ORM 查询。我有表用户、主题和区域。用户可以有很多主题,但只有一个领域。这是数据库模型:
CREATE TABLE users (
id INT AUTO_INCREMENT PRIMARY KEY,
email VARCHAR(255) NOT NULL,
password VARCHAR(255) NOT NULL,
);
CREATE TABLE topics (
id INT AUTO_INCREMENT PRIMARY KEY,
user_id INT NOT NULL,
name VARCHAR(255),
description TEXT,
FOREIGN KEY user_key (user_id) REFERENCES users(id)
);
CREATE TABLE areas (
id INT AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(255),
UNIQUE KEY (title)
);
CREATE TABLE users_areas (
user_id INT NOT NULL,
area_id INT NOT NULL,
PRIMARY KEY (user_id, area_id),
FOREIGN KEY area_key(area_id) REFERENCES areas(id),
FOREIGN KEY user_key(user_id) REFERENCES users(id)
);
我需要什么
我需要按给定区域筛选所有主题。
我正在尝试下一个:
//1. get the area from the url (OK)
$area = $this->request->getQuery('area');
//2. Get the area from db to get the id
$myarea = $this->Areas->find()->where(['title' => $area])->first();
//3. Get all the users related to the area given
$users = $this->Users->find()->matching('Areas', function ($q){
return $q->where(['Areas.id' => $myarea->id]);
});
//4. Get the Topics and Paginate
$topics = $this->paginate($this->Topics->find('all')->innerJoinWith('Users', function ($q){
return $q->where(['User.id' => $myarea->id]);
}));
//5. Set the topics (OK)
$this->set(compact('topics'));
$this->set('_serialize', ['topics']);
我遇到了很多错误,我不确定如何正确构建查询。
乍一看,至少有一个错误:
$myarea
在匿名函数中是不可见的。
如果您想在 lambda 函数中使用 $myarea
,您应该从父作用域继承此变量:use($var)
。
http://php.net/manual/en/functions.anonymous.php 示例 #3
$users = $this->Users->find()->matching('Areas', function ($q) use ($myarea) {
return $q->where(['Areas.id' => $myarea->id]);
});
$topics = $this->paginate($this->Topics->find('all')->innerJoinWith('Users', function ($q){
return $q->where(['User.id' => $myarea->id]);
}));
上下文:
我正在使用 CakePHP 3.x,我正在尝试使用分页构建 ORM 查询。我有表用户、主题和区域。用户可以有很多主题,但只有一个领域。这是数据库模型:
CREATE TABLE users (
id INT AUTO_INCREMENT PRIMARY KEY,
email VARCHAR(255) NOT NULL,
password VARCHAR(255) NOT NULL,
);
CREATE TABLE topics (
id INT AUTO_INCREMENT PRIMARY KEY,
user_id INT NOT NULL,
name VARCHAR(255),
description TEXT,
FOREIGN KEY user_key (user_id) REFERENCES users(id)
);
CREATE TABLE areas (
id INT AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(255),
UNIQUE KEY (title)
);
CREATE TABLE users_areas (
user_id INT NOT NULL,
area_id INT NOT NULL,
PRIMARY KEY (user_id, area_id),
FOREIGN KEY area_key(area_id) REFERENCES areas(id),
FOREIGN KEY user_key(user_id) REFERENCES users(id)
);
我需要什么
我需要按给定区域筛选所有主题。
我正在尝试下一个:
//1. get the area from the url (OK)
$area = $this->request->getQuery('area');
//2. Get the area from db to get the id
$myarea = $this->Areas->find()->where(['title' => $area])->first();
//3. Get all the users related to the area given
$users = $this->Users->find()->matching('Areas', function ($q){
return $q->where(['Areas.id' => $myarea->id]);
});
//4. Get the Topics and Paginate
$topics = $this->paginate($this->Topics->find('all')->innerJoinWith('Users', function ($q){
return $q->where(['User.id' => $myarea->id]);
}));
//5. Set the topics (OK)
$this->set(compact('topics'));
$this->set('_serialize', ['topics']);
我遇到了很多错误,我不确定如何正确构建查询。
乍一看,至少有一个错误:
$myarea
在匿名函数中是不可见的。
如果您想在 lambda 函数中使用 $myarea
,您应该从父作用域继承此变量:use($var)
。
http://php.net/manual/en/functions.anonymous.php 示例 #3
$users = $this->Users->find()->matching('Areas', function ($q) use ($myarea) {
return $q->where(['Areas.id' => $myarea->id]);
});
$topics = $this->paginate($this->Topics->find('all')->innerJoinWith('Users', function ($q){
return $q->where(['User.id' => $myarea->id]);
}));