Ad

October Cms "SQLSTATE[HY000]: General Error: 1364 Field Does Not Have A Default Value

- 1 answer

I'm trying to update a model but it is complaing the field does not have a default value, but I am setting one before saving it in the boot method of my plugin see code below where I have outlined the details,I am not sure why it thinks there is no value. Any help greatly appreciated

plugin.php

    public function boot()
{


//  RainlabUser::extend(function ($model) {
//      $model->hasOne['newsletter'] = [
//          'CG\Newsletter\Models\Newsletter', 'table' =>
//          'cg_newsletter', 'key' => 'user_id'
//      ];  
//    });

    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->isSubscribed = 1;
             $newsletter->user_id = $user->id;


             Log::debug(__FUNCTION__,[$newsletter]);
             $newsletter->save();

           }
    });



}

model

use Model;

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

    /**
    * Indicates if the model should be timestamped.
    *
    * @var bool
    */
    public $timestamps = false;

    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;


}

schema

<?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

After removing the line public $isSubscribed; from the model it worked and saved correctly.

Ad
source: stackoverflow.com
Ad