Symfony 2 + Doctrine 2 - DateTime字段不存在

时间:2022-10-22 18:41:02

I have an entity of type Blah with the following fields:

我有一个Blah类型的实体,包含以下字段:

/**
 * @var datetime $begin
 *
 * @ORM\Column(name="begin", type="datetime", nullable=true)
 */
private $begin;

/**
 * @var datetime $end
 *
 * @ORM\Column(name="end", type="datetime", nullable=true)
 */
private $end;

With the following getters/setters:

使用以下getter / setter:

/**
 * @param $begin
 */
public function setBegin($begin)
{
    $this->begin = $begin;
}

/**
 * @return \DateTime
 */
public function getBegin()
{
    return $this->begin;
}

/**
 * @param $end
 */
public function setEnd($end)
{
    if ($end < $this->begin) {
        $this->end = $this->begin->add(\DateInterval::createFromDateString('1 day'));
    } else {
        $this->end = $end;
    }
}

/**
 * @return \DateTime
 */
public function getEnd()
{
    return $this->end;
}

And a corresponding form type that has:

以及相应的表单类型:

    $builder->add('begin', 'datetime', array(
            'label' => 'Begin',
            'required' => false,
    ));

    $builder->add('end', 'datetime', array(
            'label' => 'End',
            'required' => false,
    ));

And the form handling portion of the method in question:

并且该方法的表单处理部分:

    $form = $this->createForm(new BlahType(), $blah);

    if ($request->getMethod() == 'POST') {
        $postData = $request->request->all();

        if (!empty($postData['save'])) {
            $form->handleRequest($request);

            $numThings = $postData['blah']['numThings'];

            if ($numThings) {
                for ($i = 0; $i < $numThings; ++$i) {
                    $thing = new Thing();

                    $blah->addThing($thing);
                }
            }

            $em->persist($blah);
            $em->flush();
        }
    }

When I attempt to save these entries, null gets passed back to the entity despite the fact that the populated entity contains two arrays for each entry

当我尝试保存这些条目时,尽管填充的实体包含每个条目的两个数组,但null仍会传递回实体

Screen shot of what's passed back to the entity:

传回实体的屏幕截图:

Symfony 2 + Doctrine 2  -  DateTime字段不存在

Screen shot of what the populated entity supposedly has after $form->handleRequest():

屏幕截图显示填充的实体在$ form-> handleRequest()之后应该具有的内容:

Symfony 2 + Doctrine 2  -  DateTime字段不存在

Needless to say, I'm confused. According to the docs, a datetime field should default to having an input of, well, datetime, yet I'm getting the data back as a series of two arrays per field - one array with the day, month, and year, and the other with the hour and minutes. And even with that, null is what's being passed back to the entity and persisted in the db.

不用说,我很困惑。根据文档,datetime字段应该默认输入,嗯,日期时间,但我将数据作为每个字段的一系列两个数组返回 - 一个数组包含日,月和年,以及其他时间和分钟。即便如此,null也会被传递回实体并保存在db中。

It's baffling. Any suggestions/answers?

这令人费解。有什么建议/答案吗?

EDIT: The form itself has nulls in its viewData:

编辑:表单本身的viewData中有空值:

Symfony 2 + Doctrine 2  -  DateTime字段不存在

1 个解决方案

#1


0  

Figured it out. Problem was due to PHP inexplicably only recognizing Continent/City timezones instead of the old Country/Timezone timezones. US/Eastern is no longer valid.

弄清楚了。问题是由于PHP莫名其妙地只识别大陆/城市时区而不是旧的国家/时区时区。美国/东方不再有效。

#1


0  

Figured it out. Problem was due to PHP inexplicably only recognizing Continent/City timezones instead of the old Country/Timezone timezones. US/Eastern is no longer valid.

弄清楚了。问题是由于PHP莫名其妙地只识别大陆/城市时区而不是旧的国家/时区时区。美国/东方不再有效。