marcosguuis
marcosguuis

Reputation: 35

Add own field to Admin Sonata

I would like make log system for many Entity. For example i have Entity: Blog, Page and News. They are admin class:

class (Blog|Page|News/)Admin.php
{
    protected function configureFormFields(FormMapper $formMapper)
    {
        $formMapper
            ->with('General')
                ->add('title', null, array())
                ->add('body', null, array())
            ->end();
    }
}

I would like also Entity Log. This has fields: log, description and date. I would like add field description to Blog, Page and News:

class (Blog|Page|News)Admin.php
{
    protected function configureFormFields(FormMapper $formMapper)
    {
        $formMapper
            ->with('General')
                ->add('title', null, array())
                ->add('body', null, array())
                ->add('description', 'text', array())
            ->end();
    }
}

but this return error - this field not exist in Entity (Blog|Page|News). I can add setDescription, getDescription etc to Entity, but this is wrong way if i have a lot of entity to logging and i will have all logs in one table (Entity Log), not in all Entities - (Blog|Page|News). I use preUpdate to save this data to table Log, but i dont know how i can add field description from Entity Log to these Entities. I can also make relation but this is also wrong way. Maybe i should use extends class? But how?

Upvotes: 3

Views: 5199

Answers (2)

sjt003
sjt003

Reputation: 2607

shouldn't optional fields have 'required' set to false

protected function configureFormFields(FormMapper $formMapper){
//
->add('title', 'text', array('mapped'=>false, 'required'=> false))
//
}

Upvotes: 0

M.B Kakadiya
M.B Kakadiya

Reputation: 576

You can add your field in sonata admin ..

It's right way to add optional filed in sonata admin form

     $formMapper
        ->with('General')
            ->add('title', null, array('mapped'=>false))
            ->add('body', null, array())
            ->add('description', 'text', array())
        ->end();

Add 'mapped'=>false in third argument

Upvotes: 5

Related Questions