symfony2admingenerator / AdmingeneratorGeneratorBundle

(old-legacy) Admingenerator for Symfony2, parse generator.yml files to build classes
http://symfony2admingenerator.org/
MIT License
360 stars 125 forks source link

Undefined "attemptObjectNew" method / i18n fields #744

Closed siuho closed 10 years ago

siuho commented 10 years ago

I'm tying to create a new object but got this error :

Undefined "attemptObjectNew" method. Does object action "new" exist in your generator file?  

Here's my generator file :


generator: admingenerator.generator.propel
params:
  model: Bundle\Model\Contact
  namespace_prefix: Bundle
  bundle_name: AdminWebsiteBundle
  fields: 
      contactI18ns:
          label: Traductions
          dbType: collection
          formType: translatable_collection
          addFormOptions:
              i18n_class: Bundle\Model\ContactI18n
              columns:
                  service:
                      type: textarea
                  poste:
                      type: textarea
      fax:
          addFormOptions:
              required: false 
      service_fr_FR:
          label: Service FR
      service_en_US:
          label: Service EN
      poste_fr_FR:
          label: Poste FR
      poste_en_US:
          label: Poste EN
  object_actions:
      delete: ~
  batch_actions:
      delete: ~

builders:
  list:
      params:
          sort: [ id, ASC ]
          title: Liste des Contacts
          display: 
          "NONE": [ nom, prenom, telephone, fax, mail, service_fr_FR, service_en_US, poste_fr_FR, poste_en_US ]
          actions:
              new: ~
          object_actions:
             edit: ~
             delete: ~

  filters:
    params:
      display: [mail, nom]
  new:
      params:
          title: Nouveau contact
          display: [nom, prenom, telephone, fax, mail, contactI18ns ]
          actions:
            save: ~
            list: ~
  edit:
      params:
          title: "Editer le contact \"%object%\"|{ %object%: Contact.nom }|"
          display: [ nom, prenom, telephone, fax, mail, contactI18ns ]
          actions:
            list: ~
            save: ~
 

But I know that I have another object call "Admin" and it seems that it search in the wrong place :

[..]Admingenerated/../BaseAdminController/ActionsController.php at line 23 
instead of BaseContactController? And I can create a new object Admin without problems. What can I do? Thank you
siuho commented 10 years ago

I've changed my routing because I think there were conflict (/admin and /admin/contact/ => /admin_contact) But now I have a problems with my i18n fields. It's available when I edit a contact but not when I created a new one. I think it's because I have to create a ContactI18n object but I don't know when exactly and how to embed this with the existing object Contact??

Here's my translatableCollectionType.php

private $container;
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $languages = $options['languages'];
        $i18nClass = $options['i18n_class'];
        $options['options']['data_class'] = $i18nClass;
        $options['options']['columns'] = $options['columns'];
        $builder->addEventListener(FormEvents::PRE_SET_DATA, function(FormEvent $event) use ($builder, $languages, $i18nClass) {
            $form = $event->getForm();
            $data = $event->getData();
            if($data == null)
            {
            return;
            }
            //get the class name of the i18nClass
            $temp = explode('\\', $i18nClass);
            $dataClass = end($temp);
            $rootData = $form->getRoot()->getData();
            //add a database row for every needed language
            foreach($languages as $lang)
            {
                $found = false;
                foreach($data as $i18n)
                {
                    if($i18n->getLocale() == $lang)
                    {
                        $found = true;
                        break;
                    }
                }
                if(!$found)
                {
                    $newTranslation = new $i18nClass();
                    $newTranslation->setLocale($lang);

                    $addFunction = 'add'.$dataClass;
                    $rootData->$addFunction($newTranslation);
                }               
            }
        });
            parent::buildForm($builder, $options);
    }
    public function getName()
    {
        return 'translatable_collection';
    }
    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        parent::setDefaultOptions($resolver);
        $resolver->setDefaults(array(
                'languages' => array(),
                'i18n_class' => '',
                'attr' => array('class' => 'type_collection'),
                'columns' => array(),
                'type' => 'translation_type',
                'allow_add' => false,
                'allow_delete' => false,
                'languages' => $this->container->getParameter('languages')
        ));
    }
    /**
    * Sets the Container.
    */
    public function setContainer(ContainerInterface $container = null)
    {
        $this->container = $container;
    }

And my TranslationType.php which create the input for the i18n fields


public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $columns = $options['columns'];
        $dataClass = $options['data_class'];
        $builder->addEventListener(FormEvents::PRE_SET_DATA, function(FormEvent $event) use ($builder, $columns, $dataClass) {
            $form = $event->getForm();
            $data = $event->getData();
            if(!$data instanceof $dataClass)
            {
                return;
            }

            //loop over all columns and add the input
            foreach($columns as $column => $options)
            {
                if($options == null) $options = array();
                $type = 'text';
                if(array_key_exists('type', $options))
                {
                    $type = $options['type'];
                }
                $label = $column;
                if(array_key_exists('label', $options))
                {
                    $label = $options['label'];
                }
                $customOptions = array();
                if(array_key_exists('options', $options))
                {
                    $customOptions = $options['options'];
                }
                $options = array(
                        'label' => $label.' '.strtoupper($data->getLocale()),
                        'auto_initialize' => false
                );
                $options = array_merge($options, $customOptions);
                $form->add($builder->getFormFactory()->createNamed($column, $type, null, $options));

            }
        });
    }
    public function getName()
    {
        return 'translation_type';
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        parent::setDefaultOptions($resolver);

        $resolver->setDefaults(array(
                'columns' => array(),
        ));
    }
    public function getDefaultOptions(array $options)
    {
        return $options + array(
                'columns' => array(),
                'language' => ''
        );
    }

Did I do something wrong?

Thank you (and sorry for my bad english)

siuho commented 10 years ago

I replace


$formOptions = $this->getFormOption('contactI18ns', array(  'allow_add' => true,  'allow_delete' => true,  'by_reference' => false,  'i18n_class' => 'Bundle\\Model\\ContactI18n',  'columns' =>   array(    'service' =>array( 'type' => 'textarea',    ),  ),  'label' => 'Traductions',  'translation_domain' => 'Admin',));
$builder->add('contactI18ns', 'translatable_collection', $formOptions);

By


$builder->add('contactI18ns', 'translatable_collection', array(  'required' => false,  'i18n_class' => 'Bundle\\Model\\ContactI18n',  'columns' => array( 'service' =>array( 'type' => 'textarea', ), ), 'label' => 'Traductions', 'translation_domain' => 'Admin', 'options'=> array('label' => false)));

in the new form and it works. Don't know why it does not love the first syntax