模型/表别名不起作用(内部联接)

时间:2017-03-26 06:08:50

标签: mysql cakephp cakephp-3.0 mariadb

问题描述

我将… 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 JOINtests。< / p>

1 个答案:

答案 0 :(得分:1)

我解决了这个问题,我正在使用CakePHP框架。我最终从关联定义中删除了joinType关联,并让框架决定了连接类型。

编辑:我验证了有人告诉我的提示(请参阅OP编辑),原因是我加载的特定记录中的maintainer_idNULL上设置为LEFT JOINLEFT 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