0

I have started using the Sonata Admin bundle and I was following the example on how to map an entity with the bundle to create an administrative interface.

I created an entity called Post and this is the configuration yml file:

Emiliano\PostsBundle\Entity\Post:
type: entity
table: null
repositoryClass: Emiliano\PostsBundle\Entity\PostRepository
id:
    id:
        type: integer
        id: true
        generator:
            strategy: AUTO
fields:
    title:
        type: string
        column: Title
        lenght: 100
    published:
        type: boolean
        column: Published
    publishingDate:
        type: datetime
        column: Publishing_Date
        nullable: TRUE
    lifecycleCallbacks: {  }

Then in my Admin class I have the configureFormFields method:

protected function configureFormFields(FormMapper $formMapper) {
    $formMapper->add('title', 'text')
       ->add('published', 'checkbox', array('required' => false))
       ->add('publishingDate', 'sonata_type_model_hidden');
}

I found the sonata_type_model_hidden on the sonata admin documentation. What I would like to achieve is to programmatically handle the publishing date (e.g. set the date only if the checkbox published is checked) hiding the implementation to the user.

Everything works fine for create, delete and read, when it comes to modify an entity I get this message in the stacktrace:

No entity manager defined for class DateTime
In sonata-project/doctrine-orm-admin-bundle/Sonata/DoctrineORMAdminBundle/Model/ModelManager.php at line 214

If I show the field everything works fine, I tried also to use:

->add('publishingDate', 'hidden');

without success.

What is exactly the problem here? Is it because Sonata Admin tries to fill a form with the entity values and for publishingDate there's a DateTime while in the form specification I wrote a sonata_type_model_hidden? If so, how can I circumvent this?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Ende Neu
  • 15,581
  • 5
  • 57
  • 68
  • So what is in line 214 in ModelManager? I'm guessing that you might be using DateTime instead of \DateTime. Pure speculation. – Cerad Jan 25 '14 at 19:59
  • `$em = $this->registry->getManagerForClass($class);` and I am using \DateTime. – Ende Neu Jan 25 '14 at 21:08
  • Then check your code because I'm pretty sure you have $class = 'DateTime' and there certainly would not be a $em for it. – Cerad Jan 26 '14 at 00:22
  • What I don't actually understand is whether this is a sonata admin bug or something I overlooked, I don't create the form, the bundle maps my entity creating an administrative interface and checks the `configureFormFields` that I wrote and nowhere I am passing a `DateTime` object. Anyway I checked the `$class` variable from my previous comment and it's `DateTime`, I tried to change it to `\DateTime` without success. – Ende Neu Jan 26 '14 at 11:48

1 Answers1

1

sonata_type_model_hidden isn't just hidden field genereator, according to documentation:

sonata_type_model_hidden will use an instance of ModelHiddenType to render hidden field. The value of hidden field is identifier of related entity.

If I understand your problem, You want to set publishing date only when field published == true

You could use entity preSave/preUpdate lifecycle callback for eaxmple

public function preSave() 
{
     /**
      * Check if item is published
      */
     if($this->getPublished()) {
         $this->setPublishingDate(new \DateTime());
     } else {
         $this->setPublishingDate(null);
     }
}

and remove publishingDate field from SonataAdmin form.

waldek_h
  • 930
  • 9
  • 16
  • I was already using the preSave/preUpdate hooks, I ended up removing the fields form the form since they were useless, still why this error happens intrigues me. – Ende Neu Jan 26 '14 at 21:10