0

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)?

1
  • which version of cake are you using? Commented Nov 18, 2012 at 19:18

3 Answers 3

1
  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. :)

Sign up to request clarification or add additional context in comments.

2 Comments

Ad 3. Names are just names, no spaces at all.
Ad 2. Can't try it since I didn't manage to get $name (see EDIT).
1

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

Comments

0

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

1 Comment

Thanks. That's why I was trying to do it step-by-step. I have an id, wanted to get name and deal with entries this way. In your method, how exactly Cake knows which player we are talking about? I miss "WHERE" option...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.