这是我的MySQL数据库的数据库模式:
create table noticia
(
id int,
imagen varchar(255),
fecha datetime,
titulo varchar(255),
url varchar(255),
descripcion varchar(255),
contenido text
)
我使用RedBeanHP作为我的ORM来将信息保存到数据库中。这里是我根据文档将日期抓取并解析到DateTime
对象的地方。
foreach ($element->find('span.fechanoticia') as $fecha) {
$tmp = str_replace("/", "-", $fecha->innertext);
print_r($tmp);
$dateFoo = new DateTime($tmp);
echo $dateFoo->format('Y-m-d H:i:s');
$newItem->set_fechanoticia($dateFoo);
}
$tmp
变量此值例如:
05-09-2012
对格式化的echo
调用返回:
2012-09-05 00:00:00
一切都很好,而且很有效。
然而,当我尝试使用RedBeanHP将其保存到数据库时,我会收到以下错误:
致命错误:未捕获异常"RedBean_exception_Security",具有中的消息"无效Bean:属性fecha"C: ''examplep''htdocs''bloguvm''rb.php:4880堆栈跟踪:#0C: ''examplep''htdocs''bloguvm''rb.php(5108):RedBean_OODB->检查(对象(RedBean_ODBBean))#1C: ''examplep''htdocs''bloguvm''rb.php(5082):RedBean_OODB->storeBean(对象(RedBean_ODBBean))#2C: ''examplep''htdocs''bloguvm''rb.php(7005):RedBean_OODB->存储(对象(RedBean_ODBBean))#3C: ''examplep''htdocs''bloguvm''index.php(60):RedBean_Ffacade::store(Object(RedBean_OODDBBean))#4{main}抛出C: ''examplep''htdocs''bloguvm''rb.php,第4880行
RedBeanHP不能处理日期时间对象吗?
根据RedBean的文档(更多数据类型),只有当DateTime
对象是字符串形式时,才能保存它们。
例如:
// It's a string - not a DateTime.
$photo->created = '1995-12-05 19:00:00';
在这种情况下,解决方案应该是保存格式化的字符串,而不是DateTime
对象本身:
$newItem->set_fechanoticia($dateFoo->format('Y-m-d H:i:s'));
发现:http://groups.google.com/group/redbeanorm/browse_thread/thread/6961ac635e6886f6
The Optimizer will now convert columns with datetime values to datetimefields. If a different value is inserted the column will be reverted by OODB in fluid mode.