我正在通过实体方法将数据保存到我的数据库,而且我不确定我错过了什么。
我有一个方法可以为我提供一个虚拟字段,显示要约剩余的时间。这适用于显示值。我想对此进行扩展,以便当剩余时间为0时,staus字段从1更改为4以表示它已过期。
这是我到目前为止看起来似乎有效,但状态值似乎在设置后没有保存。
此功能用于显示剩余时间
protected function _getRemaining(){
if($this->status === 1 || $this->status === 'Live'){
// Get various timestamps
$now = strtotime(Time::now());
$created = strtotime($this->created);
$duration = $this->duration;
// Get the time remaining
$remaining = (($created + $duration) - $now);
// If below 0 set the value to 0
if($remaining < 0){
$remaining = 0;
$this->_setStatusAuto(4);
}
} else {
$remaining = 0;
}
return $remaining;
}
这是我尝试更改状态值的位置。它返回正确的值,但数据库值没有改变!
protected function _setStatusAuto($level){
$this->set('status', $level);
return $this->status;
}
任何朝着正确方向的推动都会很棒,谢谢。
答案 0 :(得分:0)
$this->set('status', $level);
用于将数据从控制器发送到视图,而不是模型/数据库。您将要使用此处描述的方法: http://book.cakephp.org/3.0/en/orm/saving-data.html
答案 1 :(得分:0)
首先创建模型的对象,然后再创建属性
<强> $模型 - &GT;保存(); 强>