2017-10-10 91 views
0

我試圖更新模型,但它抱怨該字段沒有默認值,但我保存之前設置一個它在我的插件的引導方法見下文,我所概述的細節代碼,我不知道爲什麼它認爲沒有價值。任何幫助非常感謝十月cms「SQLSTATE [HY000]:一般錯誤:1364字段沒有默認值

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(); 

      } 
    }); 



} 

模型

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; 


} 

模式

<?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'); 
    } 
} 

回答

0

刪除行public isSubscribed;從它的模型工作並保存正確。

相關問題