doubi9615
2012-11-18 19:15 阅读 76
已采纳

CakePHP sql查询

I'm sort of new to CakePHP and can't figure out how sql queries work here. I have a delete button which erases news about players. What it's not doing (and it should) is also take care of number of news, which is stored in "news" column in "fc_players" table. In an old-fashioned way I'd go like:

("SELECT player FROM entries WHERE id = '" . $id . "'");

(returns $name of the player; can't just use $id because of how database is built; "entries" is a table where news are stored)

("SELECT news FROM fc_players WHERE name = '" . $name . "'");

(returns how many news about certain player is in the database)

If only one, delete $name from database:

("DELETE FROM fc_players WHERE name = '" . $name . "'");

I've tried to do: $this->FCPlayer->deleteAll(array('FCPlayer.name' => $name)); but for some reason it doesn't work.

If more, simply reduce it:

$news = $news - 1;

("UPDATE fc_players SET news = '" . $news . "' WHERE name = '" . $name . "'");

I can handle it in plain PHP but Cake is a bit harder and I can't find any useful examples. Can you help me to translate it from sql queries to CakePHP or guide me to articles which are actually explaining it?

Thanks!

EDIT:

Ok, Nik and Steve, thanks for you answers. Unfortunately it doesn't solve my problem. I think there's something wrong with handling sql queries.

$this->Session->setFlash($id, 'success'); shows me correct entry id, so $id is fine.

But when I'm trying to get player name with:

$player = $this->Entry->field("player", array("id" => $id));

or with

$this->Entry->id = $id;
$player = $this->Entry->field('player');

or execute query

player = $this->Entry->query("SELECT player FROM entries WHERE id = '" . $id . "'");

the result is blank. I did manage to get player name by doing:

$player = $this->Entry->field('player');

but it simply returned name of the player from most recent entry, not the one I'm deleting.

I just can't figure out where I'm making mistake. Is it possible that it returns some kind od array or object that setFlash cannot process? If so, why last example works (sort of)?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

3条回答 默认 最新

  • 已采纳
    dqhr76378 dqhr76378 2012-11-18 20:01

    When you delete a piece of news, CakePHP wont take care of the number of views in any other table. Is not so intelligent yet :)

    You have to change it manually.

    You can use something like:

    $number_of_news = $this->Player->field("number_of_news", array("id" => $id));
    
    $this->Player->set("number_of_news" => $number_of_news - 1);
    
    点赞 评论 复制链接分享
  • douwen2072 douwen2072 2012-11-18 23:41
    1. Start the debug from core.php and see why it doesn't remove the entry it's possible to say something in the messages.

    2. Try with

      $this->FCPlayer->deleteAll(array('FCPlayer.name LIKE' => $name));

    3. Is it possible the name column to have extra spaces in the beginning or in the end?

    4. You can always use: $this->FCPlayer->query("your-query-goes-here");, but it's not recomended. :)

    点赞 评论 复制链接分享
  • douxian6086 douxian6086 2012-11-20 16:44

    Ok, finally I managed to do this. Correct answer was:

    $player = $this->Entry->field('player', array('id' => $id)); // yeah, single '

    $news = $this->FCPlayer->field('news', array('name' => $player));

    if ( $gossips > 1)
    {
    $this->FCPlayer->updateAll(array('FCPlayer.news'=>$news - 1),
    array('FCPlayer.name'=>$player));
    }
    else
    {
    $this->LFCPlayer->deleteAll(array('LFCPlayer.name LIKE' => $player));
    }

    点赞 评论 复制链接分享

相关推荐