简体   繁体   English

Sonata Admin Bundle的简单约会

[英]Simple date with Sonata Admin Bundle

I have a form with Sonata Admin Bundle with a date, to set the birthday of the user we want to add. 我有一个带有日期的Sonata Admin Bundle表单,用于设置我们要添加的用户的生日。 Here goes MemberAdmin.php : 这里是MemberAdmin.php

/**
 * @param \Sonata\AdminBundle\Form\FormMapper $formMapper
 *
 * @return void
 */
protected function configureFormFields(FormMapper $formMapper)
{
    $formMapper
        ->with('General')
            ->add('username')
            ->add('name')
            ->add('surname')
            ->add('birthdate', 'birthday', array('format' => 'yyyy-MM-dd'))
            // ...
}

And my problem is when I send the form, I obtain Error: Call to a member function format() on a non-object ... But if I do print_r($birthdate) in the Entity class it shows me the DateTime object ... 我的问题是,当我发送表单时,出现Error: Call to a member function format() on a non-object ...但是,如果我在Entity类中执行print_r($birthdate) ,它将向我显示DateTime对象。 ..

Here are the interesting Entity parts: 以下是有趣的实体部分:

/**
 * @var date
 *
 * @ORM\Column(name="birthdate", type="date", nullable=true, options={"default" = "1990-01-01 00:00:00"})
 * @Assert\DateTime()
 */
private $birthdate;


/**
 * Set birthdate
 *
 * @param \DateTime $birthdate
 * @return Membre
 */
public function setBirthdate($birthdate)
{
    $this->birthdate = $birthdate;

    return $this;
}

/**
 * Get birthdate
 *
 * @return \DateTime 
 */
public function getBirthdate()
{
    return $this->birthdate;
}

My problem, currently, is that I don't know what I should do, I just want the date, no time, no anything else, i don't know if the column should be date (I work with PostgreSQL). 目前,我的问题是我不知道该怎么办,我只想要日期,没有时间,没有别的东西,我不知道列是否应该是日期(我使用PostgreSQL)。 What should I use for the types of my variables, I feel lost here, no simple Date possible ?? 我应该使用什么类型的变量,我在这里感到迷茫,没有简单的Date可能吗?

I tried to figure out from where it could come, but when I change too much I end up with: This form should not contain extra fields directly in the form, or even Incorrect value , but the field is a valid date ... 我试图弄清楚它可能从哪里来,但是当我进行过多更改时,我最终得到: This form should not contain extra fields直接在表单中This form should not contain extra fields ,甚至This form should not contain extra fields Incorrect value ,但该字段是有效日期...

Thanks for your help !! 谢谢你的帮助 !!

Change your field type to sonata_type_date_picker and test if the error message persist. 将字段类型更改为sonata_type_date_picker,然后测试错误消息是否仍然存在。

 ->add('birthdate', 'sonata_type_date_picker', array(
              'format' => 'dd/MM/yyyy',
              'widget' => 'single_text',
              'label' => 'Birthdate',

          ))

From manual ( sonata-project.org ) : 从手册( sonata-project.org ):

If no type is set, the Admin class will use the one set in the doctrine mapping definition. 如果未设置任何类型,则Admin类将使用原则映射定义中的一组。

So, you can try this: 因此,您可以尝试以下操作:

->add('birthdate', null, array('format' => 'yyyy-MM-dd'));

Edit 编辑

As @rande said, modifying vendors files is not the way to go, it provided an easy temp workaround for a local private app. 正如@rande所说,修改供应商文件不是要走的路,它为本地私有应用程序提供了一个简单的临时解决方法。 As it is not dedicated to stay like that, I took care of the issue once I had more time. 由于它不是专门致力于保持这种状态,因此,我有更多时间后就解决了这个问题。 Sorry for the delay to come back to you guys. 很抱歉拖延给大家。

I played around, tried with multiple setups, it took me time to figure it out, but I finally came to the conclusion that the issue... was caused by another date, that I was generating wrong in the constructor one line above. 我试玩了一下,尝试了多种设置,花了一些时间才弄清楚,但最终我得出的结论是,问题是由另一个日期引起的,在上面的构造函数中我产生了错误。

Also, thanks to all of you, that guided me on the right path! 另外,感谢大家,这引导了我正确的道路!

@wr0ng.name you should never overwrite vendor code. @ wr0ng.name绝不能覆盖供应商代码。 NEVER. 决不。

There is something wrong with your mapping somewhere. 您在某处的映射存在问题。 You can use doctrine's commands to check your entity. 您可以使用教义的命令来检查您的实体。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM