如何使用BETWEEN mysql运算符和postCondition方法?

时间:2022-10-16 07:46:47

I'm new to cakePHP and I need to use the controller::postConditions method with the BETWEEN operator but I can't figure out how.

我是cakePHP的新手,我需要在BETWEEN运算符中使用controller :: postConditions方法,但我无法弄清楚如何。

I tried this syntax, as I have read on the controller::find documentation :

我尝试了这种语法,正如我在控制器上看到的:: find文档:

$this->postConditions($this->data, array('Client.client_date_inscription BETWEEN ? AND ?'=>array($this->data['Client']['client_date_inscriptionD'],$this->data['Client']['client_date_inscriptionF'])))

the form input is created like so :

表单输入创建如下:

echo $form->input('Client.client_date_inscriptionD',array('label'=>false,'type'=>'text','class'=>'toDatePicker'));

but this throws this error :

但是这会抛出这个错误:

SQL Error: 1054: Unknown column 'Client.client_date_inscriptionD' in 'where clause' 

So my question is, how do you use the BETWEEN operator with postConditions ?

所以我的问题是,你如何在postConditions中使用BETWEEN运算符?

2 个解决方案

#1


2  

@boobyWomack I changed the uppercase D and F, it's something I've tried randomly.

@boobyWomack我改变了大写的D和F,这是我随机尝试的东西。

I tried many things and lurked in the cookbook to find the answer. I found how to do this by chance actually, not in the postConditions doc.

我尝试了很多东西,潜伏在食谱中寻找答案。我实际上发现了如何偶然地做到这一点,而不是在postConditions doc中。

Here's the postConditions call :

这是postConditions调用:

 $this->postConditions($this->data, array('Client.client_nom' => 'LIKE', 'Client.client_prenom' => 'LIKE', 'Client.client_email' => 'LIKE','Client.client_date_inscription'=>'BETWEEN ? AND ?'))

However, the Client.client_date_inscription input in the view should provide an array of 2 values. I did it with a syntax I've found in the cookbook :

但是,视图中的Client.client_date_inscription输入应提供包含2个值的数组。我用我在cookbook中找到的语法做到了:

$form->input('Client.client_date_inscription.0',array('label'=>false,'type'=>'text','class'=>'toDatePicker'))
$form->input('Client.client_date_inscription.1',array('label'=>false,'type'=>'text','class'=>'toDatePicker'))

Another way is to change manually the value of $this->data['Client']['client_date_inscription'] in the controller, but that wasn't relevant in my case.

另一种方法是在控制器中手动更改$ this-> data ['Client'] ['client_date_inscription']的值,但这与我的情况无关。

Hope this helps anyone with the same issue, since it's not very clear in the cookbook nor in the API.

希望这可以帮助任何有同样问题的人,因为它在食谱和API中都不是很清楚。

#2


1  

hi is your column really got the uppercase D and the uppercase F at the end? I would change that if you are able! Doesnt seem to fit with cake conventions of naming.

嗨,你的专栏真的得到了大写的D和最后的大写字母F?如果你能,我会改变它!似乎不符合命名的蛋糕惯例。

that said it is obviously possible to use these names with Cake.

这说明显然可以在Cake上使用这些名称。

I think you should first try to get the right query with a normal find from Cake. PostCondtions method is a advanced shortcut function really, and it might be better in this case to try to get it working in standard find way.

我想你应该首先尝试使用Cake的正常查找来获得正确的查询。 PostCondtions方法确实是一种高级快捷方式,在这种情况下尝试以标准查找方式工作可能会更好。

then you can use the debug output of the SQL from normal find() to see where it differs with the PostCondtions. Maybe that will help.

然后你可以使用普通find()的SQL调试输出来查看它与PostCondtions的不同之处。也许这会有所帮助。

I also notice the syntax in the docs for PostConditions hint at something like 'referrer'=>'BETWEEN' (array parameters needed basically - so read the API or actual docs for the function and you might find the option you need there!)

我还注意到PostConditions文档中的语法提示类似'referrer'=>'BETWEEN'(基本上需要数组参数 - 所以请阅读函数的API或实际文档,你可能会找到你需要的选项!)

Someone on IRC may know something obscure like this- did you try there?

IRC上的某个人可能会知道像这样模糊不清的东西 - 你在那里试过吗?

good luck, please post back if you find the answer I would like to know :)

祝你好运,如果你找到我想知道的答案,请回复:)

#1


2  

@boobyWomack I changed the uppercase D and F, it's something I've tried randomly.

@boobyWomack我改变了大写的D和F,这是我随机尝试的东西。

I tried many things and lurked in the cookbook to find the answer. I found how to do this by chance actually, not in the postConditions doc.

我尝试了很多东西,潜伏在食谱中寻找答案。我实际上发现了如何偶然地做到这一点,而不是在postConditions doc中。

Here's the postConditions call :

这是postConditions调用:

 $this->postConditions($this->data, array('Client.client_nom' => 'LIKE', 'Client.client_prenom' => 'LIKE', 'Client.client_email' => 'LIKE','Client.client_date_inscription'=>'BETWEEN ? AND ?'))

However, the Client.client_date_inscription input in the view should provide an array of 2 values. I did it with a syntax I've found in the cookbook :

但是,视图中的Client.client_date_inscription输入应提供包含2个值的数组。我用我在cookbook中找到的语法做到了:

$form->input('Client.client_date_inscription.0',array('label'=>false,'type'=>'text','class'=>'toDatePicker'))
$form->input('Client.client_date_inscription.1',array('label'=>false,'type'=>'text','class'=>'toDatePicker'))

Another way is to change manually the value of $this->data['Client']['client_date_inscription'] in the controller, but that wasn't relevant in my case.

另一种方法是在控制器中手动更改$ this-> data ['Client'] ['client_date_inscription']的值,但这与我的情况无关。

Hope this helps anyone with the same issue, since it's not very clear in the cookbook nor in the API.

希望这可以帮助任何有同样问题的人,因为它在食谱和API中都不是很清楚。

#2


1  

hi is your column really got the uppercase D and the uppercase F at the end? I would change that if you are able! Doesnt seem to fit with cake conventions of naming.

嗨,你的专栏真的得到了大写的D和最后的大写字母F?如果你能,我会改变它!似乎不符合命名的蛋糕惯例。

that said it is obviously possible to use these names with Cake.

这说明显然可以在Cake上使用这些名称。

I think you should first try to get the right query with a normal find from Cake. PostCondtions method is a advanced shortcut function really, and it might be better in this case to try to get it working in standard find way.

我想你应该首先尝试使用Cake的正常查找来获得正确的查询。 PostCondtions方法确实是一种高级快捷方式,在这种情况下尝试以标准查找方式工作可能会更好。

then you can use the debug output of the SQL from normal find() to see where it differs with the PostCondtions. Maybe that will help.

然后你可以使用普通find()的SQL调试输出来查看它与PostCondtions的不同之处。也许这会有所帮助。

I also notice the syntax in the docs for PostConditions hint at something like 'referrer'=>'BETWEEN' (array parameters needed basically - so read the API or actual docs for the function and you might find the option you need there!)

我还注意到PostConditions文档中的语法提示类似'referrer'=>'BETWEEN'(基本上需要数组参数 - 所以请阅读函数的API或实际文档,你可能会找到你需要的选项!)

Someone on IRC may know something obscure like this- did you try there?

IRC上的某个人可能会知道像这样模糊不清的东西 - 你在那里试过吗?

good luck, please post back if you find the answer I would like to know :)

祝你好运,如果你找到我想知道的答案,请回复:)