getting message "locked is required" back when saving model

Hi,

I am just starting with Phalcon. I have created a simple model called "Reminders" which is mapped to a MySQL table reminders. A reminder has quite a few parameters including id, accout, type, and much more.

I am trying to build a simple REST API to add reminders to my table.

For adding a reminder I am using this route:

$app->post('/api/reminders/add/{type}', function($type) use ($app) {
    $reminder       = new Reminders();
    $reminder->type = $type;
    $reminder->account = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaab';
    $reminder->id = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaannn';
    $reminder->recurrence_type = '1';

    if ($reminder->save() == false) {
        echo "Umh, We can't store reminders right now: \n";
        foreach ($reminder->getMessages() as $message) {
            echo $message, ".\n";
        }
    } else {
        echo "Great, a new reminder was saved successfully!";
    }
});

Unfortunately when I am doing a POST http://192.168.41.104:55000//api/reminders/add/2 The reminder is not added to the DB and I am getting:

Umh, We can't store reminders right now: locked is required.

Can anyone tell me what is "locked is required." ? how can I resolve this issue?

For info Reminders.php, looks like this:

<?php

use Phalcon\Mvc\Model,
    Phalcon\Mvc\Model\Message,
    Phalcon\Mvc\Model\Validator\InclusionIn,
    Phalcon\Mvc\Model\Validator\Uniqueness;

class Reminders extends Model
{

    public $id;
    public $account;
    public $type;   
    public $activated;
    public $deleted;
    public $to;
    public $from;
    public $replyto;
    public $title;
    public $subject;
    public $body;
    public $cc;
    public $bcc;        

    public function initialize()
    {
        $this->setSource("reminders"); // set correponding MySQL table 
    }
}

?>
edited May '14

Looking at the DB structure on MySQL, there is indeed a filed called "Locked" which needs to be set. So I added this in the parameters and this solved the issue.

Another question: Some fileds in the DB have default values, but when I use the above code there are set to NULL in the DB instead to these default values. I thought the code above will simply at the end execute an INSERT on the DB (which noramlly sets the default values). Am I doing something wrong?



169
edited May '14

There are 2 ways you can solve your second issue.

First method: Tell Phalcon to use the default database value in your model by adding a new function within your model.

public function beforeValidationOnCreate()
{
    $this->my_field = new RawValue('default');
    // do this for each field that has a default value
}

Or the second method: Tell Phalcon to ignore these fields when doing an insert by adding some more lines to your initialize.

public function initialize()
{
        $this->setSource("reminders"); // set correponding MySQL table 
        $this->skipAttributesOnCreate(array('my_field', 'my_field2', 'my_field3'));
}

Referenced from: http://stackoverflow.com/questions/15951959/how-to-make-models-use-defaults-in-phalcon-php-framework

Thanks for the tip.

On 26 May 2014 17:29, izayoi <[email protected]> wrote: