gpt4 book ai didi

php - cakephp - 搜索整个模型和 HasManys 并使用通配符

转载 作者:行者123 更新时间:2023-11-29 14:01:11 25 4
gpt4 key购买 nike

我有一个用户模型和另外 3 个用户模型 (HasManys):技能、经验、资格

public $hasMany = array(

  'Experience' => array(...
'Qualification' => array(...
'Skill' => array(...

这 3 个型号都有

public $belongsTo = array( 'User' => array(...

技能、经验和资格都包含一个名为“标题”和“描述”的字段

通过所有 3 个模型的每个OR“标题”和“描述”进行通配符搜索 (%search%) 并返回符合条件的用户的最佳方式是什么?

我真的很难理解它,无论我搜索什么,都与我想要的相反,或者只是不相关。

有人知道如何完成这样的任务吗?

到目前为止我有这样的事情:

$cond=array('OR'=>array(
"Skills.name LIKE '%$keyword%'",
"Experiences.name LIKE '%$keyword%'",
"Experiences.description LIKE '%$keyword%'")
);

$conditions = array('conditions'=>$cond);
$list = $this->User->find('all',
array('conditions') => $cond);

$list = $this->User->find('all', array('recursive'=> 1, 'conditions'=> $cond));

如果我能提供任何帮助,我将不胜感激,因为我花了 5 个小时的时间寻找并尝试不同的方法,但实际上没有任何效果。

干杯!

最佳答案

我将使用“关键字”条件过滤每个模型并从中收集用户 ID。接下来,使用用户 ID 列表返回用户列表。类似的东西;

// get distinct user-ids and simplify the results array with Hash::extract()
$userIds = Hash::extract($this->User->Skill->find('all', array(
'fields' => array('DISTINCT Skill.user_id'),
'conditions' => array('Skill.name LIKE ?' => "%$keyword%"),
'recursive' => -1
)), '{n}.User.id');

// repeat for the other tables, and merge the user-ids

.....


// Use the list of user-ids for retrieve matching users
$this->User->find('all', array('conditions' => array('User.id' => $userIds)));

但是,根据用户数量,这可能会返回大量用户 ID 列表,这可能并不理想。最后,总是可以使用自定义查询并通过 model->query() 执行该查询。

关于php - cakephp - 搜索整个模型和 HasManys 并使用通配符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15029513/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com