Forms 如何在奏鸣曲式中嵌入其他实体的字段?
我有两个实体Sport和Tarif(按价格翻译)通过多通关系联系在一起 我只希望有一个管理表单(在Sonata管理包中)来创建或删除包含三个字段的运动:Forms 如何在奏鸣曲式中嵌入其他实体的字段?,forms,symfony,sonata-admin,Forms,Symfony,Sonata Admin,我有两个实体Sport和Tarif(按价格翻译)通过多通关系联系在一起 我只希望有一个管理表单(在Sonata管理包中)来创建或删除包含三个字段的运动: 诽谤 ValerDebase(=价格值),它是Tarif实体的数字属性 ConditionDeReduce(=具有折扣的条件),它是Tarif实体的文本属性 我正在寻找一种方法来实现这一点,我发现使用CollectionType()在SportAdmin表单中嵌入Tarif字段,但这不起作用,正如您在下面看到的: 以下是实体: Sport
- 诽谤
- ValerDebase(=价格值),它是Tarif实体的数字属性
- ConditionDeReduce(=具有折扣的条件),它是Tarif实体的文本属性
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Sport
*
* @ORM\Table(name="sport")
* @ORM\Entity(repositoryClass="AppBundle\Repository\SportRepository")
*/
class Sport
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="libelle", type="string", length=255, unique=true)
*/
private $libelle;
/**
* Un Sport est lié à 1 et 1 seul Tarif
* @ORM\ManyToOne(targetEntity="Tarif")
* @ORM\JoinColumn(nullable=false)
*/
private $tarif;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set libelle
*
* @param string $libelle
* @return Sport
*/
public function setLibelle($libelle)
{
$this->libelle = $libelle;
return $this;
}
/**
* Get libelle
*
* @return string
*/
public function getLibelle()
{
return $this->libelle;
}
/**
* Constructor
*/
public function __construct()
{
$this->licences = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Add licence
*
* @param \AppBundle\Entity\Licence $licence
*
* @return Sport
*/
public function addLicence(\AppBundle\Entity\Licence $licence)
{
$this->licences[] = $licence;
return $this;
}
/**
* Remove licence
*
* @param \AppBundle\Entity\Licence $licence
*/
public function removeLicence(\AppBundle\Entity\Licence $licence)
{
$this->licences->removeElement($licence);
}
/**
* Get licences
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getLicences()
{
return $this->licences;
}
/**
* Set tarif
*
* @param \AppBundle\Entity\Tarif $tarif
*
* @return Sport
*/
public function setTarif(\AppBundle\Entity\Tarif $tarif)
{
$this->tarif = $tarif;
return $this;
}
/**
* Get tarif
*
* @return \AppBundle\Entity\Tarif
*/
public function getTarif()
{
return $this->tarif;
}
}
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Tarif
*
* @ORM\Table(name="tarif")
* @ORM\Entity(repositoryClass="AppBundle\Repository\TarifRepository")
*/
class Tarif
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="valeurDeBase", type="decimal", precision=10, scale=2)
*/
private $valeurDeBase;
/**
* @var string
*
* @ORM\Column(name="conditionReduction", type="text", nullable=true)
*/
private $conditionReduction;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set valeurDeBase
*
* @param string $valeurDeBase
* @return Tarif
*/
public function setValeurDeBase($valeurDeBase)
{
$this->valeurDeBase = $valeurDeBase;
return $this;
}
/**
* Get valeurDeBase
*
* @return string
*/
public function getValeurDeBase()
{
return $this->valeurDeBase;
}
/**
* Set conditionReduction
*
* @param string $conditionReduction
* @return Tarif
*/
public function setConditionReduction($conditionReduction)
{
$this->conditionReduction = $conditionReduction;
return $this;
}
/**
* Get conditionReduction
*
* @return string
*/
public function getConditionReduction()
{
return $this->conditionReduction;
}
}
<?php
// src/AppBundle/Admin/SportAdmin.php
namespace AppBundle\Admin;
use Sonata\AdminBundle\Admin\AbstractAdmin;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Datagrid\DatagridMapper;
use Sonata\AdminBundle\Form\FormMapper;
use Sonata\CoreBundle\Form\Type\CollectionType;
class SportAdmin extends AbstractAdmin
{
protected function configureFormFields(FormMapper $formMapper)
{
$formMapper->add('libelle', 'text');
$formMapper->add('tarif', CollectionType::class, array(
'by_reference' => false
),
array(
'edit' => 'inline',
'inline' => 'table',
'sortable' => 'position',
));
}
protected function configureDatagridFilters(DatagridMapper $datagridMapper)
{
$datagridMapper->add('libelle');
}
protected function configureListFields(ListMapper $listMapper)
{
$listMapper->addIdentifier('id');
$listMapper->add('libelle');
$listMapper->add('tarif.valeurDeBase');
$listMapper->add('tarif.conditionReduction');
}
public function toString($object)
{
return $object instanceof Sport
? $object->getTitle()
: 'Sport'; // shown in the breadcrumb on the create view
}
}
<?php
// src/AppBundle/Admin/TarifAdmin.php
namespace AppBundle\Admin;
use Sonata\AdminBundle\Admin\AbstractAdmin;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Datagrid\DatagridMapper;
use Sonata\AdminBundle\Form\FormMapper;
class TarifAdmin extends AbstractAdmin
{
protected function configureFormFields(FormMapper $formMapper)
{
$formMapper->add('valeurDeBase', 'number');
$formMapper->add('conditionReduction', 'text');
}
protected function configureDatagridFilters(DatagridMapper $datagridMapper)
{
}
protected function configureListFields(ListMapper $listMapper)
{
}
public function toString($object)
{
return $object instanceof Tarif
? $object->getTitle()
: 'Tarif'; // shown in the breadcrumb on the create view
}
}
<?php
// src/AppBundle/Admin/SportAdmin.php
namespace AppBundle\Admin;
use Sonata\AdminBundle\Admin\AbstractAdmin;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Datagrid\DatagridMapper;
use Sonata\AdminBundle\Form\FormMapper;
use Sonata\CoreBundle\Form\Type\CollectionType;
class SportAdmin extends AbstractAdmin
{
protected function configureFormFields(FormMapper $formMapper)
{
$formMapper->add('libelle', 'text');
$formMapper->add('tarif', 'sonata_type_admin', array(), array(
'admin_code' => 'admin.tarif'
));
}
protected function configureDatagridFilters(DatagridMapper $datagridMapper)
{
$datagridMapper->add('libelle');
}
protected function configureListFields(ListMapper $listMapper)
{
$listMapper->addIdentifier('id');
$listMapper->add('libelle');
$listMapper->add('tarif.valeurDeBase');
$listMapper->add('tarif.conditionReduction');
}
public function toString($object)
{
return $object instanceof Sport
? $object->getTitle()
: 'Sport'; // shown in the breadcrumb on the create view
}
}
最后,我为Tarif创建了一个管理块,并使用sonata_type_admin将其嵌入到SportAdmin中。它工作得很好。
这里是SportAdmin.php
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Sport
*
* @ORM\Table(name="sport")
* @ORM\Entity(repositoryClass="AppBundle\Repository\SportRepository")
*/
class Sport
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="libelle", type="string", length=255, unique=true)
*/
private $libelle;
/**
* Un Sport est lié à 1 et 1 seul Tarif
* @ORM\ManyToOne(targetEntity="Tarif")
* @ORM\JoinColumn(nullable=false)
*/
private $tarif;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set libelle
*
* @param string $libelle
* @return Sport
*/
public function setLibelle($libelle)
{
$this->libelle = $libelle;
return $this;
}
/**
* Get libelle
*
* @return string
*/
public function getLibelle()
{
return $this->libelle;
}
/**
* Constructor
*/
public function __construct()
{
$this->licences = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Add licence
*
* @param \AppBundle\Entity\Licence $licence
*
* @return Sport
*/
public function addLicence(\AppBundle\Entity\Licence $licence)
{
$this->licences[] = $licence;
return $this;
}
/**
* Remove licence
*
* @param \AppBundle\Entity\Licence $licence
*/
public function removeLicence(\AppBundle\Entity\Licence $licence)
{
$this->licences->removeElement($licence);
}
/**
* Get licences
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getLicences()
{
return $this->licences;
}
/**
* Set tarif
*
* @param \AppBundle\Entity\Tarif $tarif
*
* @return Sport
*/
public function setTarif(\AppBundle\Entity\Tarif $tarif)
{
$this->tarif = $tarif;
return $this;
}
/**
* Get tarif
*
* @return \AppBundle\Entity\Tarif
*/
public function getTarif()
{
return $this->tarif;
}
}
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Tarif
*
* @ORM\Table(name="tarif")
* @ORM\Entity(repositoryClass="AppBundle\Repository\TarifRepository")
*/
class Tarif
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="valeurDeBase", type="decimal", precision=10, scale=2)
*/
private $valeurDeBase;
/**
* @var string
*
* @ORM\Column(name="conditionReduction", type="text", nullable=true)
*/
private $conditionReduction;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set valeurDeBase
*
* @param string $valeurDeBase
* @return Tarif
*/
public function setValeurDeBase($valeurDeBase)
{
$this->valeurDeBase = $valeurDeBase;
return $this;
}
/**
* Get valeurDeBase
*
* @return string
*/
public function getValeurDeBase()
{
return $this->valeurDeBase;
}
/**
* Set conditionReduction
*
* @param string $conditionReduction
* @return Tarif
*/
public function setConditionReduction($conditionReduction)
{
$this->conditionReduction = $conditionReduction;
return $this;
}
/**
* Get conditionReduction
*
* @return string
*/
public function getConditionReduction()
{
return $this->conditionReduction;
}
}
<?php
// src/AppBundle/Admin/SportAdmin.php
namespace AppBundle\Admin;
use Sonata\AdminBundle\Admin\AbstractAdmin;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Datagrid\DatagridMapper;
use Sonata\AdminBundle\Form\FormMapper;
use Sonata\CoreBundle\Form\Type\CollectionType;
class SportAdmin extends AbstractAdmin
{
protected function configureFormFields(FormMapper $formMapper)
{
$formMapper->add('libelle', 'text');
$formMapper->add('tarif', CollectionType::class, array(
'by_reference' => false
),
array(
'edit' => 'inline',
'inline' => 'table',
'sortable' => 'position',
));
}
protected function configureDatagridFilters(DatagridMapper $datagridMapper)
{
$datagridMapper->add('libelle');
}
protected function configureListFields(ListMapper $listMapper)
{
$listMapper->addIdentifier('id');
$listMapper->add('libelle');
$listMapper->add('tarif.valeurDeBase');
$listMapper->add('tarif.conditionReduction');
}
public function toString($object)
{
return $object instanceof Sport
? $object->getTitle()
: 'Sport'; // shown in the breadcrumb on the create view
}
}
<?php
// src/AppBundle/Admin/TarifAdmin.php
namespace AppBundle\Admin;
use Sonata\AdminBundle\Admin\AbstractAdmin;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Datagrid\DatagridMapper;
use Sonata\AdminBundle\Form\FormMapper;
class TarifAdmin extends AbstractAdmin
{
protected function configureFormFields(FormMapper $formMapper)
{
$formMapper->add('valeurDeBase', 'number');
$formMapper->add('conditionReduction', 'text');
}
protected function configureDatagridFilters(DatagridMapper $datagridMapper)
{
}
protected function configureListFields(ListMapper $listMapper)
{
}
public function toString($object)
{
return $object instanceof Tarif
? $object->getTitle()
: 'Tarif'; // shown in the breadcrumb on the create view
}
}
<?php
// src/AppBundle/Admin/SportAdmin.php
namespace AppBundle\Admin;
use Sonata\AdminBundle\Admin\AbstractAdmin;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Datagrid\DatagridMapper;
use Sonata\AdminBundle\Form\FormMapper;
use Sonata\CoreBundle\Form\Type\CollectionType;
class SportAdmin extends AbstractAdmin
{
protected function configureFormFields(FormMapper $formMapper)
{
$formMapper->add('libelle', 'text');
$formMapper->add('tarif', 'sonata_type_admin', array(), array(
'admin_code' => 'admin.tarif'
));
}
protected function configureDatagridFilters(DatagridMapper $datagridMapper)
{
$datagridMapper->add('libelle');
}
protected function configureListFields(ListMapper $listMapper)
{
$listMapper->addIdentifier('id');
$listMapper->add('libelle');
$listMapper->add('tarif.valeurDeBase');
$listMapper->add('tarif.conditionReduction');
}
public function toString($object)
{
return $object instanceof Sport
? $object->getTitle()
: 'Sport'; // shown in the breadcrumb on the create view
}
}
最后,我为Tarif创建了一个管理块,并使用sonata_type_admin将其嵌入到SportAdmin中。它工作得很好。
这里是SportAdmin.php
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Sport
*
* @ORM\Table(name="sport")
* @ORM\Entity(repositoryClass="AppBundle\Repository\SportRepository")
*/
class Sport
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="libelle", type="string", length=255, unique=true)
*/
private $libelle;
/**
* Un Sport est lié à 1 et 1 seul Tarif
* @ORM\ManyToOne(targetEntity="Tarif")
* @ORM\JoinColumn(nullable=false)
*/
private $tarif;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set libelle
*
* @param string $libelle
* @return Sport
*/
public function setLibelle($libelle)
{
$this->libelle = $libelle;
return $this;
}
/**
* Get libelle
*
* @return string
*/
public function getLibelle()
{
return $this->libelle;
}
/**
* Constructor
*/
public function __construct()
{
$this->licences = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Add licence
*
* @param \AppBundle\Entity\Licence $licence
*
* @return Sport
*/
public function addLicence(\AppBundle\Entity\Licence $licence)
{
$this->licences[] = $licence;
return $this;
}
/**
* Remove licence
*
* @param \AppBundle\Entity\Licence $licence
*/
public function removeLicence(\AppBundle\Entity\Licence $licence)
{
$this->licences->removeElement($licence);
}
/**
* Get licences
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getLicences()
{
return $this->licences;
}
/**
* Set tarif
*
* @param \AppBundle\Entity\Tarif $tarif
*
* @return Sport
*/
public function setTarif(\AppBundle\Entity\Tarif $tarif)
{
$this->tarif = $tarif;
return $this;
}
/**
* Get tarif
*
* @return \AppBundle\Entity\Tarif
*/
public function getTarif()
{
return $this->tarif;
}
}
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Tarif
*
* @ORM\Table(name="tarif")
* @ORM\Entity(repositoryClass="AppBundle\Repository\TarifRepository")
*/
class Tarif
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="valeurDeBase", type="decimal", precision=10, scale=2)
*/
private $valeurDeBase;
/**
* @var string
*
* @ORM\Column(name="conditionReduction", type="text", nullable=true)
*/
private $conditionReduction;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set valeurDeBase
*
* @param string $valeurDeBase
* @return Tarif
*/
public function setValeurDeBase($valeurDeBase)
{
$this->valeurDeBase = $valeurDeBase;
return $this;
}
/**
* Get valeurDeBase
*
* @return string
*/
public function getValeurDeBase()
{
return $this->valeurDeBase;
}
/**
* Set conditionReduction
*
* @param string $conditionReduction
* @return Tarif
*/
public function setConditionReduction($conditionReduction)
{
$this->conditionReduction = $conditionReduction;
return $this;
}
/**
* Get conditionReduction
*
* @return string
*/
public function getConditionReduction()
{
return $this->conditionReduction;
}
}
<?php
// src/AppBundle/Admin/SportAdmin.php
namespace AppBundle\Admin;
use Sonata\AdminBundle\Admin\AbstractAdmin;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Datagrid\DatagridMapper;
use Sonata\AdminBundle\Form\FormMapper;
use Sonata\CoreBundle\Form\Type\CollectionType;
class SportAdmin extends AbstractAdmin
{
protected function configureFormFields(FormMapper $formMapper)
{
$formMapper->add('libelle', 'text');
$formMapper->add('tarif', CollectionType::class, array(
'by_reference' => false
),
array(
'edit' => 'inline',
'inline' => 'table',
'sortable' => 'position',
));
}
protected function configureDatagridFilters(DatagridMapper $datagridMapper)
{
$datagridMapper->add('libelle');
}
protected function configureListFields(ListMapper $listMapper)
{
$listMapper->addIdentifier('id');
$listMapper->add('libelle');
$listMapper->add('tarif.valeurDeBase');
$listMapper->add('tarif.conditionReduction');
}
public function toString($object)
{
return $object instanceof Sport
? $object->getTitle()
: 'Sport'; // shown in the breadcrumb on the create view
}
}
<?php
// src/AppBundle/Admin/TarifAdmin.php
namespace AppBundle\Admin;
use Sonata\AdminBundle\Admin\AbstractAdmin;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Datagrid\DatagridMapper;
use Sonata\AdminBundle\Form\FormMapper;
class TarifAdmin extends AbstractAdmin
{
protected function configureFormFields(FormMapper $formMapper)
{
$formMapper->add('valeurDeBase', 'number');
$formMapper->add('conditionReduction', 'text');
}
protected function configureDatagridFilters(DatagridMapper $datagridMapper)
{
}
protected function configureListFields(ListMapper $listMapper)
{
}
public function toString($object)
{
return $object instanceof Tarif
? $object->getTitle()
: 'Tarif'; // shown in the breadcrumb on the create view
}
}
<?php
// src/AppBundle/Admin/SportAdmin.php
namespace AppBundle\Admin;
use Sonata\AdminBundle\Admin\AbstractAdmin;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Datagrid\DatagridMapper;
use Sonata\AdminBundle\Form\FormMapper;
use Sonata\CoreBundle\Form\Type\CollectionType;
class SportAdmin extends AbstractAdmin
{
protected function configureFormFields(FormMapper $formMapper)
{
$formMapper->add('libelle', 'text');
$formMapper->add('tarif', 'sonata_type_admin', array(), array(
'admin_code' => 'admin.tarif'
));
}
protected function configureDatagridFilters(DatagridMapper $datagridMapper)
{
$datagridMapper->add('libelle');
}
protected function configureListFields(ListMapper $listMapper)
{
$listMapper->addIdentifier('id');
$listMapper->add('libelle');
$listMapper->add('tarif.valeurDeBase');
$listMapper->add('tarif.conditionReduction');
}
public function toString($object)
{
return $object instanceof Sport
? $object->getTitle()
: 'Sport'; // shown in the breadcrumb on the create view
}
}