We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Change a databasefield on function call

I tried to change a database field everytime a function is called. But it doesn't work. Any ideas or is this complete wrong.

public function changetaskAction($taskitem_id) {

  $task = Tasks::find(array(
    "taskitem_id = $taskitem_id"
    ));

  if (count($task) == 0) {
      $this->flash->error("Task does not exist");
      return $this->forward("tasks/index");
    }

    if ($task->ready="0") {
        $ready = "0";
    } else {
        $ready = "1";
    }
    $task->ready = $ready;
    if ($task->update() == false) {
        foreach ($task->getMessages() as $message) {
            $this->flash->error($message);
        }
      } else {
        $this->flash->success("Task was updated successfully");
        return $this->forward("tasks/index");
    }
  }


7.0k
edited Jul '15

Hmm, maybe you should try if($task->ready == "0") instead of if($task->ready = "0")

PS: You should protect your Database-activity against SQL Injection in line 4. ("taskitem_id = $taskitem_id")



2.2k

I think there is someting else wrong because the log say's "could not be converted to string". Thank you for pointing me to line 4!



7.0k

Which log do you exatcly mean?



2.2k

The apache log and is referring to line 12: if ($task->ready="0")



7.0k

Try change this entire line to the following:

if($task->ready == 0)

Now you check the state of $task->ready and doesn't set it to "0". That should be the error.