-1

I have another problem with one of my controller functions. I have written a function that updates some fields from my database. I am grabbing the content from a decoded json_file and after I grabbed the data I have encoding it again so I could store it in my database. I am getting some errors I do not understand.

My function

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
        );
    }
}

The error message I get:

Database Error

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 '[{"date":"2008-08-01","text":"This card has been returned to its original functi' at line 1

SQL Query: UPDATE magicmm.cards AS Cards SET Cards.rulings = [{"date":"2008-08-01","text":"This card has been returned to its original functionality. If it is enchanting an artifact that's already a creature, it won't change its power and toughness."},{"date":"2008-08-01","text":"A noncreature permanent that turns into a creature can attack, and its {T} abilities can be activated, only if its controller has continuously controlled that permanent since the beginning of his or her most recent turn. It doesn't matter how long the permanent has been a creature."}] WHERE multiverseid = 96

Notice: If you want to customize this error message, create app/View/Errors/pdo_error.ctp

Any help on solving this issue would be greatly appreciated :-).

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 に答える