tre*_*ake 7 datetime symfony symfony4
所以我一直在关注这个数据库和Doctrine教程:https://symfony.com/doc/current/doctrine.html
唯一的区别是我添加了一个created_ts
字段(在其他一些字段中,但它们工作正常,所以不需要进入它们).
我使用该make:entity
命令生成我的类,并设置我created_ts
生成的方法如下:
public function setCreatedTs(\DateTimeInterface $created_ts): self
{
$this->created_ts = $created_ts;
return $this;
}
Run Code Online (Sandbox Code Playgroud)
所以在我的/index
页面中,我使用以下命令保存新实体:
$category->setCreatedTs(\DateTimeInterface::class, $date);
Run Code Online (Sandbox Code Playgroud)
我有一种有趣的感觉,这会出错,我是对的:
Type error: Argument 1 passed to App\Entity\Category::setCreatedTs() must implement interface DateTimeInterface, string given
Run Code Online (Sandbox Code Playgroud)
但我不知道如何实现DateTimeInterface
内部功能..我尝试谷歌搜索但它显示了很多Symfony2
帖子,一些我试图无济于事.
如何datetime
在->set
方法中设置实体中的值?
(如果已经有答案,请链接.#symfonyScrub)
# tried doing this:
$dateImmutable = \DateTime::createFromFormat('Y-m-d H:i:s', strtotime('now')); # also tried using \DateTimeImmutable
$category->setCategoryName('PHP');
$category->setCategoryBio('This is a category for PHP');
$category->setApproved(1);
$category->setGuruId(1);
$category->setCreatedTs($dateImmutable); # changes error from about a string to bool
Run Code Online (Sandbox Code Playgroud)
fxb*_*xbt 16
如果您的日期是当前日期,您可以这样做:
$category->setCreatedTs(new \DateTime())
Run Code Online (Sandbox Code Playgroud)
您的第一个错误是由strtotime
返回时间戳但是\ DateTime构造函数期望Y-m-d H:i:s
格式的函数引起的.
这就是为什么不是创建有效的\ DateTime,而是返回false.
即使在这种情况下没有必要,你应该做这样的事情来创建一个\DateTime
基于时间戳的东西:
$date = new \DateTime('@'.strtotime('now'));
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
11742 次 |
最近记录: |