2017-08-10 125 views
0
public function search_between() { 

$min = $this->request->query['min']; 
$max = $this->request->query['max']; 

$conditions = array('Ad.price BETWEEN ? AND ?' => array($min,$max)); 
$ads = $this->Anuncio->find('all',array('conditions' => $conditions)); 
$this->set('ads',$ads); 

} 

警告:strlen()期望参数1是字符串,数组给定。CakePHP 2.8为什么我的search_between函数给我一个警告?

**Thank's for your help I'm new to asking questions here.** 
+0

你能张贴你在哪里调用'strlen的()' ? – bill

+0

我不调用strlen()它的一个警告显示cakephp当我将数组中的变量($ min,$ max)传递给Between:array('Ad.price BETWEEN?AND?'=> array( $ min,$ max)< - 这些数组会导致警告,但我不知道为什么。谢谢 –

+0

你可以尝试抛出你的请求生成的sql吗?这是一个很好的方法来开始调试这个 – Amjo

回答

0

试试这个:

public function search_between() { 

$min = $this->request->query['min']; 
$max = $this->request->query['max']; 

$conditions = array('Anuncio.price >=' => $min,'Anuncio.price <=' $max)); 
$ads = $this->Anuncio->find('all',array('conditions' => $conditions)); 
$this->set('ads',$ads); 
+0

我试过这些代码。工作我纠正你的代码中的一些错误,并尝试但不工作:'Anuncio.price <='<---和这些---->'Anuncio.price> =')显示错误错误:SQLSTATE [42000]:语法错误或访问冲突:1064您的SQL语法出错,无法正常工作。谢谢您的帮助。 –

0

您可以使用CakePHP MySQL查询将作品等作为CakePHP的查询

if(!empty($min) && !empty($max)){ 
    $ads = $this->Anuncio->query('SELECT * from Anuncio WHERE Anuncio.price BETWEEN "'.$min.'" AND "'.$max.'"'); 
} 

else{ 
$ads = $this->Anuncio->query('SELECT * from Anuncio'); 
} 
+0

我要试一试你的代码如果它有效,我会结束这个问题。非常感谢你! –

+0

我在网上读到strlen()不支持数组,如果你传递一个数组,警告显示。 –

+0

我修复了在位于c:\ wamp \ your_project_name \\ lib \ Cake \ Utility \ CakeText.php第221行的文件中更改strlen的问题,并更改了$ offset = $ pos + strlen($ val);到$ offset = $ pos + count($ val);只能改变strlen来计数,现在警告会按预期飞走代码工作。 Tnx的帮助。 –

相关问题