如何以奏鸣曲形式嵌入其他实体的字段?

时间:2017-10-10 11:31:25

标签: forms symfony sonata-admin

我有两个实体Sport和Tarif(按价格翻译)由ManyToOne关系链接。

我希望只有一个管理表单(在Sonata Admin Bundle中)创建或删除包含三个字段的Sport:

  • libelle(=姓名)
  • valeurDeBase(=价格值),它是Tarif实体的数字属性
  • conditionDeReduction(=有折扣的条件),这是Tarif实体的文本属性

我正在寻找一种方法来实现这一点,我发现使用CollectionType(https://sonata-project.org/bundles/doctrine-orm-admin/master/doc/reference/form_field_definition.html)将Tarif字段嵌入到SportAdmin表单中,但是这不起作用,如下所示:

Screen capture of SportAdmin form

以下是实体:

Sport.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;
}
}

Tarif.php

<?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;
}
}

SportAdmin.php

<?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
}
}

TarifAdmin.php

<?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
}
}

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

最后,我为Tarif创建了一个管理块,并使用sonata_type_admin将其嵌入到SportAdmin中。它完美地运作。 这里是 SportAdmin.php

<?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
}
}