如何解决PHP 7 和 Laravel 8 使用私有变量序列化模型
我正在将一个项目从 PHP 5.4、Laravel 4.2 升级到 PHP 7.4、Laravel 8。
使用 PHP 5.4 和 Laravel 4.2,我会serialize()
模型与关系和私有变量
存储在redis中。当用户完成他们的更改后,我们存储到 DB 并从 redis 中删除。
在 PHP 5.4 和 Laravel 4.2 中,使用 serialize()
效果很好。
在 PHP 7.4 和 Laravel 8 中,使用 serialize()
不会在我的模型中包含私有变量
序列化时。我需要将我的模型与加载的所有关系和私有变量一起存储在 redis 中
当用户与模型交互时。
如何让 PHP 7.4 和 Laravel 8 序列化我的模型,包括私有变量?
class MyParent extends Model
{
private $privatevalueone = 1;
private $privatevaluetwo = 2;
public function childeh()
{ return $this->hasOne('ChildEh'); }
public function childbee()
{ return $this->hasMany('ChildBee'); }
public function childsea()
{ return $this->hasMany('ChildSea'); }
// there are a total of 11 relations
public function calc()
{
// - does calculations that can change model properties in relations
// and properties in the parent model.
// - stores values needed while the model is cached using private variables,// the private variables are only needed while cached and not needed when
// written to DB
}
}
/**
* With PHP 5.4,Laravel 4.2,using serialize preserved
* the entire model,including private variables in the model
*
* With PHP 7.4,Laravel 8,using serialize does not
* include the private variables in the model,*/
public function cacheSet($key,$model)
{
Redis::set($key,serialize($model));
}
public function cacheGet($key)
{
return unserialize(Redis::get($key));
}
解决方法
感谢@nice_dev 的回复。 我的解决方案是将以下特征添加到我的模型中,它执行您引用的操作:
\Illuminate\Queue\SerializesModels
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。