CAKEPHP 2.0,填充具有可变条件的查找条件数组

时间:2015-06-26 10:53:16

标签: php arrays cakephp find conditional-statements

我正在为我的一个观点制作过滤器。我想根据if/else语句填写条件数组,该语句检查从网址收到的数据。

我试图建立它有点像这样。我将创建多个if/else语句。如果该陈述为真,我希望条件如下所示填写,否则将条件保持为空。正在运行else语句中的find()查询并创建错误。

//$set and $kleur are variables that come from the URL, they work
public function seriesfilter($foil='search', $color, $set, $name){
    $this->loadModel('Cards');
    $this->loadModel('Magicsets');
    $setname = $this->Magicsets->findByCode($set);
    $this->Card->recursive=-1;
    $this->layout = 'ajax';

    $kleur = "%".$color."%";
    $condition = "'manaCost like' => $kleur";

    $conditions = array(
        'set' => $set,
        $condition
    );


    if ($foil == 'nonfoil'){
        if($color == 'all'){
        $cards= $this->Cards->find('all',array('fields'=>'multiverseid,name,nprice,nstock,set,rarity','conditions'=>array('set' => $set),'limit'=>750));
        }
        else{
            $cards= $this->Cards->find('all',array('fields'=>'multiverseid,name,nprice,nstock,set,rarity','manaCost','conditions' => $conditions,'limit'=>750));
        }
    }

下面你可以看到我收到的错误

Error: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '=> %R% LIMIT 750' at line 1

SQL Query: SELECT `Cards`.`multiverseid`, `Cards`.`name`, `Cards`.`nprice`, `Cards`.`nstock`, `Cards`.`set`, `Cards`.`rarity` FROM `magicmm`.`cards` AS `Cards` WHERE `set` = 'BNG' AND 'manaCost like' => %R% LIMIT 750

设置多个条件的任何帮助都会很棒。我只想在条件数组中添加通过url接收的条件。

亲切的问候,

尼克

1 个答案:

答案 0 :(得分:0)

'条件应该LIKE秒。试试 -

$condition = "manaCost like '%$kleur%'";

$conditions = array(
    'set' => $set,
    0 => $condition
);