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-uvm\rb.php:4880 Stack trace: #0 C:\xampp\htdocs\blog-uvm\rb.php(5108): RedBean_OODB->check(Object(RedBean_OODBBean)) #1 C:\xampp\htdocs\blog-uvm\rb.php(5082): RedBean_OODB->storeBean(Object(RedBean_OODBBean)) #2 C:\xampp\htdocs\blog-uvm\rb.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-uvm\rb.php on line 4880
Can RedBeanPHP not handle datetime objects?
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'));