php Redbean O/RM 将“日期”存储为 varchar(255)?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3212917/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 09:01:39  来源:igfitidea点击:

Redbean O/RM store "date" as varchar(255)?

phpmysqlormredbean

提问by Sirber

From this code:

从这个代码:

$toolbox = RedBean_Setup::kickstartDev("mysql:*****************");

$r = $toolbox->getRedBean();

$test = $r->dispense("test");
$test->nom = 'Test #1';
$test->date = '2010-07-08';
$test->date_deux = '08/07/2010';
$test->num = 5;

$id = $r->store( $test ); 

I get this SQL:

我得到这个 SQL:

CREATE TABLE IF NOT EXISTS `test` (
  `id` int(11) unsigned NOT NULL auto_increment,
  `nom` varchar(255) collate utf8_unicode_ci default NULL,
  `date` varchar(255) collate utf8_unicode_ci default NULL,
  `num` tinyint(3) unsigned default NULL,
  `date_deux` varchar(255) collate utf8_unicode_ci default NULL,
  PRIMARY KEY  (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=6 ;

--
-- Dumping data for table `test`
--

INSERT INTO `test` (`id`, `nom`, `date`, `num`, `date_deux`) VALUES
(1, 'Test #1', '2010-07-08', NULL, NULL),
(2, 'Test #1', '2010-07-08', 5, NULL),
(3, 'Test #1', '2010-07-08', 5, '08/07/2010'),
(4, 'Test #1', '2010-07-08', 5, '08/07/2010'),
(5, 'Test #1', '2010-07-08', 5, '08/07/2010');

is there a special way to use datewith RedBean?

有没有一种特殊的方法可以date与 RedBean一起使用?

采纳答案by Sirber

found this: http://groups.google.com/group/redbeanorm/browse_thread/thread/6961ac635e6886f6

发现这个: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.

优化器现在会将具有日期时间值的列转换为日期时间字段。如果插入了不同的值,则 OODB 将在流体模式下恢复该列。

回答by josh

Looks like you need to be very specific in the format for a datetime column. For instance, the following code should provide a date/time column:

看起来您需要对日期时间列的格式非常具体。例如,以下代码应提供日期/时间列:

$mysqldate = date("Y-m-d", $your_date); // for date column
OR
$mysqldatetime = date("Y-m-d H:i:s", $your_date_time); // for datetime column

$mysqldate = date("Y-m-d", $your_date); // for date column
或者
$mysqldatetime = date("Y-m-d H:i:s", $your_date_time); // for datetime column

Specifically, "Y-m-d" for date, and "Y-m-d H:i:s" for datetime, are the formats RedBean is looking for.

具体来说,日期的“Ymd”和日期时间的“Ymd H:i:s”是 RedBean 正在寻找的格式。

回答by Gabor de Mooij

You can either use time() or change the column type after freezing the DB.

您可以使用 time() 或在冻结数据库后更改列类型。

回答by NicoGranelli

The Standard Optimizer handles DateTime columns. You can check it out here: http://redbeanphp.com/#/Plugins(almost at the end of the page)

标准优化器处理日期时间列。您可以在这里查看:http: //redbeanphp.com/#/Plugins(几乎在页面末尾)