我正在用 CakePHP(最新版本)编写标签搜索,但与 CakePHP 的其余部分的简单程度相比,我所做的解决方案似乎过于复杂。希望有人能为我指明正确的方向或帮助我改进当前的解决方案。
我的应用程序中的每个用户都可以使用标签来标记自己,例如:php、objective-c、javascript、jquery。不同类型的用户可以搜索具有特定标签的用户。他们可能会搜索:php、objective-c、ios。我需要按照匹配的标签数量的顺序返回一个用户数组,具有所有 3 个标签的用户将出现在数组的顶部。
下面是数据库示例和我的解决方案。如果能帮助我改进这一点,我将不胜感激。
[数据库]
[解决方案]
//Search Array
//Format: array('objective-c', 'javascript', 'jquery', 'php')
$tag_array = explode(",", $this->request->data["User"]["search"]);
//Get Tag IDs - array([id] => [id])
//Format: array([1] => '1', [2] => '2', [4] => '4', [15] => '15')
$result_tag_ids = $this->User->TagsUser->Tag->find('list', array(
'conditions' => array(
"Tag.name" => $tag_array
),
'fields' => array('Tag.id')
));
//Get User IDs - array([id] => [id])
//Format: array([24] => '24', [24] => '24', [26] => 26, [27] => '27')
$result_user_ids = $this->User->TagsUser->find('list', array(
'conditions' => array(
"TagsUser.tag_id" => $result_tag_ids
),
'fields' => array('TagsUser.user_id')
));
//Remove Duplicate user ids and add a count of how many tags matched & sort the array in that order
//Format: array([26] => 1, [24] => 2, [27] => 3)
$counted_user_ids = array_count_values($result_user_ids);
asort($counted_user_ids);
//Get the keys (user_ids)
$list_user_ids = array_keys($counted_user_ids);
//Get these users in the order of the sorted array
$search_result = $this->User->find('all', array(
'conditions' => array(
"User.id" => $list_user_ids
),
'order' => 'FIELD(User.id,' . implode(" ,", $list_user_ids) . ')'
));
最佳答案
请试一试:
$tag_array = explode(",", $this->request->data["User"]["search"]);
//arrays expanded for better readability,
//you should be able to compress in fewer lines if desired
$options = array();
$options['contain'] = '';
//or recursive=-1, depends of what you are using to avoid extra models/fields
$options['joins'][0]['table'] = 'tags_users';
$options['joins'][0]['conditions'] = 'User.id = user_id';
$options['joins'][1]['alias'] = 'Tag';
$options['joins'][1]['table'] = 'tags';
$options['joins'][1]['conditions']= 'Tag.id = tag_id';
$options['fields'] = array('User.id', 'COUNT(*) as tag_counter');
$options['group'] = 'User.id';
$options['order'] = 'tag_counter DESC';
$options['conditions']['Tag.name'] = $tag_array;
$search_result = $this->User->find('all', $options);
print_r($search_result) 应该给出:
Array
(
[0] => Array
(
[User] => Array
(
[id] => (user id)
)
[0] => Array
(
[tag_counter] => (how many tags)
)
)
[...]
)
希望对你有用。 如果您还想知道每个用户在同一查询中有哪些标签,只需调整包含或递归值即可。
关于php - CakePHP Complex 查找/查询 - 改进当前解决方案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11988062/
我正在用Ruby编写一个简单的程序来检查域列表是否被占用。基本上它循环遍历列表,并使用以下函数进行检查。require'rubygems'require'whois'defcheck_domain(domain)c=Whois::Client.newc.query("google.com").available?end程序不断出错(即使我在google.com中进行硬编码),并打印以下消息。鉴于该程序非常简单,我已经没有什么想法了-有什么建议吗?/Library/Ruby/Gems/1.8/gems/whois-2.0.2/lib/whois/server/adapters/base.
在MRIRuby中我可以这样做:deftransferinternal_server=self.init_serverpid=forkdointernal_server.runend#Maketheserverprocessrunindependently.Process.detach(pid)internal_client=self.init_client#Dootherstuffwithconnectingtointernal_server...internal_client.post('somedata')ensure#KillserverProcess.kill('KILL',
我知道我可以指定某些字段来使用pluck查询数据库。ids=Item.where('due_at但是我想知道,是否有一种方法可以指定我想避免从数据库查询的某些字段。某种反拔?posts=Post.where(published:true).do_not_lookup(:enormous_field) 最佳答案 Model#attribute_names应该返回列/属性数组。您可以排除其中一些并传递给pluck或select方法。像这样:posts=Post.where(published:true).select(Post.attr
我刚刚被困在这个问题上一段时间了。以这个基地为例:moduleTopclassTestendmoduleFooendend稍后,我可以通过这样做在Foo中定义扩展Test的类:moduleTopmoduleFooclassSomeTest但是,如果我尝试通过使用::指定模块来最小化缩进:moduleTop::FooclassFailure这失败了:NameError:uninitializedconstantTop::Foo::Test这是一个错误,还是仅仅是Ruby解析变量名的方式的逻辑结果? 最佳答案 Isthisabug,or
我正在尝试解析一个CSV文件并使用SQL命令自动为其创建一个表。CSV中的第一行给出了列标题。但我需要推断每个列的类型。Ruby中是否有任何函数可以找到每个字段中内容的类型。例如,CSV行:"12012","Test","1233.22","12:21:22","10/10/2009"应该产生像这样的类型['integer','string','float','time','date']谢谢! 最佳答案 require'time'defto_something(str)if(num=Integer(str)rescueFloat(s
相信很多人在录制视频的时候都会遇到各种各样的问题,比如录制的视频没有声音。屏幕录制为什么没声音?今天小编就和大家分享一下如何录制音画同步视频的具体操作方法。如果你有录制的视频没有声音,你可以试试这个方法。 一、检查是否打开电脑系统声音相信很多小伙伴在录制视频后会发现录制的视频没有声音,屏幕录制为什么没声音?如果当时没有打开音频录制,则录制好的视频是没有声音的。因此,建议在录制前进行检查。屏幕上没有声音,很可能是因为你的电脑系统的声音被禁止了。您只需打开电脑系统的声音,即可录制音频和图画同步视频。操作方法:步骤1:点击电脑屏幕右下侧的“小喇叭”图案,在上方的选项中,选择“声音”。 步骤2:在“声
首先回顾一下拉格朗日定理的内容:函数f(x)是在闭区间[a,b]上连续、开区间(a,b)上可导的函数,那么至少存在一个,使得:通过这个表达式我们可以知道,f(x)是函数的主体,a和b可以看作是主体函数f(x)中所取的两个值。那么可以有, 也就意味着我们可以用来替换 这种替换可以用在求某些多项式差的极限中。方法: 外层函数f(x)是一致的,并且h(x)和g(x)是等价无穷小。此时,利用拉格朗日定理,将原式替换为 ,再进行求解,往往会省去复合函数求极限的很多麻烦。使用要注意:1.要先找到主体函数f(x),即外层函数必须相同。2.f(x)找到后,复合部分是等价无穷小。3.要满足作差的形式。如果是加
深度学习部署:Windows安装pycocotools报错解决方法1.pycocotools库的简介2.pycocotools安装的坑3.解决办法更多Ai资讯:公主号AiCharm本系列是作者在跑一些深度学习实例时,遇到的各种各样的问题及解决办法,希望能够帮助到大家。ERROR:Commanderroredoutwithexitstatus1:'D:\Anaconda3\python.exe'-u-c'importsys,setuptools,tokenize;sys.argv[0]='"'"'C:\\Users\\46653\\AppData\\Local\\Temp\\pip-instal
如何找到调用此方法的位置?defto_xml(options={})binding.pryoptions=options.to_hifoptions&&options.respond_to?(:to_h)serializable_hash(options).to_xml(options)end 最佳答案 键入caller。这将返回当前调用堆栈。文档:Kernel#caller.例子[0]%rspecspec10/16|===================================================62=====
我正在尝试查询我的Rails数据库(Postgres)中的购买表,我想查询时间范围。例如,我想知道在所有日期的下午2点到3点之间进行了多少次购买。此表中有一个created_at列,但我不知道如何在不搜索特定日期的情况下完成此操作。我试过:Purchases.where("created_atBETWEEN?and?",Time.now-1.hour,Time.now)但这最终只会搜索今天与那些时间的日期。 最佳答案 您需要使用PostgreSQL'sdate_part/extractfunction从created_at中提取小时