-1

我的一个控制器功能有另一个问题。我编写了一个函数来更新我的数据库中的一些字段。我正在从解码的 json_file 中获取内容,在获取数据后,我再次对其进行编码,以便将其存储在我的数据库中。我收到一些我不明白的错误。

我的功能

public function admin_update_rulings()
{
    $this->loadModel('Magicsets');
    $this->loadModel('Cards');
    $this->autoRender = false;
    $code = 'LEA';
    $cards = file_get_contents('http://mtgjson.com/json/' . $code . '-x.json');

    $decodedcards = json_decode($cards);
    $this->Card->query("SET CHARACTER SET utf8");


    foreach ($decodedcards->cards as $cards) {
        $mvid = $cards->multiverseid;
        $legal = json_encode($cards->legalities);
        $rulings = '';
        if (!empty($cards->rulings)) {
            $rulings = json_encode($cards->rulings);
        } else {
            $rulings = json_encode('leeg');
        }

        $this->Cards->updateAll(
            array('rulings' => $rulings),   //fields to update
            array('multiverseid' => $mvid)  //condition
        );
    }
}

我得到的错误信息:

数据库错误

错误:SQLSTATE[42000]:语法错误或访问冲突:1064 您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以在第 1 行的 '[{"date":"2008-08-01","text":"This card has been returned to its original functi' 附近使用正确的语法

SQL 查询:更新magicmmcards原样。Cards_ = [{"date":"2008-08-01","text":"这张卡已经恢复了原来的功能。如果它为已经是生物的神器附魔,它不会改变它的力量和防御力."},{"date":"2008-08-01","text":"一个变成生物的非生物永久物可以攻击,并且它的 {T} 异能可以被起动,前提是它的操控者持续操控那个永久物从他或她最近的回合开始。无论永久物成为生物多久。"}] WHERE = 96Cardsrulingsmultiverseid

注意:如果要自定义此错误消息,请创建 app/View/Errors/pdo_error.ctp

任何有关解决此问题的帮助将不胜感激:-)。

4

2 回答 2

0

updateAll()在 Cake 中使用时需要将字符串用引号引起来。您可以使用数据源的value()方法执行此操作:-

$db = $this->getDataSource();
$value = $db->value($rulings, 'string');
$this->Cards->updateAll(
    array('rulings' => $rulings),   //fields to update
    array('multiverseid' => $mvid)  //condition
);

您最好使用save()而不是updateAll()在保存时知道主键。

请参阅接受的错误答案:SQLSTATE[42000]:语法错误或访问冲突:1064 您的 SQL 语法有错误;查看手册以获得更完整的解释。

于 2015-06-30T10:24:34.373 回答
0

谢谢大家的建议。我决定研究你们俩建议我使用的保存方法。它为我解决了这个问题,现在一切正常。我将更新代码更改为此。

        $updaterow = $this->Cards->find('first', array('fields' => array('id'), 'conditions' => array('multiverseid' => $mvid), 'limit' => 1));
        $id = $updaterow['Cards']['id'];

        $data = array('id' => $id, 'rulings' => $rulings, 'legalities' => $legal);

        $this->Cards->save($data);
于 2015-06-30T10:44:36.300 回答