Ad

October Cms Extending Plugin With Event Model Is Not Instantiated Correctly

- 1 answer

I am trying to extend the october cms user plugin to listin to the register event and get the post data and write the user and newsletter subscribe to the newsletter table when saving it says "SQLSTATE[42S22]: Column not found: 1054 Unknown column 'updated_at' in 'field list' (SQL: insert into cg_newsletter (updated_at, created_at) values ". I am not sure why it is complaining about created or updated date I did not create them in the model and I am not trying to update those columns. Any help greatly appreciated.

model

<?php


namespace CG\Newsletter\Models;

use Model;

/**
 * Model
 */
class Newsletter extends Model {

    protected $primaryKey = 'id';

    /**
     *
     * @var string The database table used by the model.
     */
    public $table = 'cg_newsletter';


    public $belongsTwo = [
        'user' => ['rainlab/user/models/user']
    ];

    public $isSubscribed;


}

plugin.php

<?php


namespace CG\Newsletter;


use System\Classes\PluginBase;
use Log;
use Event;
use Rainlab\User\Components\Account as RainlabAccount;
//use Rainlab\User\models\user as RainlabUser;
use Request;
use Illuminate\Support\Facades\Input;
use Rainlab\user\Models\User as RainlabUser;
use Auth;
use CG\Newsletter\Models\Newsletter;


class Plugin extends PluginBase {


    public function registerSettings() {

        return [ 
                'settings' => [ 
                        'label' => 'Newsletter',
                        'description' => 'Newsletter subscription',
                        'category' => 'misc',
                        'icon' => 'icon-shopping-basket',
                        'class' => 'CG\Newsletter\Models\Settings',
                        'order' => 500,
                        'keywords' => 'newsletter settings',
                        'permissions' => [ 
                                'admin' 
                        ]     
                ] 
        ];
    }

    public function boot() 
    {

        Event::listen('eloquent.updating: RainLab\User\Models\User', 
function(){
                $data = post();

               if (array_key_exists('email', $data)) {  

                 Log::debug(__FUNCTION__,[$data['email']]);                     
                 $user = RainlabUser::findByEmail($data['email']);

                 Log::debug(__FUNCTION__,[$user->id]);  
                 $newsletter = new Newsletter();
                 $newsletter->belongsTwo = $user->id;
                 $newsletter->isSubscribed =1;
                 $newsletter->save();

           }
        });



    }   

}

migration file

<?php namespace CG\Newsletter\Updates;

use Schema;
use October\Rain\Database\Updates\Migration;

class BuilderTableCreateCgNewsletter extends Migration
{
    public function up()
    {
        Schema::create('cg_newsletter', function($table)
        {
            $table->engine = 'InnoDB';
            $table->increments('id');
            $table->integer('user_id')->unsigned()->index();
            $table->tinyInteger('isSubscribed');
            $table->foreign('user_id')
              ->references('id')
              ->on('users')
              ->onDelete('CASCADE'); 

        });
    }

    public function down()
    {
        Schema::dropIfExists('cg_signshop_menus');
    }
}
Ad

Answer

Adding this to my model fixed this error. I found the answer here https://octobercms.com/docs/database/mutators under date mutators

/**
 * Indicates if the model should be timestamped.
 *
 * @var bool
 */
public $timestamps = false;
Ad
source: stackoverflow.com
Ad