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

无法将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?

  • 写回答

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条)

报告相同问题?

悬赏问题

  • ¥15 急matlab编程仿真二阶震荡系统
  • ¥20 TEC-9的数据通路实验
  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃
  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?
  • ¥15 python爬取bilibili校园招聘网站
  • ¥30 求解达问题(有红包)
  • ¥15 请解包一个pak文件