Arrays CakePHP3:newEntity()为空日期
我试图将CakePHP3中的一个实体保存在蛋糕壳中 这是我正在使用的代码,添加了一些调试:Arrays CakePHP3:newEntity()为空日期,arrays,cakephp,cakephp-3.0,Arrays,Cakephp,Cakephp 3.0,我试图将CakePHP3中的一个实体保存在蛋糕壳中 这是我正在使用的代码,添加了一些调试: $this->out("PRODUCT: " . var_dump($product) ); $newProduct = $this->VendorProducts->newEntity($product); $this->out("NEWPRODUCT: " . var_dump($newProduct) ); if($this->VendorProducts->
$this->out("PRODUCT: " . var_dump($product) );
$newProduct = $this->VendorProducts->newEntity($product);
$this->out("NEWPRODUCT: " . var_dump($newProduct) );
if($this->VendorProducts->save($newProduct)) {
$this->out("SAVED");
} else {
$this->out("ERROR: " . print_r($newProduct->errors(), TRUE));
}
这将为产品提供以下输出:
// Product:
array(5) {
["vendor_id"]=> int(1)
["vendor_sku"]=> string(15) "NW-ME-WL-MW150R"
["vendor_description"]=> string(38) "MERCUSYS 150Mbps WL-N BROADBAND ROUTER"
["vendor_price"]=> string(5) "11.00"
["date_fetched"]=> string(19) "2015-09-24 18:32:14"
}
在这里,您可以看到有一个“date\u fetched”属性,但在传递给newEntity()后,该值变为null。下面是调用newEntity()函数后$newProduct的输出
// New Product:
object(App\Model\Entity\VendorProduct)#57 (12) {
["vendor_id"]=> int(1)
["vendor_sku"]=> string(15) "NW-ME-WL-MW150R"
["vendor_description"]=> string(38) "MERCUSYS 150Mbps WL-N BROADBAND ROUTER"
["vendor_price"]=> float(11)
["date_fetched"]=> NULL
["[new]"]=> bool(true)
["[accessible]"]=> array(1) {
["*"]=>
bool(true)
}
["[dirty]"]=> array(5) {
["vendor_id"]=> bool(true)
["vendor_sku"]=> bool(true)
["vendor_description"]=> bool(true)
["vendor_price"]=> bool(true)
["date_fetched"]=> bool(true)
}
["[original]"]=> array(0) {
}
["[virtual]"]=> array(0) {
}
["[errors]"]=> array(0) {
}
["[repository]"]=> string(14) "VendorProducts"
}
是什么导致了这种情况的发生?基本上,正如我在上面所评论的,我也有同样的问题,我需要把下面这句话放在下面
$this->request->data['appointment'] = Time::parse($this->request->data['appointment']);
在此代码中:
if ($this->request->is(['patch', 'post', 'put'])) {
$this->request->data['appointment'] = Time::parse($this->request->data['appointment']);
$appointment = $this->Appointments->patchEntity($appointment, $this->request->data);
if ($this->Appointments->save($appointment)) {
$this->Flash->success(__('The appointment has been saved.'), ['plugin' => 'CakeBootstrap']);
return $this->redirect(['action' => 'index']);
} else {
$this->Flash->error(__('The appointment could not be saved. Please, try again.'));
}
}
我真的不想每次保存datetime字段时都解析日期。我真的希望它能自动化。不知道是否有解决办法,或者是我们做错了什么。我发现了同样的问题。这是一个适合我的解决方案 我在实体修补后更新了日期列。例如,在您的案例中:
$newProduct = $this->VendorProducts->newEntity($product);
修补程序覆盖后,请再次像这样覆盖数组日期列
$newProduct->date_fetched = date('Y-m-d H:i:s'); // here will be what you wants to save
现在再次检查您的数据。希望它对您有用:)
可能是,可能是实体中的,可能是…列类型设置为“DATETIME”,这是一个新烘焙的模型,我没有任何变体或任何配置。。。现在我只是在创建实体之后,调用->保存($newProduct)之前手动设置$newProduct->date\u fetched,这似乎可以保存它。不过这很奇怪。嗯,我想你得做些调试。首先,检查
$columnType
和$converter
是什么,然后检查converter/type类的marshall()
方法中值更改为NULL
的位置。例如,如果type类是DateTimeType
,则可能会发生这种情况。可能数据被更改得更早,例如在侦听器中,谁知道呢……我得到了相同的结果,而不是来自Shell。你找到解决方案了吗?使用Time::parse()确实解决了这个问题,但你是对的,这有点不太对劲。我相信一定有更好的解决办法
$this->out("NEWPRODUCT: " . var_dump($newProduct) );