Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

Form\Collection: allow create new objects #4075

Closed
wants to merge 3 commits into
from

Conversation

Projects
None yet
6 participants
Contributor

Slamdunk commented Mar 19, 2013

The commit b0c03a3 , merged in 2.1.4, introduced a compatibility break: before that commit, the behaviour of collection was totally different. If you expecially used collections with Doctrine Entities, this is a huge change.

This PR reintroduce the ability to create new objects if requested, and leave the new behaviour by default.

Contributor

bakura10 commented Mar 28, 2013

I suppose the fix should revert back to previous behaviour by default, not to the new one that provided a BC. I'm not sure to understand everything though (sorry), but Doctrine entities should not be cloned right (mainly because of collections) ?

EDIT : nvm, I think I understand... Basically 2.1.4 introduced a way so that objects are not dummies anymore which is good. The thing that confused me in that all my form I have a hidden "id" field so I never encounter this problem becuase my entities hydrate this too.

Owner

weierophinney commented Apr 3, 2013

@bakura10 Do you feel the approach taken here makes sense? I'd argue that the default behavior should be what it was before, so the flag would need to be inverted. Thoughts?

Contributor

Slamdunk commented Apr 3, 2013

The default flag for me doesn't matter, but could be misleading to have one behaviour < 2.1.4, another = 2.1.4 and the old one again in > 2.1.4
Btw it's up to you

Tomorrow morning I'll rebase this PR to see what happens with zendframework#4077 that has just been merged

Contributor

bakura10 commented Apr 4, 2013

@weierophinney I think that what it makes more sense is keeping the same object (so no creation) because of the problme it solves... At the same time, the whole 2.0 branch and 2.1.0-3 has been with the old way so to my opininon I think it should be reverted to what it was before although the new behaviour may remove some strange things. I really don't know to be honest.

@ghost ghost assigned weierophinney Apr 11, 2013

weierophinney added a commit that referenced this pull request Apr 11, 2013

Merge pull request #4075 from Slamdunk/collection-allow-recreate
Form\Collection: allow create new objects

weierophinney added a commit that referenced this pull request Apr 11, 2013

joacub commented May 16, 2013

this still does not work if count is 0 does not add new elements

array(
                    'type' => 'Zend\Form\Element\Collection',
                    'name' => 'detail',
                    'options' => array(
                        'label' => 'Listado de productos para esta venta',
                        'count' => 0,
                        'should_create_template' => true,
                        'template_placeholder' => '__placeholder__',
                        'allow_add' => true,
                        'allow_delete' => true,
                        'target_element' => $fieldset
                    )
                ));
Contributor

thestanislav commented May 21, 2013

See also this zendframework#4492

joacub commented May 21, 2013

@thestanislav I looked at your reference but does not solve my problem, the problem is that pupolate fails if count is 0 that does not create objects if you do not you set a count to 1 0 2 or whatever, but if 0 does not create anything always leave it empty this is a failure queire not see what I see.

Contributor

davidwindell commented May 21, 2013

Guys, the best thing to do would be to create a PR with a failing text to demonstrate the issue.

joacub commented May 21, 2013

it give me a moment now shipping

joacub commented May 21, 2013

This is an example for sales:
This is the form that comprises all

<?php
namespace CustomediaGestionSales\Form;

use DoctrineModule\Stdlib\Hydrator\DoctrineObject as DoctrineHydrator;
use Zend\Form\Form;
use Zend\ServiceManager\ServiceManager;

class SalesForm extends Form
{
    public function __construct(ServiceManager $serviceManager)
    {
        parent::__construct('customer-form');
        $entityManager = $serviceManager->get('cmgestion_doctrine_em');

        // The form will hydrate an object of type "Customers"
        $this->setHydrator(new DoctrineHydrator($entityManager, 'CustomediaGestionSales\Entity\Sales'));

        // Add the user fieldset, and set it as the base fieldset
        $fieldset = $serviceManager->get('FormElementManager')->get('CustomediaGestionSales\Form\SalesFieldset');
        $fieldset->setUseAsBaseFieldset(true);
        $this->add($fieldset);


    }
}

CustomediaGestionSales\Form\SalesFieldset:

<?php
namespace CustomediaGestionSales\Form;
use DoctrineModule\Stdlib\Hydrator\DoctrineObject as DoctrineHydrator;
use Zend\Form\Fieldset;
use Zend\ServiceManager\ServiceManager;
use CustomediaGestionCustomers\Entity\Customers;
use Zend\Form\Element\Csrf;
use Nette\Diagnostics\Debugger;

class SalesFieldset extends Fieldset
{

