我将…
export function fetchPosts (term = '', random = false) {
return function(dispatch) {
fetch(`${API_URL}posts?term=${term}&random=${random}`, {
method: 'get',
headers: !sessionStorage.jwt ? default_header : auth_header
})
.then(response => response.json())
.then(json => {
if (!json || json.error) {
let default_error = 'An Error Occured.';
throw `Oops! ${json.exception || default_error}`;
}
dispatch(requestPostSuccess(term, json));
})
.catch(error => {
dispatch(requestPostError(error));
});
}
}
…
表与…
class Posts extends Component {
componentWillMount(){
if (this.props.location.pathname.slice(1).split('/')[0] == 'categories'){
this.props.actions.fetchPosts('', true)
}
}
…
表关联两次,一次在别名Users
下。如果我定义了Tests
Maintainers
关联,则不会返回任何结果。我需要弄清楚为什么第一个查询没有返回任何结果。
Maintainers
belongsTo
编辑:我被告知
SELECT
Tests.id AS `Tests__id`,
Tests.title AS `Tests__title`,
Tests.slug AS `Tests__slug`,
Tests.description AS `Tests__description`,
Tests.user_id AS `Tests__user_id`,
Tests.maintainer_id AS `Tests__maintainer_id`,
Tests.test_question_count_total AS `Tests__test_question_count_total`,
Tests.test_question_count_mature AS `Tests__test_question_count_mature`,
Tests.test_session_count_total AS `Tests__test_session_count_total`,
Tests.test_session_count_mature AS `Tests__test_session_count_mature`,
Tests.photo AS `Tests__photo`,
Tests.photo_dir AS `Tests__photo_dir`,
Tests.photo_size AS `Tests__photo_size`,
Tests.view_count AS `Tests__view_count`,
Tests.published AS `Tests__published`,
Tests.mature AS `Tests__mature`,
Tests.flagged AS `Tests__flagged`,
Tests.deleted AS `Tests__deleted`,
Tests.created AS `Tests__created`,
Tests.modified AS `Tests__modified`,
Users.id AS `Users__id`,
Users.first_name AS `Users__first_name`,
Users.last_name AS `Users__last_name`,
Users.username AS `Users__username`,
Users.email AS `Users__email`,
Users.password AS `Users__password`,
Users.token AS `Users__token`,
Users.photo AS `Users__photo`,
Users.photo_dir AS `Users__photo_dir`,
Users.photo_size AS `Users__photo_size`,
Users.bio AS `Users__bio`,
Users.is_admin AS `Users__is_admin`,
Users.created AS `Users__created`,
Users.modified AS `Users__modified`,
Maintainers.id AS `Maintainers__id`,
Maintainers.first_name AS `Maintainers__first_name`,
Maintainers.last_name AS `Maintainers__last_name`,
Maintainers.username AS `Maintainers__username`,
Maintainers.email AS `Maintainers__email`,
Maintainers.password AS `Maintainers__password`,
Maintainers.token AS `Maintainers__token`,
Maintainers.photo AS `Maintainers__photo`,
Maintainers.photo_dir AS `Maintainers__photo_dir`,
Maintainers.photo_size AS `Maintainers__photo_size`,
Maintainers.bio AS `Maintainers__bio`,
Maintainers.is_admin AS `Maintainers__is_admin`,
Maintainers.created AS `Maintainers__created`,
Maintainers.modified AS `Maintainers__modified`
FROM
tests Tests
INNER JOIN users Users ON Users.id = (Tests.user_id)
INNER JOIN users Maintainers ON Maintainers.id = (Tests.maintainer_id)
WHERE
(
Tests.slug = 'what-word-are-youquestion'
AND Tests.deleted IS NULL
)
LIMIT
1
期望两个表中的数据都可能是失败的,因为在
SELECT
Tests.id AS `Tests__id`,
Tests.title AS `Tests__title`,
Tests.slug AS `Tests__slug`,
Tests.description AS `Tests__description`,
Tests.user_id AS `Tests__user_id`,
Tests.maintainer_id AS `Tests__maintainer_id`,
Tests.test_question_count_total AS `Tests__test_question_count_total`,
Tests.test_question_count_mature AS `Tests__test_question_count_mature`,
Tests.test_session_count_total AS `Tests__test_session_count_total`,
Tests.test_session_count_mature AS `Tests__test_session_count_mature`,
Tests.photo AS `Tests__photo`,
Tests.photo_dir AS `Tests__photo_dir`,
Tests.photo_size AS `Tests__photo_size`,
Tests.view_count AS `Tests__view_count`,
Tests.published AS `Tests__published`,
Tests.mature AS `Tests__mature`,
Tests.flagged AS `Tests__flagged`,
Tests.deleted AS `Tests__deleted`,
Tests.created AS `Tests__created`,
Tests.modified AS `Tests__modified`,
Users.id AS `Users__id`,
Users.first_name AS `Users__first_name`,
Users.last_name AS `Users__last_name`,
Users.username AS `Users__username`,
Users.email AS `Users__email`,
Users.password AS `Users__password`,
Users.token AS `Users__token`,
Users.photo AS `Users__photo`,
Users.photo_dir AS `Users__photo_dir`,
Users.photo_size AS `Users__photo_size`,
Users.bio AS `Users__bio`,
Users.is_admin AS `Users__is_admin`,
Users.created AS `Users__created`,
Users.modified AS `Users__modified`
FROM
tests Tests
INNER JOIN users Users ON Users.id = (Tests.user_id)
WHERE
(
Tests.slug = 'what-word-are-youquestion'
AND Tests.deleted IS NULL
)
LIMIT
1
表中,INNER JOIN
是tests
。< / p>
答案 0 :(得分:1)
我解决了这个问题,我正在使用CakePHP
框架。我最终从关联定义中删除了joinType
关联,并让框架决定了连接类型。
编辑:我验证了有人告诉我的提示(请参阅OP编辑),原因是我加载的特定记录中的maintainer_id
在NULL
上设置为LEFT JOIN
。 LEFT JOIN
要求数据都在两个表中。将maintainer_id
设置为现有记录后,一切正常。更好的选择,因为并非总是会填充maintainer_id
,所以使用LEFT JOIN
。
$this->belongsTo('Users', [
'foreignKey' => 'user_id',
'joinType' => 'INNER'
]);
$this->belongsTo('Maintainers', [
'className' => 'Users',
'foreignKey' => 'maintainer_id',
'joinType' => 'INNER'
]);
$this->belongsTo('Users', [
'foreignKey' => 'user_id',
'joinType' => 'INNER'
]);
$this->belongsTo('Maintainers', [
'className' => 'Users',
'foreignKey' => 'maintainer_id'
]);
SELECT
Tests.id AS `Tests__id`,
Tests.title AS `Tests__title`,
Tests.slug AS `Tests__slug`,
Tests.description AS `Tests__description`,
Tests.user_id AS `Tests__user_id`,
Tests.maintainer_id AS `Tests__maintainer_id`,
Tests.test_question_count_total AS `Tests__test_question_count_total`,
Tests.test_question_count_mature AS `Tests__test_question_count_mature`,
Tests.test_session_count_total AS `Tests__test_session_count_total`,
Tests.test_session_count_mature AS `Tests__test_session_count_mature`,
Tests.photo AS `Tests__photo`,
Tests.photo_dir AS `Tests__photo_dir`,
Tests.photo_size AS `Tests__photo_size`,
Tests.view_count AS `Tests__view_count`,
Tests.published AS `Tests__published`,
Tests.mature AS `Tests__mature`,
Tests.flagged AS `Tests__flagged`,
Tests.deleted AS `Tests__deleted`,
Tests.created AS `Tests__created`,
Tests.modified AS `Tests__modified`,
Users.id AS `Users__id`,
Users.first_name AS `Users__first_name`,
Users.last_name AS `Users__last_name`,
Users.username AS `Users__username`,
Users.email AS `Users__email`,
Users.password AS `Users__password`,
Users.token AS `Users__token`,
Users.photo AS `Users__photo`,
Users.photo_dir AS `Users__photo_dir`,
Users.photo_size AS `Users__photo_size`,
Users.bio AS `Users__bio`,
Users.is_admin AS `Users__is_admin`,
Users.created AS `Users__created`,
Users.modified AS `Users__modified`,
Maintainers.id AS `Maintainers__id`,
Maintainers.first_name AS `Maintainers__first_name`,
Maintainers.last_name AS `Maintainers__last_name`,
Maintainers.username AS `Maintainers__username`,
Maintainers.email AS `Maintainers__email`,
Maintainers.password AS `Maintainers__password`,
Maintainers.token AS `Maintainers__token`,
Maintainers.photo AS `Maintainers__photo`,
Maintainers.photo_dir AS `Maintainers__photo_dir`,
Maintainers.photo_size AS `Maintainers__photo_size`,
Maintainers.bio AS `Maintainers__bio`,
Maintainers.is_admin AS `Maintainers__is_admin`,
Maintainers.created AS `Maintainers__created`,
Maintainers.modified AS `Maintainers__modified`
FROM
tests Tests
INNER JOIN users Users ON Users.id = (Tests.user_id)
LEFT JOIN users Maintainers ON Maintainers.id = (Tests.maintainer_id)
WHERE
(
Tests.slug = 'what-word-are-youquestion'
AND Tests.deleted IS NULL
)
LIMIT
1