我正在尝试更新模型,但它抱怨该字段没有默认值,但我在设置之前设置了一个我的插件的启动方法,请参阅下面的代码,我已经概述了详细信息,我我不确定为什么它认为没有价值。任何帮助非常感谢
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 :(得分:0)
删除行后公共$ isSubscribed;从模型中运作并正确保存。