查找条件和doen't工作+ cakephp [英] Find Condition by And doen't work + cakephp

查看:154
本文介绍了查找条件和doen't工作+ cakephp的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此函数正在处理OR条件。我需要这个工作与条件。
任何帮助:

  $ ands = array() 
foreach($ array_training_id as $ id){
$ ands [] = array('TrainingsUser.training_id'=> $ id);
}
$ conditions = array('AND'=> $ ands);
$ users = $ this-> TrainingsUser-> find('all',array('conditions'=> $ conditions));

我想像这样写一个查询AND条件:

  SELECT user_id FROM trainings_users WHERE training_id = 172 AND training_id = 174 

此查询在获取第一个标识时正在工作。

表解释:
我有3个表Training - User - Training_users ... Training_users表有traing_id和user_id 我想要的是一些这样的东西: SELECT user_id FROM Training_users WHERE training_id = 172 AND training_id = 174 ...甚至在SqlMyadmin中工作







当我提供training_id 172 AND training_id 174时,我只需要返回用户150


<为什么你的$ array_project_id首先是一个字符串(除了命名冲突^^)?
它应该是一个ids数组,并且蛋糕将自动能够开箱即用。



好吧,

  $ ids = explode(',',$ array_project_id); 
//现在是一个干净的数组

$ users = $ this-> Training-> find('all',array('conditions'=>
array ('Training.Project_id'=> $ ids)
));

在您的注释后,我仍然看不到使用AND这里的值。
但是,这里你走了:

  $ ands = array(); 
foreach($ ids as $ id){
$ ands [] = array('Training.Project_id'=> $ id);
}
$ condition = array('AND'=> $ ands);
$ users = $ this-> Training-> find('all',array('conditions'=> $ conditions));

Voila。但它仍然不会工作。



似乎你使用一些HABTM关系(你应该在开始时提供这个重要的信息)。然后你需要查询中间模型/关系,以便在这里有两个belongsTo关系(=左连接)。



你真正需要什么只是发布相关关系和关于Training_users的信息吗?)是查询中间表Training_users,在belongsTo方向(左连接到用户)中包含一个包含。
最重要的事实:使用OR而不是AND。这将得到你想要的结果。


This function is working on OR condition. I need this to work with AND condition. Any help:

$ands = array();
foreach ($array_training_id as $id) {
        $ands[] = array('TrainingsUser.training_id' => $id);
}
$conditions = array('AND' => $ands);
$users = $this->TrainingsUser->find('all', array('conditions' => $conditions));

I'd like to write a query AND condition like this:

SELECT user_id FROM trainings_users WHERE training_id = 172 AND training_id = 174

This query is working when is getting the first id.
Table explanation: I have 3 table Training - User - Training_users... Training_users table have the traing_id and user_id What I want I need is some thing like this : SELECT user_id FROM Training_users WHERE training_id = 172 AND training_id = 174... which it doesn't work even in SqlMyadmin


what i need when I give training_id 172 AND training_id 174 it have to return user 150 only

解决方案

Why is your $array_project_id a string in the first place (besides the naming conflict^^)? It should be an array of ids and cake will automatically be able to work with it out of the box.

Well, anyway:

$ids = explode(',', $array_project_id);
// now its a clean array

$users = $this->Training->find('all', array('conditions' => 
    array('Training.Project_id' => $ids)
));

After your comments I still don't see the value in using AND here. But what the heck, here you go:

$ands = array();
foreach ($ids as $id) {
    $ands[] = array('Training.Project_id' => $id);
}
$conditions = array('AND' => $ands);
$users = $this->Training->find('all', array('conditions' => $conditions));

Voila. It will still never work, though.

It seems you are using some HABTM relation (you should have provided this vital piece of information in the beginning!). Then you need to query the intermediate model/relation in order to have two belongsTo-relations (= left joins) here.

What you REALLY need (again: why did you just post the relevant relation and the information about Training_users now?) is to query the intermediate table "Training_users" with one contain in the belongsTo direction (left joins to users). And the most important fact: using OR instead of AND. This will get the results you want.

这篇关于查找条件和doen't工作+ cakephp的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