Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Orm\Observer_UpdatedAt
  • Hi,
    I have a problem with a model involved in a many_to_many relation.
    [code]
    class Model_Fanuser extends \Orm\Model
    {
        protected static $_properties = array(
            'id',
            'first_name',
    ...
            'created_at',
            'updated_at',
     );

    protected static $_many_many = array(
            'teams' => array(
                'key_from'         => 'id',
                'key_through_from' => 'fanuser_id', // column 1 from the table in between, should match a posts.id
                'table_through'    => 'fanusers_teams', // both models plural without prefix in alphabetical order
                'key_through_to'   => 'team_id', // column 2 from the table in between, should match a users.id
                'model_to'         => 'Model_Team',
                'key_to'           => 'id',
                'cascade_save'     => false,
                'cascade_delete'   => false,
            ),

            'stickers' => array(
                'key_from'         => 'id',
                'key_through_from' => 'fanuser_id', // column 1 from the table in between, should match a posts.id           
    'table_through'    => 'fanusers_stickers', // both models plural without prefix in alphabetical order
                'key_through_to'   => 'sticker_id', // column 2 from the table in between, should match a users.id           
    'model_to'         => 'Model_Sticker',           
    'key_to'           => 'id',           
    'cascade_save'     => false,
                'cascade_delete'   => false,
            )
        );

    protected static $_observers = array(
            'Orm\Observer_CreatedAt' => array(
                'events' => array('before_insert'),
                'mysql_timestamp' => false,
            ),
            'Orm\Observer_UpdatedAt' => array(
                'events' => array('before_save'),
                'mysql_timestamp' => false,
            ),
        );
    [code]
    and I have this function :
    [code]
    public function addSticker($sticker_id, $mode='one')
    {
            $hasIt = 0;
            if( is_array($this->stickers) )
            {
                foreach( $this->stickers as $k => $sticker )
                {
                    if( $sticker->id == $sticker_id )
                    {
                        $hasIt++;
                    }
                }
            }

            // is a one-only-sticker
            if( $mode=="one" && $hasIt>0 )
            {
                // no sticker. he already have the sticker
            }
            else
            {
                $this->stickers[] = Model_Sticker::find($sticker_id);
                return $this->save(false);
            }
        }
    [code]
    the problem is, that when I give a sticker to a user, everything goes well in the DB, but the created_at and updated_at fields ARE NOT FILLED with the timestamp.

    Could someone explain me what I'm doing wrong ?

    Thanks
  • Fields of which table? The Users table?
  • No, the fields from fanusers_stickers  (the crosstable betweene fanusers and stickers).
  • HarroHarro
    Accepted Answer
    Observers run on the model they are defined in. if you define a many-many with a through table, there is no model for that table, so no observers to run.

    If you want this, you need to define a model for it, and split the relation into two many-one's between your two models and the new through-table model.
  • Ok, I understand now.

    In this case, I think that will be much
    easier to change the type of the column created_at OR updated_at, in
    mysql, to timestamp, with default value CURRENT_TIMESTAMP and with the
    feature On_update_current_timestamp_checked
  • I agree about using ON UPDATE CURRENT_TIMESTAMP on MySQL side. This way can fixate updates in data changed from MySQL-client directly. And of course minus one method from Observers for better performance.
  • How set up ON UPDATE CURRENT_TIMESTAMP when i use create_table method to create new tables? 

    'created_at' => array('type' => 'timestamp', 'default' => \DB::expr('CURRENT_TIMESTAMP')),

    This way set CURRENT_TIMESTAMP, only when entry creates.
  • \DB::query(<<<EOS
    CREATE TABLE IF NOT EXISTS `cms_blocks` (
    `id` int UNSIGNED NOT NULL AUTO_INCREMENT,
    `uniqid` varchar(125) NOT NULL,
    `content` text NOT NULL,
    `created_at` timestamp NULL DEFAULT NULL,
    `updated_at` timestamp NOT NULL ON UPDATE CURRENT_TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    PRIMARY KEY (`id`),
    UNIQUE `page_uniqid` (uniqid)
    ) ENGINE=`InnoDB` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
    EOS
    )->execute();Now only this code works fine for me...

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion