dongming5444
2012-09-05 13:21
浏览 118
已采纳

无法将DateTime对象保存到MySQL中的datetime字段

Here is my database schema for my MySQL database:

create table noticia 
(
    id int,
    imagen varchar(255),
    fecha datetime,
    titulo varchar(255),
    url varchar(255),
    descripcion varchar(255),
    contenido text
) 

I'm using RedBeanPHP as my ORM to save the information to a database. Here is where I'm scraping and parsing the dates to a DateTime object as per the documentation.

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);
}

The $tmp variable this value for example:

05-09-2012

The echo call to format returns:

2012-09-05 00:00:00

Everything is peachy, and working.

However when I try to save it to the database using RedBeanPHP I get this error:

Fatal error: Uncaught exception 'RedBean_Exception_Security' with message 'Invalid Bean: property fecha ' in C:\xampp\htdocs\blog-uvmb.php:4880 Stack trace: #0 C:\xampp\htdocs\blog-uvmb.php(5108): RedBean_OODB->check(Object(RedBean_OODBBean)) #1 C:\xampp\htdocs\blog-uvmb.php(5082): RedBean_OODB->storeBean(Object(RedBean_OODBBean)) #2 C:\xampp\htdocs\blog-uvmb.php(7005): RedBean_OODB->store(Object(RedBean_OODBBean)) #3 C:\xampp\htdocs\blog-uvm\index.php(60): RedBean_Facade::store(Object(RedBean_OODBBean)) #4 {main} thrown in C:\xampp\htdocs\blog-uvmb.php on line 4880

Can RedBeanPHP not handle datetime objects?

图片转代码服务由CSDN问答提供 功能建议

这是我的MySQL数据库的数据库架构:

   create table noticia 
(
 id int,
 imagen varchar(255),
 fecha datetime,
 titulo varchar(255),
 url varchar(255),
 descripcion varchar(255),
  contenido text 
)
   
 
 

我使用RedBeanPHP作为我的ORM将信息保存到数据库。 这里是我根据 DateTime 对象的地方。 rel =“nofollow”>文档

  foreach($ element-> find('span.fechanoticia')as $ fecha){
 $ tmp  = str_replace(“/”,“ - ”,$ fecha-> innertext); 
 print_r($ tmp); 
 
 $ dateFoo = new DateTime($ tmp); 
 echo $ dateFoo-> format  ('Ymd H:i:s'); 
 
 $ newItem-> set_fechanoticia($ dateFoo); 
} 
   
 
 

$ tmp 变量此值例如:

  05-09-2012 
   
 
 

< code> echo 调用格式返回:

  2012-09-05 00:00:00 
   
 
  

一切都很好,并且正常工作。

但是当我尝试使用RedBeanPHP将其保存到数据库时,我收到此错误:

\ n

致命错误:在 C:\ xampp \ htdocs \ blog-uvm 中带有 消息'无效Bean:属性fecha'的未捕获异常'RedBean_Exception_Security' b.php:4880堆栈跟踪:#0 C:\ xampp \ htdocs \ blog-uvm b.php(5108): RedBean_OODB-&gt; check(对象(RedBean_OODBBean))#1 C:\ xampp \ htdocs \ blog-uvm b.php(5082): RedBean_OODB-&gt; storeBean(Object(RedBean_OODBBean))#2 C:\ xampp \ htdocs \ blog-uvm b.php(7005): RedBean_OODB-&gt; store(Object(RedBean_OODBBean))#3 C:\ xampp \ htdocs \ blog-uvm \ index.php(60): RedBean_Facade :: store(Object(RedBean_OODBBean))#4 { 在第4880行的 C:\ xampp \ htdocs \ blog-uvm b.php中抛出

RedBeanPHP可以不处理日期时间对象吗?

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dongya1875 2012-09-05 13:27
    已采纳

    As per RedBean's documentation (More data types), you can only save DateTime objects if they are in string form.

    For example:

    // It's a string - not a DateTime.
    $photo->created = '1995-12-05 19:00:00';
    

    In this case the solution should be to save the formatted string, not the DateTime object itself:

    $newItem->set_fechanoticia($dateFoo->format('Y-m-d H:i:s'));
    
    评论
    解决 无用
    打赏 举报
查看更多回答(1条)

相关推荐 更多相似问题