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;
}
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire