我使用SonataAdmin(和Symfony2)来管理我的实体.我在一个步骤和许多任务之间有一个多关系.由于一步可以包含许多任务,当我创建一个步骤时,我想要能够创建许多任务,我希望这些任务链接到此步骤.为此,我创建了所有正确的管理类(一个用于Task,一个用于Step).
这是我做的,导致我的问题.当我尝试创建一个步骤时,我可以创建任务,甚至重新排序,这是非常好的,并且都由SonataAdminBundle自动完成.当我点击保存时,所有内容都保存在数据库中,除了在数据库中,步骤的ID不会在任务的行中设置.因此,任务没有与步骤…相关联
这是我的Step的管理类:
<?PHP // src/Acme/DemoBundle/Admin/PostAdmin.PHP namespace IMA\ProcessManagementBundle\Admin; use Sonata\AdminBundle\Admin\Admin; use Sonata\AdminBundle\Datagrid\ListMapper; use Sonata\AdminBundle\Datagrid\DatagridMapper; use Sonata\AdminBundle\Form\FormMapper; class StepAdmin extends Admin { // Fields to be shown on create/edit forms protected function configureFormFields(FormMapper $formMapper) { $formMapper ->add('name','text',array('label' => 'Nom de l\'étape')) ->add('tasks','sonata_type_collection',array(),array( 'edit' => 'inline','inline' => 'table','sortable' => 'positionNumber' )) ->add('positionNumber','integer',array('label' => 'Position')) ; } // Fields to be shown on filter forms protected function configureDatagridFilters(DatagridMapper $datagridMapper) { $datagridMapper ->add('name') ; } // Fields to be shown on lists protected function configureListFields(ListMapper $listMapper) { $listMapper ->addIdentifier('name') ->add('slug') ; } }
这也是我的任务管理类:
<?PHP // src/Acme/DemoBundle/Admin/PostAdmin.PHP namespace IMA\ProcessManagementBundle\Admin; use Sonata\AdminBundle\Admin\Admin; use Sonata\AdminBundle\Datagrid\ListMapper; use Sonata\AdminBundle\Datagrid\DatagridMapper; use Sonata\AdminBundle\Form\FormMapper; class TaskAdmin extends Admin { // Fields to be shown on create/edit forms protected function configureFormFields(FormMapper $formMapper) { $formMapper ->add('name',array('label' => 'Tâche')) ->add('positionNumber',array('label' => 'Position')) ; } // Fields to be shown on filter forms protected function configureDatagridFilters(DatagridMapper $datagridMapper) { $datagridMapper ->add('name') ; } // Fields to be shown on lists protected function configureListFields(ListMapper $listMapper) { $listMapper ->addIdentifier('name') ->add('slug') ; } }
另外,这里是我的实体的描述
IMA\ProcessManagementBundle\Entity\Step: type: entity table: null fields: id: type: integer id: true generator: strategy: AUTO name: type: string length: 255 positionNumber: type: integer oneToMany: tasks: targetEntity: Task mappedBy: step cascade: ["persist","merge"] lifecycleCallbacks: { } IMA\ProcessManagementBundle\Entity\Task: type: entity table: null fields: id: type: integer id: true generator: strategy: AUTO name: type: string length: 255 positionNumber: type: integer manyToOne: step: targetEntity: Step inversedBy: tasks lifecycleCallbacks: { }
我想知道为什么步骤的id不在任务行中设置…
您需要手动将步骤附加到任务中,为此您需要在步骤admin类中使用prePersist和preUpdate方法…
原文链接:https://www.f2er.com/php/139739.html这样做的原因是SonataAdminBundle的开发人员说,这是理论的关注,要处理这个问题,而这个原则开发者说这是捆绑的责任.所以现在我们需要为自己做.
这将是你新的stepAdmin类:
<?PHP namespace IMA\ProcessManagementBundle\Admin; use Sonata\AdminBundle\Admin\Admin; use Sonata\AdminBundle\Datagrid\ListMapper; use Sonata\AdminBundle\Datagrid\DatagridMapper; use Sonata\AdminBundle\Form\FormMapper; class StepAdmin extends Admin { // Fields to be shown on create/edit forms protected function configureFormFields(FormMapper $formMapper) { $formMapper ->add('name',array('label' => 'Position')) ; } // Fields to be shown on filter forms protected function configureDatagridFilters(DatagridMapper $datagridMapper) { $datagridMapper ->add('name') ; } // Fields to be shown on lists protected function configureListFields(ListMapper $listMapper) { $listMapper ->addIdentifier('name') ->add('slug') ; } public function prePersist($object) { foreach ($object->getTasks() as $task) { $task->setStep($object); } } public function preUpdate($object) { foreach ($object->getTasks() as $task) { $task->setStep($object); } } }