소스 검색

update documentation, use field type from the service container

Thomas Rabaix 14 년 전
부모
커밋
d42ae6fc69

+ 9 - 10
Builder/ORM/FormContractor.php

@@ -14,8 +14,6 @@ namespace Sonata\AdminBundle\Builder\ORM;
 use Sonata\AdminBundle\Admin\AdminInterface;
 use Sonata\AdminBundle\Admin\FieldDescriptionInterface;
 use Sonata\AdminBundle\Admin\ORM\FieldDescription;
-use Sonata\AdminBundle\Form\EditableCollectionField;
-use Sonata\AdminBundle\Form\EditableFieldGroup;
 use Sonata\AdminBundle\Model\ModelManagerInterface;
 use Sonata\AdminBundle\Builder\FormContractorInterface;
 use Sonata\AdminBundle\Form\Type\AdminType;
@@ -83,7 +81,7 @@ class FormContractor implements FormContractorInterface
         }
 
         // retrieve the related object
-        $childBuilder = $formBuilder->create($fieldName, 'sonata_model_admin', array(
+        $childBuilder = $formBuilder->create($fieldName, 'sonata_type_admin', array(
             'field_description' => $fieldDescription
         ));
 
@@ -134,10 +132,11 @@ class FormContractor implements FormContractorInterface
             return $this->defineChildFormBuilder($formBuilder, $fieldDescription);
         }
 
-        $type = new ModelType($fieldDescription->getAssociationAdmin()->getModelManager());
+        $type = 'sonata_type_model';
 
         $options = $fieldDescription->getOption('form_field_options', array());
-        $options['class'] = $fieldDescription->getTargetEntity();
+        $options['class']         = $fieldDescription->getTargetEntity();
+        $options['model_manager'] = $fieldDescription->getAdmin()->getModelManager();
 
         if ($fieldDescription->getOption('edit') == 'list') {
             $options['parent'] = 'text';
@@ -160,13 +159,13 @@ class FormContractor implements FormContractorInterface
 
             // create a collection type with the generated prototype
             $options = $fieldDescription->getOption('form_field_options', array());
-            $options['type'] = 'sonata_model_admin';
+            $options['type'] = 'sonata_type_admin';
             $options['modifiable'] = true;
             $options['type_options'] = array(
                 'field_description' => $fieldDescription,
             );
 
-            $formBuilder->add($fieldDescription->getFieldName(), 'sonata_admin_collection', $options);
+            $formBuilder->add($fieldDescription->getFieldName(), 'sonata_type_collection', $options);
 
             return;
 //            $value = $fieldDescription->getValue($formBuilder->getData());
@@ -211,8 +210,8 @@ class FormContractor implements FormContractorInterface
     /**
      * Add a new field type into the provided FormBuilder
      *
-     * @param \Symfony\Component\Form\FormBuilder $form
-     * @param \Sonata\AdminBundle\Admin\FieldDescriptionInterface $name
+     * @param \Symfony\Component\Form\FormBuilder $formBuilder
+     * @param \Sonata\AdminBundle\Admin\FieldDescriptionInterface $fieldDescription
      * @return void
      */
     public function addField(FormBuilder $formBuilder, FieldDescriptionInterface $fieldDescription)
@@ -243,7 +242,7 @@ class FormContractor implements FormContractorInterface
     /**
      * The method defines the correct default settings for the provided FieldDescription
      *
-     * @param \Sonata\AdminBundle\Admin\AdminInterface
+     * @param \Sonata\AdminBundle\Admin\AdminInterface $admin
      * @param \Sonata\AdminBundle\Admin\FieldDescriptionInterface $fieldDescription
      * @param array $options
      * @return void

+ 1 - 1
Form/EventListener/MergeCollectionListener.php

@@ -35,7 +35,7 @@ class MergeCollectionListener implements EventSubscriberInterface
     public function onBindNormData(FilterDataEvent $event)
     {
         $collection = $event->getForm()->getData();
-        $data = $event->getData();
+        $data       = $event->getData();
 
         if (!$collection) {
             $collection = $data;

+ 9 - 5
Form/FormMapper.php

@@ -122,23 +122,27 @@ class FormMapper
      * @param array $fieldDescriptionOptions
      * @return \Symfony\Component\Form\FormInterface
      */
-    public function addType($name, $type, array $options = array(), array $fieldDescriptionOptions = array())
+    public function addType($name, $type = null, array $options = array(), array $fieldDescriptionOptions = array())
     {
-        if (!isset($fieldDescriptionOptions['type'])) {
+        if (!isset($fieldDescriptionOptions['type']) && is_string($type)) {
             $fieldDescriptionOptions['type'] = $type;
         }
 
         $fieldDescription = $this->admin->getModelManager()->getNewFieldDescriptionInstance(
             $this->admin->getClass(),
-            $name,
+            $name instanceof FormBuilder ? $name->getName() : $name,
             $fieldDescriptionOptions
         );
 
         $this->formContractor->fixFieldDescription($this->admin, $fieldDescription, $fieldDescriptionOptions);
 
-        $this->admin->addFormFieldDescription($name, $fieldDescription);
+        $this->admin->addFormFieldDescription($name instanceof FormBuilder ? $name->getName() : $name, $fieldDescription);
 
-        $this->formBuilder->add($name, $type, $options);
+        if ($name instanceof FormBuilder) {
+            $this->formBuilder->add($name);
+        } else {
+            $this->formBuilder->add($name, $type, $options);
+        }
 
         return $this;
     }

+ 4 - 4
Form/Type/AdminType.php

@@ -66,8 +66,8 @@ class AdminType extends AbstractType
         return $this->getFieldDescription($options)->getAssociationAdmin();
     }
 
-      public function getName()
-      {
-          return 'sonata_model_admin';
-      }
+    public function getName()
+    {
+        return 'sonata_model_admin';
+    }
 }

+ 11 - 11
Form/Type/ModelType.php

@@ -28,25 +28,25 @@ class ModelType extends AbstractType
     {
         if ($options['multiple']) {
             $builder
-                ->addEventSubscriber(new MergeCollectionListener($this->modelManager))
+                ->addEventSubscriber(new MergeCollectionListener($options['model_manager']))
                 ->prependClientTransformer(new ModelsToArrayTransformer($options['choice_list']));
         } else {
-            $builder->prependClientTransformer(new ModelToIdTransformer($this->modelManager, $options['class']));
+            $builder->prependClientTransformer(new ModelToIdTransformer($options['model_manager'], $options['class']));
         }
     }
 
     public function getDefaultOptions(array $options)
     {
         $defaultOptions = array(
-            'template'      => 'choice',
-            'multiple'      => false,
-            'expanded'      => false,
-            'model_manager' => null,
-            'class'         => null,
-            'property'      => null,
-            'query'         => null,
-            'choices'       => array(),
-            'parent'        => 'choice',
+            'template'          => 'choice',
+            'multiple'          => false,
+            'expanded'          => false,
+            'model_manager'     => null,
+            'class'             => null,
+            'property'          => null,
+            'query'             => null,
+            'choices'           => array(),
+            'parent'            => 'choice',
             'preferred_choices' => array(),
             'field_description' => false,
         );

+ 3 - 3
Resources/config/form_types.xml

@@ -6,15 +6,15 @@
 
     <services>
         <service id="sonata.admin.form.type.admin" class="Sonata\AdminBundle\Form\Type\AdminType">
-            <tag name="form.type" alias="sonata_model_admin" />
+            <tag name="form.type" alias="sonata_type_admin" />
         </service>
 
         <service id="sonata.admin.form.type.collection" class="Sonata\AdminBundle\Form\Type\CollectionType">
-            <tag name="form.type" alias="sonata_admin_collection" />
+            <tag name="form.type" alias="sonata_type_collection" />
         </service>
 
         <service id="sonata.admin.form.type.model" class="Sonata\AdminBundle\Form\Type\ModelType">
-            <tag name="form.type" alias="sonata_admin_model" />
+            <tag name="form.type" alias="sonata_type_model" />
         </service>
 
     </services>

+ 55 - 0
Resources/doc/doctrine_orm/troubleshootings.rst

@@ -0,0 +1,55 @@
+Troubleshooting
+===============
+
+Deleted elements from a one-to-many association are not removed!
+----------------------------------------------------------------
+
+Make sure the Orphan Removal option is set to ``true``
+
+.. code-block:: xml
+
+    <?xml version="1.0" encoding="utf-8"?>
+    <doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping" xsi="http://www.w3.org/2001/XMLSchema-instance" schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping http://doctrine-project.org/schemas/orm/doctrine-mapping.xsd">
+        <entity name="Application\Sonata\MediaBundle\Entity\Gallery" table="media__gallery" >
+
+            <one-to-many
+                field="galleryHasMedias"
+                target-entity="Application\Sonata\MediaBundle\Entity\GalleryHasMedia"
+                mapped-by="gallery"
+                >
+
+                <orphan-removal>true</orphan-removal>
+
+            </one-to-many>
+
+            <!-- other definitions -->
+        </entity>
+    </doctrine-mapping>
+
+
+Ordered fields are not ordered!
+-------------------------------
+
+Make sure the ``order-by`` option is correctly set.
+
+.. code-block:: xml
+
+    <?xml version="1.0" encoding="utf-8"?>
+    <doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping" xsi="http://www.w3.org/2001/XMLSchema-instance" schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping http://doctrine-project.org/schemas/orm/doctrine-mapping.xsd">
+        <entity name="Application\Sonata\MediaBundle\Entity\Gallery" table="media__gallery" >
+
+            <one-to-many
+                field="galleryHasMedias"
+                target-entity="Application\Sonata\MediaBundle\Entity\GalleryHasMedia"
+                mapped-by="gallery"
+                >
+                <order-by>
+                    <order-by-field name="position" direction="ASC" />
+                </order-by>
+
+            </one-to-many>
+
+            <!-- other definitions -->
+
+        </entity>
+    </doctrine-mapping>

+ 1 - 0
Resources/doc/index.rst

@@ -33,6 +33,7 @@ Doctrine ORM
    :numbered:
 
    doctrine_orm/query_proxy
+   doctrine_orm/troubleshootings
 
 
 Tutorial

+ 3 - 3
Resources/doc/reference/form_types_and_transformers.rst

@@ -7,12 +7,12 @@ to handle the diffent model's workflows and lifecycle.
 Form types
 ----------
 
-    - ``sonata_model_admin`` : this type is linked to an Admin class and the field construction is
+    - ``sonata_type_admin`` : this type is linked to an Admin class and the field construction is
       delegated to an Admin class.
-    - ``sonata_admin_collection`` : this type works like the native ``CollectionType`` but contains two extra
+    - ``sonata_type_collection`` : this type works like the native ``CollectionType`` but contains two extra
       features : the data layer is abstracted to work with any implemented layer and a delete option is added
       so a collection entry can be deleted.
-    - ``sonata_admin_model`` : this type works like the native ``EntityType`` but this internal is abstracted
+    - ``sonata_type_model`` : this type works like the native ``EntityType`` but this internal is abstracted
       to work with any implemented layer.