Pimcore:在对象上设置 DateTime Class 字段
Pimcore: Setting DateTime Class Fields on Objects
我正在编写一个导入程序,但在实际创建对象时遇到了困难。具体来说,我在使用 DateTime 字段时遇到了麻烦。我有一个名为 blogArticle
的 class,我正在使用它们,就像 Pimcore 演示数据将它们用于博客文章一样。
$newPost = new Object\BlogArticle();
$newPost->setCreationDate( time() );
$newPost->setPublished( true );
$newPost->setText( $text ); // text input field
$newPost->setTitle( $title ); // text input field
$newPost->setDate( $date ); // datetime input field
$newPost->setKey( \Pimcore\File::getValidFilename( $key ) );
$newPost->setParentId( $id );
$newPost->save();
我得到的确切错误是:
Whoops\Exception\ErrorException thrown with message "Call to a member function getTimestamp() on a non-object"
Stacktrace:
#0 Whoops\Exception\ErrorException in /.../pimcore/models/Object/ClassDefinition/Data/Datetime.php:73
除了该字段类型的值在数据库中的存储方式外,我在文档中找不到任何内容。关于如何适当地将值分配给每个字段类型的 class 个字段的文档几乎为零。
解决方案
感谢 Igor Benko 解决了这个问题!
$newPost_date = new DateTime( "2016-07-01 12:10:37" );
$newPost->setDate( $newPost_date );
您的系统似乎仍设置为使用 Zend_Date。仅当您全新安装 Pimcore 4 时才使用 DateTime,否则在更新后默认情况下会打开兼容层。兼容层使用 Zend_Date 代替。
在您的 system.php 中,您必须将标志 useZendDate 设置为 false 才能使用 DateTime class.
您需要将 DateTime class 的实例传递给 setter。像这样:
$date=new DateTime("2016-07-01 12:10:37");
$newPost->setDate($date);
编辑:在@GrafikMatthew 更新了他的问题后更新了答案。
我正在编写一个导入程序,但在实际创建对象时遇到了困难。具体来说,我在使用 DateTime 字段时遇到了麻烦。我有一个名为 blogArticle
的 class,我正在使用它们,就像 Pimcore 演示数据将它们用于博客文章一样。
$newPost = new Object\BlogArticle();
$newPost->setCreationDate( time() );
$newPost->setPublished( true );
$newPost->setText( $text ); // text input field
$newPost->setTitle( $title ); // text input field
$newPost->setDate( $date ); // datetime input field
$newPost->setKey( \Pimcore\File::getValidFilename( $key ) );
$newPost->setParentId( $id );
$newPost->save();
我得到的确切错误是:
Whoops\Exception\ErrorException thrown with message "Call to a member function getTimestamp() on a non-object"
Stacktrace:
#0 Whoops\Exception\ErrorException in /.../pimcore/models/Object/ClassDefinition/Data/Datetime.php:73
除了该字段类型的值在数据库中的存储方式外,我在文档中找不到任何内容。关于如何适当地将值分配给每个字段类型的 class 个字段的文档几乎为零。
解决方案
感谢 Igor Benko 解决了这个问题!
$newPost_date = new DateTime( "2016-07-01 12:10:37" );
$newPost->setDate( $newPost_date );
您的系统似乎仍设置为使用 Zend_Date。仅当您全新安装 Pimcore 4 时才使用 DateTime,否则在更新后默认情况下会打开兼容层。兼容层使用 Zend_Date 代替。
在您的 system.php 中,您必须将标志 useZendDate 设置为 false 才能使用 DateTime class.
您需要将 DateTime class 的实例传递给 setter。像这样:
$date=new DateTime("2016-07-01 12:10:37");
$newPost->setDate($date);
编辑:在@GrafikMatthew 更新了他的问题后更新了答案。