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

I have to enter a child entity ( one to many ) in which it does not serve to select the father #2890

Closed meiome closed 9 years ago

meiome commented 9 years ago

I find only this method ('sonata_type_collection') to insert a entity cascade one to many I want that the entity daughter does not require the selection of his father, but that this is taken directly help my please!!!!

//ArticleAdmin namespace Andrea\PrimoBundle\Admin;

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

class ArticleAdmin extends Admin { // Fields to be shown on create/edit forms protected function configureFormFields(FormMapper $formMapper) { $formMapper ->add('description', 'text', array('label' => 'Descrizione Articolo')) ->add('code', 'text', array('label' => 'Codice Articolo')) ->add('disable', 'checkbox', array('required' => false,'label' => 'Disabilitato')) //->add('taxcode', null, array('label' => 'Codice Iva')) ->add('taxcode', 'entity', array('class' => 'Andrea\PrimoBundle\Entity\TaxCode'))

        ->add('pricelist', 'sonata_type_collection', array(), array(
            'edit' => 'inline',
            'inline' => 'table',
            'sortable'  => 'position'
        ))

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

/**
 * @ORM\Column(type="string", length=10)
 */
protected $code;

/**
 * @ORM\Column(type="string", length=100)
 */
protected $description;

/**
 * @ORM\Column(type="boolean")
 */
protected $disable;

/**
 * @ORM\ManyToOne(targetEntity="TaxCode", inversedBy="article")
 * @ORM\JoinColumn(name="taxcode_id", referencedColumnName="id")
 */
protected $taxcode;

/**
 * @ORM\OneToMany(targetEntity="PriceList", mappedBy="article", cascade="all")
 */
protected $pricelist;

//etc ........................................ }

class PriceList { /* * @var integer * @ORM\Column(name="id", type="integer") * @ORM\Id * @ORM\GeneratedValue(strategy="AUTO") */ private $id;

/**
 * @var float
 *
 * @ORM\Column(name="price", type="float")
 */
private $price;

/**
 * @var \DateTime
 *
 * @ORM\Column(name="startdate", type="date")
 */
private $startdate;

/**
 * @var \DateTime
 *
 * @ORM\Column(name="enddate", type="date", nullable=true)
 */
private $enddate;

/**
 * @ORM\ManyToOne(targetEntity="ListName", inversedBy="pricelist")
 * @ORM\JoinColumn(name="listname_id", referencedColumnName="id")
 */
protected $listname;

/**
 * @ORM\ManyToOne(targetEntity="Article", inversedBy="pricelist")
 * @ORM\JoinColumn(name="article_id", referencedColumnName="id")
 */
protected $article;

//etc....

}

sfavot commented 9 years ago

use a different PriceListAdmin class in which you'll set the field article as hidden, you can specify which Admin class to use in your Article form with the option 'admin_code' in the second array like so :

$formMapper->add('pricelist', 'sonata_type_collection', array(), array( 'edit' => 'inline', 'inline' => 'table', 'sortable' => 'position', 'admin_code' => 'sonata.admin.price_list', ))

meiome commented 9 years ago

thankyou sfavot!!!!!