【问题标题】:CAKEPHP 2.0, fill find condition array with variable conditionsCAKEPHP 2.0,用可变条件填充查找条件数组
【发布时间】:2015-09-13 07:10:47
【问题描述】:

我目前正在为我的一种观点制作过滤器。我想根据 if/else 检查从 url 收到的数据的语句填充条件数组。

我正在尝试像这样构建它。我将创建多个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 接收的条件数组中。

亲切的问候,

尼克

【问题讨论】:

    标签: php arrays cakephp find conditional-statements


    【解决方案1】:

    LIKE 条件应该有 's。试试 -

    $condition = "manaCost like '%$kleur%'";
    
    $conditions = array(
        'set' => $set,
        0 => $condition
    );
    

    【讨论】:

    • 我有一个问题,类名的第一个字母应该大写吗?还是不再重要了? (在 php 中) Tnx
    • 我们应该遵守约定。对于 2.x,CakePHP 正常工作很重要。
    • 哇,脚本现在可以运行了!非常感谢。我尝试了许多变体,但我不确定如果引号来自变量,查找查询将如何读取它们。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-10-22
    • 1970-01-01
    • 2021-08-28
    • 1970-01-01
    • 1970-01-01
    • 2012-08-27
    • 1970-01-01
    相关资源
    最近更新 更多