sonata-project / SonataAdminBundle

The missing Symfony Admin Generator
https://docs.sonata-project.org/projects/SonataAdminBundle
MIT License
2.11k stars 1.26k forks source link

Relation oneToMany and ajax create #3373

Closed 2b3ez closed 8 years ago

2b3ez commented 9 years ago

I can not create a contact from my company service.

symfony : 2.7 sonataAdmin : 2.4

my Entity Company (Parent)

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;

class Company
{
    /**
     * @var int $id
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     * @ORM\Column(name="id_company", type="integer")
     */
    protected $id;
    /**
     * @ORM\OneToMany(targetEntity="AppBundle\Entity\Contact", mappedBy="company", cascade={"all"}, orphanRemoval=true)
     * @ORM\OrderBy({"main" = "DESC"})
     */
    protected $contacts;

    public function __construct(){
        $this->contacts=new ArrayCollection();
    }

    /**
     * @param Contact $contact
     * @return Company
     */
    public function addContact(Contact $contact)
    {
        $contact->setCompany($this);
        $this->contacts[] = $contact;
        return $this;
    }

    /**
     * @param Contact $contact
     */
    public function removeContact(Contact $contact)
    {
        $this->contacts->removeElement($contact);
    }

    /**
     * @return mixed
     */
    public function getContacts()
    {
        return $this->contacts;
    }

    /**
     * @param mixed $contacts
     */
    public function setContacts($contacts)
    {
        $this->contacts = $contacts;
    }

my Entity Contact (Children)

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

class Contact
{
    /**
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     * @ORM\Column(name="id_contact", type="integer")
     */
    protected $id;

    /**
     * @ORM\ManyToOne(targetEntity="AppBundle\Entity\Company", inversedBy="contacts")
     * @ORM\JoinColumn(name="id_company", referencedColumnName="id_company", nullable=false)
     */
    protected $company;

my class Admin Company (Parent)

namespace AppBundle\Admin;

use Sonata\AdminBundle\Admin\Admin;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Datagrid\DatagridMapper;
use Sonata\AdminBundle\Form\FormMapper;

class CompanyAdmin extends Admin
{
    protected function configureFormFields(FormMapper $formMapper)
    {
        $formMapper
            ->with('app.admin.form.legend_company_details', array('class' => 'col-md-6', 'box_class' => 'box box-danger'))
            ->add('contacts', 'sonata_type_collection', array('label' => 'app.admin.form.label_contacts', 'required' => false, 'by_reference' => false, 'cascade_validation'=>true), array(
             'edit' => 'inline',
             'inline' => 'table'
         ))
            ->end();
    }
}

my class Admin Contact (Children)

namespace AppBundle\Admin;

use Sonata\AdminBundle\Admin\Admin;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Datagrid\DatagridMapper;
use Sonata\AdminBundle\Form\FormMapper;

class ContactAdmin extends Admin
{
    protected $parentAssociationMapping = 'Company';

    protected function configureFormFields(FormMapper $formMapper)
    {
            $formMapper
                ->with('app.admin.form.legend_customer', array('class' => 'col-md-12', 'box_class' => 'box box-default'))
                ->add('gender', 'sonata_type_model', array('label' => 'app.admin.form.label_gender', 'expanded' => true, 'btn_add'=>false, 'horizontal_label_class' => 'radio-inline'))
                ->add('name', 'text', array('label' => 'app.admin.form.label_name'))
                ->add('firstname', 'text', array('label' => 'app.admin.form.label_firstname', 'required' => false))
                ->add('office_phone', 'text', array('label' => 'app.admin.form.label_officephone'))
                ->add('main', 'checkbox', array('label' => 'app.admin.form.label_main', 'required' => false, 'attr'=>array('checked' => 'checked')))
                ->add('company', 'sonata_type_model', array('label' => 'app.admin.form.label_company', 'btn_add'=>false))
                ->add('newsletter', 'hidden', array('data'=>'0'))
                ->end();
    }
}

My services

  sonata.admin.company:
    class: AppBundle\Admin\CompanyAdmin
    tags:
        - { name: sonata.admin, manager_type: orm, group: app.admin.menu.group_contact, label: app.admin.menu.item_company, label_catalogue: "AppBundle", icon: <i class="fa fa-users"></i>}
    arguments:
        - ~
        - AppBundle\Entity\Company
        - ~
    calls:
        - [ setTranslationDomain, [AppBundle]]

  sonata.admin.contact:
    class: AppBundle\Admin\ContactAdmin
    tags:
      - { name: sonata.admin, manager_type: orm, group: app.admin.menu.group_contact, label: app.admin.menu.item_contact, label_catalogue: "AppBundle"}
    arguments:
      - ~
      - AppBundle\Entity\Contact
      - ~
    calls:
      - [ setTranslationDomain, [AppBundle]]

When I want to add a new contact from company no default value was selected for company select in my embed form contact.

If I selected a company I got an error 500 on submit in ajax with this message : Invalid argument, object or null required

If I delete the company field in my contact form I have this error when I submit : SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'id_company' cannot be null

I have read a lot of pots and article through the net without found a working issue. :(

What I did wrong ???

If someone could explain me that would be great ! Thanks to read my post ;)

greg0ire commented 8 years ago

Hi, sorry to break this news to you, but Sonata 2 is no longer supported. Please reopen if you still experience this with Sonata 3.