Przeglądaj źródła

add create link on one to one association, tweak code

Thomas 14 lat temu
rodzic
commit
b6df5460ed

+ 51 - 0
Admin/Admin.php

@@ -203,6 +203,13 @@ abstract class Admin extends ContainerAware
 
     }
 
+    public function getNewInstance()
+    {
+        $class = $this->getClass();
+
+        return new $class;
+    }
+
     /**
      * return the target objet
      *
@@ -235,6 +242,26 @@ abstract class Admin extends ContainerAware
         }
     }
 
+    public function preUpdate($object)
+    {
+
+    }
+
+    public function postUpdate($object)
+    {
+
+    }
+
+    public function preInsert($object)
+    {
+
+    }
+
+    public function postInsert($object)
+    {
+        
+    }
+
     /**
      * build the fields to use in the form
      *
@@ -251,6 +278,11 @@ abstract class Admin extends ContainerAware
                 throw new \RuntimeException(sprintf('You must declare a type for the field `%s`', $name));
             }
 
+            // make sure the options field is set
+            if(!isset($this->form_fields[$name]['fieldName'])) {
+                $this->form_fields[$name]['fieldName'] = $name;
+            }
+
             // make sure the options field is set
             if(!isset($this->form_fields[$name]['options'])) {
                 $this->form_fields[$name]['options'] = array();
@@ -263,6 +295,9 @@ abstract class Admin extends ContainerAware
                 if($this->form_fields[$name]['type'] == \Doctrine\ORM\Mapping\ClassMetadataInfo::ONE_TO_ONE)
                 {
                     $this->form_fields[$name]['template'] = 'Sonata\BaseApplicationBundle:CRUD:edit_one_to_one.twig';
+                    $this->form_fields[$name]['configuration']  = $this->getConfigurationPool()
+                        ->getConfigurationByClass($this->form_fields[$name]['targetEntity']);
+
                 }
 
                 if($this->form_fields[$name]['type'] == \Doctrine\ORM\Mapping\ClassMetadataInfo::MANY_TO_ONE)
@@ -278,6 +313,14 @@ abstract class Admin extends ContainerAware
                     $this->form_fields[$name]['configuration']  = $this->getConfigurationPool()
                         ->getConfigurationByClass($this->form_fields[$name]['targetEntity']);
                 }
+
+                if($this->form_fields[$name]['type'] == \Doctrine\ORM\Mapping\ClassMetadataInfo::ONE_TO_MANY)
+                {
+                    $this->form_fields[$name]['template'] = 'Sonata\BaseApplicationBundle:CRUD:edit_one_to_many.twig';
+                    $this->form_fields[$name]['configuration']  = $this->getConfigurationPool()
+                        ->getConfigurationByClass($this->form_fields[$name]['targetEntity']);
+                }
+                
             }
 
             // set correct default value
@@ -338,6 +381,13 @@ abstract class Admin extends ContainerAware
                     ->getConfigurationByClass($this->list_fields[$name]['targetEntity']);
             }
 
+            // fix template for mapping
+            if($this->list_fields[$name]['type'] == \Doctrine\ORM\Mapping\ClassMetadataInfo::ONE_TO_ONE) {
+                $this->list_fields[$name]['template']       = 'Sonata/BaseApplicationBundle:CRUD:list_one_to_one.twig';
+                $this->list_fields[$name]['configuration']  = $this->getConfigurationPool()
+                    ->getConfigurationByClass($this->list_fields[$name]['targetEntity']);
+            }
+
             if($this->list_fields[$name]['type'] == \Doctrine\ORM\Mapping\ClassMetadataInfo::ONE_TO_MANY) {
                 $this->list_fields[$name]['template']       = 'Sonata/BaseApplicationBundle:CRUD:list_one_to_many.twig';
             }
@@ -458,6 +508,7 @@ abstract class Admin extends ContainerAware
 
             switch($description['type']) {
 
+                case \Doctrine\ORM\Mapping\ClassMetadataInfo::ONE_TO_MANY:
                 case \Doctrine\ORM\Mapping\ClassMetadataInfo::MANY_TO_MANY:
 
                     $transformer = new \Symfony\Bundle\DoctrineBundle\Form\ValueTransformer\CollectionToChoiceTransformer(array(

+ 14 - 3
Controller/CRUDController.php

@@ -150,8 +150,7 @@ class CRUDController extends Controller
 
             $action = 'edit';
         } else {
-            $class = $this->configuration->getClass();
-            $object = new $class;
+            $object = $this->configuration->getNewInstance();
 
             $action = 'create';
         }
@@ -163,9 +162,21 @@ class CRUDController extends Controller
 
         if($form->isValid()) {
 
+            if($action == 'create') {
+                $this->configuration->preInsert($object);
+            } else {
+                $this->configuration->preUpdate($object);
+            }
+
             $this->configuration->getEntityManager()->persist($object);
             $this->configuration->getEntityManager()->flush($object);
 
+            if($action == 'create') {
+                $this->configuration->postInsert($object);
+            } else {
+                $this->configuration->postUpdate($object);
+            }
+
             if($this->get('request')->isXmlHttpRequest()) {
                 return $this->createResponse('ok');
             }
@@ -214,7 +225,7 @@ class CRUDController extends Controller
             $form   = $id;
         } else {
             $class = $this->configuration->getClass();
-            $object = new $class;
+            $object = $this->configuration->getNewInstance();
 
             $form   = $this->configuration->getForm($object, $fields);
         }

+ 5 - 1
Resources/views/CRUD/edit_array.twig

@@ -11,4 +11,8 @@ file that was distributed with this source code.
 
 {% extends 'Sonata/BaseApplicationBundle:CRUD:base_edit_field.twig' %}
 
-{% block field %}{{ form_field(field_element, {'class' : 'title'}) }}{% endblock %}
+{% block field %}
+    <span class="edit">
+        {{ form_field(field_element, {'class' : 'title'}) }}
+    </span>
+{% endblock %}

+ 12 - 0
Resources/views/CRUD/edit_one_to_many.twig

@@ -0,0 +1,12 @@
+{#
+
+This file is part of the Sonata package.
+
+(c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
+
+For the full copyright and license information, please view the LICENSE
+file that was distributed with this source code.
+
+#}
+
+{% extends 'Sonata/BaseApplicationBundle:CRUD:base_edit_field.twig' %}

+ 18 - 0
Resources/views/CRUD/edit_one_to_one.twig

@@ -10,3 +10,21 @@ file that was distributed with this source code.
 #}
 
 {% extends 'Sonata/BaseApplicationBundle:CRUD:base_edit_field.twig' %}
+
+{% block field %}
+    <div id="field_container_{{ configuration.code }}_{{ field_element.id}}">
+        <span id="field_widget_{{ configuration.code }}_{{ field_element.id}}" >
+            {{ form_field(field_element) }}
+        </span>
+
+        <span id="field_actions_{{ configuration.code }}_{{ field_element.id}}" >
+            <a href="{{ field_description.configuration.generateUrl('create') }}" onclick="start_field_dialog_form_add_{{ configuration.code }}_{{ field_element.id }}(event)" class="action"><img src="{{ asset('bundles/baseapplication/famfamfam/add.png') }}" alt="{% trans 'btn_add' from 'BaseApplicationBundle' %}" /></a>
+        </span>
+
+        <div style="display: hidden" id="field_dialog_{{ configuration.code }}_{{ field_element.id }}">
+
+        </div>
+    </div>
+
+    {% include 'Sonata/BaseApplicationBundle:CRUD:edit_many_association_script.twig' %}
+{% endblock %}

+ 18 - 0
Resources/views/CRUD/list_array.twig

@@ -0,0 +1,18 @@
+{#
+
+This file is part of the Sonata package.
+
+(c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
+
+For the full copyright and license information, please view the LICENSE
+file that was distributed with this source code.
+
+#}
+
+{% extends 'Sonata/BaseApplicationBundle:CRUD:base_list_field.twig' %}
+
+{% block field%}
+    {% for key, val in value %}
+        [{{ key }} => {{ val }}]
+    {% endfor %}
+{% endblock %}

+ 0 - 1
Twig/Extension/BaseApplicationExtension.php

@@ -68,7 +68,6 @@ class BaseApplicationExtension extends \Twig_Extension
         } else if(method_exists($object, $field_description['code'])) {
 
             $value = call_user_func(array($object, $field_description['code']));
-            
         }
 
         $template = $this->environment->loadTemplate($field_description['template']);