    public function __construct (ServiceManager $serviceManager)
    {
        $view = $serviceManager->get('viewHelperManager');
        $basepath = $view->get('basepath');
        $view->get('inlinescript')->appendFile(
                $basepath->__invoke() . '/CustomediaGestionSales/admin/sales.js');
        $view->get('inlinescript')->appendFile(
                $basepath->__invoke() . '/CustomediaGestionSales/admin/Hogan.js');

        parent::__construct('sales');
        $entityManager = $serviceManager->get('cmgestion_doctrine_em');

        $this->setHydrator(
                new DoctrineHydrator($entityManager, 
                        'CustomediaGestionSales\Entity\Sales'))->setObject(
                new Customers());

        $form = $serviceManager->get('formGenerator')
            ->setClass('CustomediaGestionSales\Entity\Sales')
            ->getForm();
        $elements = $form->getElements();

        foreach ($elements as $e) {
            if ($e instanceof Csrf)
                continue;
            $this->add($e);
        }

        $fieldset = $serviceManager->get('FormElementManager')->get(
                'CustomediaGestionSales\Form\SalesDetailsFieldset');
        $fieldset->setUseAsBaseFieldset(true);

        $this->add(
                array(
                    'type' => 'Zend\Form\Element\Collection',
                    'name' => 'detail',
                    'options' => array(
                        'label' => 'Listado de productos para esta venta',
                        'count' => 0,
                        'should_create_template' => true,
                        'template_placeholder' => '__placeholder__',
                        'allow_add' => true,
                        'allow_delete' => true,
                        'target_element' => $fieldset,
                    )
                ));


    }

}

CustomediaGestionSales\Form\SalesDetailsFieldset:

<?php
namespace CustomediaGestionSales\Form;
use DoctrineModule\Stdlib\Hydrator\DoctrineObject as DoctrineHydrator;
use Zend\Form\Fieldset;
use AtDataGrid\DataGrid\DataSource;
use Zend\Form\Element\Csrf;
use Zend\Form\Element\Hidden;
use CustomediaGestionSales\Entity\SalesDetails;
use Zend\ServiceManager\ServiceManager;
use Nette\Diagnostics\Debugger;

class SalesDetailsFieldset extends Fieldset
{

    public function __construct (ServiceManager $serviceManager)
    {
        parent::__construct('sales-details');
        $entityManager = $serviceManager->get('cmgestion_doctrine_em');

        $this->setHydrator(
                new DoctrineHydrator($entityManager, 
                        'CustomediaGestionSales\Entity\SalesDetails'))->setObject(
                new SalesDetails());

        $this->add(
                array(
                    'type' => 'Zend\Form\Element\Hidden',
                    'name' => 'id',
                )
        );

        $this->add(
                array(
                    'type' => 'Zend\Form\Element\Text',
                    'name' => 'catalog_name',
                    'options' => array(
                        'label' => 'Buscar producto'
                    ),
                    'attributes' => array(
                        'autocomplete' => 'off'
                    )
                )
                /*array(
                    'type' => 'Zend\Form\Element\Text',
                    'name' => 'sale',
                    'options' => array(
                        'label' => 'Venta',
                        'object_manager' => $entityManager,
                        'target_class'   => 'CustomediaGestionSales\Entity\Sales',
                        'property'       => 'sodBy',
                    ),
                ),*/

                );

        $this->add(
                array(
                    'type' => 'CustomediaGestionBase\Doctrine\Form\Element\ObjectHidden',
                    'name' => 'article',
                    'options' => array(
                        'object_manager' => $entityManager,
                        'target_class' => 'CustomediaGestionCatalog\Entity\Catalog',
                        'property' => 'name',
                        'is_method' => true,
                        'find_method' => array(
                            'name' => 'findBy',
                            'params' => array(
                                'criteria' => array(
                                    'id' => null
                                )
                            // 'orderBy' => array('lastname' => 'ASC'),
                                                        )
                        )
                    )
                ));

        $this->add(
                array(
                    'type' => 'Zend\Form\Element\Text',
                    'name' => 'quantity',
                    'options' => array(
                        'label' => 'Cantidad'
                    )
                ));

        $this->add(
                array(
                    'type' => 'Zend\Form\Element\Text',
                    'name' => 'quantityReturn',
                    'options' => array(
                        'label' => 'Cantidad devuelta'
                    )
                ));

        $this->add(
                array(
                    'type' => 'Zend\Form\Element\Text',
                    'name' => 'productPrice',
                    'options' => array(
                        'label' => 'Precio producto'
                    )
                ));

        $this->add(
                array(
                    'type' => 'Zend\Form\Element\Text',
                    'name' => 'productReference',
                    'options' => array(
                        'label' => 'Refernecia del producto'
                    )
                ));

        $this->add(
                array(
                    'type' => 'Zend\Form\Element\Text',
                    'name' => 'unitPriceTaxIncl',
                    'options' => array(
                        'label' => 'Precio con iva incl'
                    )
                ));

        $this->add(
                array(
                    'type' => 'Zend\Form\Element\Text',
                    'name' => 'unitPriceTaxExcl',
                    'options' => array(
                        'label' => 'Precio sin iva'
                    )
                ));

        $options = array(
            'bootstrap' => array(),
        );
        $this->setOptions($options);

        $this->setLabel('Producto');
    }
}

well then the fieldset to count to 0 does not add new objects created automatically via javascript and sent via post if the count does not get any positive number.

joacub commented May 23, 2013

Any comments?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment