我想使用 CakePHP 的 find 方法,这样我就可以利用它的模型关联。问题是我有这个复杂的 SQL 搜索,但我不知道如何将它翻译成 CakePHP 的 find 方法。我一直在使用 $model->query() 然后在 ID 上使用 Set::extract 。问题是我失去了相关顺序。如何将其转换为 Cake 中的 find 方法以便我可以使用关联?
SQL:
SELECT id, title, SUM(relevance) as total_relevance FROM (
SELECT
card_definitions.id,
card_definitions.title,
(MATCH(card_definitions.title) AGAINST ('%s')) * 5 AS relevance
FROM card_definitions
UNION
SELECT
card_definitions.id,
card_definitions.title,
(MATCH(card_def_identities.special_ability_text) AGAINST ('%s')) * 0.5 AS relevance
FROM card_def_identities
INNER JOIN card_definitions ON card_def_identities.card_def_sid = card_definitions.id
UNION
SELECT
card_definitions.id,
card_definitions.title,
(MATCH(brigades.brigade_color) AGAINST ('%s')) AS relevance
FROM brigades
INNER JOIN card_def_brigades ON brigades.id = card_def_brigades.brigade_sid
INNER JOIN card_definitions ON card_def_brigades.card_def_sid = card_definitions.id
UNION
SELECT
card_definitions.id,
card_definitions.title,
(MATCH(identifiers.identifier) AGAINST ('%s')) AS relevance
FROM identifiers
INNER JOIN card_def_identifiers ON identifiers.id = card_def_identifiers.identifier_sid
INNER JOIN card_definitions on card_def_identifiers.card_def_sid = card_definitions.id
UNION
SELECT
card_definitions.id,
card_definitions.title,
(MATCH(card_effects.effect) AGAINST ('%s')) AS relevance
FROM card_effects
INNER JOIN card_def_effects ON card_effects.id = card_def_effects.effect_sid
INNER JOIN card_definitions on card_def_effects.card_def_sid = card_definitions.id
) AS combined_search
GROUP BY id
HAVING total_relevance > 0
ORDER BY total_relevance DESC
LIMIT 10;
%s
替换为搜索查询。
感谢您的任何帮助,您可以提供。