Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

Some before update operations are not performing

Hi,

In my application I have some fields that are handled by a BaseModel, extended by every Model in my application.

These fields are the dates when the record was created and last updated and the user id who last updated and created the record.

The only difference between the "created" and the "updated" fields is that the "updated" ones can be NULL and are NULL by default. This is obviously necessary because when the record is first created it has no date or user id updated.

Anyway, if I set the two "updated" fields to be NOT NULL, I obtain the same behaviour from the Model.

In the BaseModel I manage these fields using beforeValidationOnCreate() and beforeValidationOnUpdate(). Then I use afterFetch(), afterUpdate() and afterCreate() just to convert the dates between MySQL and human readable formats.

The problem is that DateCreated and UserIdCreated are handled perfectly by the application. The two "updated" fields are completely ignored. Where is my error?

Thank you.

This is my BaseModel:

<?php
namespace MyApp\Models;

use Phalcon\Mvc\Model;

class ModelBase extends Model {
    protected $HasUserIdUpdated = true;
    protected $HasDateCreated = true;
    protected $HasUserIdCreated = true;
    protected $HasDateUpdated = true;

    private $DateUpdated;
    private $UserIdUpdated;
    private $UserIdCreated;
    private $DateCreated;


/* getters and setters... */

    public function beforeValidationOnCreate() {
        if( $this->HasDateCreated ) {
            $this->setDateCreated( date('Y-m-d H:i:s') );
        }

        if( $this->HasUserIdCreated ) {
            $this->setUserIdCreated( 234 );
        }
    }


    public function beforeValidationOnUpdate() {
        if( $this->HasDateUpdated ) {
            $this->setDateUpdated( date('Y-m-d H:i:s') );
        }

        if( $this->HasUserIdUpdated ) {
            $this->setUserIdUpdated( 432 );
        }
    }

    public function afterCreate() {
        if( $this->HasDateCreated ) {
            $this->setDateCreated( date('d/m/Y H:i:s', strtotime($this->getDateCreated())) );
        }
    }


    public function afterUpdate() {
        if( $this->HasDateUpdated ) {
            $this->setDateUpdated( date('d/m/Y H:i:s', strtotime($this->getDateUpdated())) );
        }
    }


    public function afterFetch() {
        if( $this->HasDateCreated ) {
            $this->setDateCreated( date('d/m/Y H:i:s', strtotime($this->getDateCreated())) );
        }

        if( $this->HasDateUpdated ) {
            $this->setDateUpdated( date('d/m/Y H:i:s', strtotime($this->getDateUpdated())) );
        }
    }

}

This is my "Version" Model (table "version"):

<?php

namespace MyApp\Models;

use MyApp\Models\ModelBase;

class Version extends ModelBase {
    private $SectionName;
    private $SectionDescription;
    private $Sort;

/* getters and setters... */

    public function beforeValidationOnCreate() {
        parent::beforeValidationOnCreate();

        $last = self::findFirst(array(
            'order' => 'Sort DESC',
            'limit' => 1,
        ));

        if( $last === false ) {
            $this->setSort( 1 );
        } else {
            $this->setSort( $last->Sort + 1 );
        }
    }


    public function columnMap() {
        return array(
            'version_id' => 'Id',
            'section_name' => 'SectionName',
            'section_description' => 'SectionDescription',
            'sort' => 'Sort',

            'user_id_updated' => 'UserIdUpdated',
            'date_updated' => 'DateUpdated',
            'user_id_created' => 'UserIdCreated',
            'date_created' => 'DateCreated',
        );
    }
}

This is the database (table "version"):

+---------------------+------------------+------+-----+---------+----------------+
| Field               | Type             | Null | Key | Default | Extra          |
+---------------------+------------------+------+-----+---------+----------------+
| version_id          | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| section_name        | varchar(50)      | NO   | MUL | NULL    |                |
| section_description | varchar(255)     | NO   | MUL | NULL    |                |
| sort                | int(10) unsigned | NO   | MUL | NULL    |                |
| user_id_updated     | int(10) unsigned | YES  |     | NULL    |                |
| date_updated        | timestamp        | YES  |     | NULL    |                |
| user_id_created     | int(10) unsigned | NO   |     | NULL    |                |
| date_created        | timestamp        | NO   |     | NULL    |                |
+---------------------+------------------+------+-----+---------+----------------+


3.4k
Accepted
answer
edited Dec '15

Found the answer, it was so stupid actually...

The fields in the BaseModel were private, but I should give them protected visibility. Getters and Setters are all public of course, but the fields themselves must be protected in order to be used by the children Models.

Thank you.

Also its not phalcon problem :)