简体   繁体   English

如何在SonataAdminBundle中添加具有关系的新实体

[英]How to add new entities with relations in SonataAdminBundle

Currently I'm building a web-shop, and there are products that I want to manage in my system. 目前,我正在建立一个网上商店,有些产品需要在系统中进行管理。 The thing is that SonataAdmin doesn't allow me to edit two entities with relation! 事实是SonataAdmin不允许我编辑两个具有关联的实体!

  1. There is "RFID" Entity: 有“ RFID”实体:

namespace Admin\IdManageBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

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

    /**
     * @var string
     *
     * @ORM\Column(name="Barcode", type="string", length=255)
     */
    private $barcode;


    /**
     * Get id
     *
     * @return integer
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set barcode
     *
     * @param string $barcode
     *
     * @return RFID
     */
    public function setBarcode($barcode)
    {
        $this->barcode = $barcode;

        return $this;
    }

    /**
     * Get barcode
     *
     * @return string
     */
    public function getBarcode()
    {
        return $this->barcode;
    }
}
  1. The "Product" Entity: “产品”实体:

namespace Admin\StorageBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

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

    /**
     * @var string
     *
     * @ORM\Column(name="Name", type="string", length=255)
     */
    private $name;

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

    /**
     * @ORM\OneToOne(targetEntity="Admin\IdManageBundle\Entity\RFID")
     */
    private $rfid;


    /**
     * Get id
     *
     * @return integer
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set name
     *
     * @param string $name
     *
     * @return Product
     */
    public function setName($name)
    {
        $this->name = $name;

        return $this;
    }

    /**
     * Get name
     *
     * @return string
     */
    public function getName()
    {
        return $this->name;
    }

    /**
     * Set price
     *
     * @param float $price
     *
     * @return Product
     */
    public function setPrice($price)
    {
        $this->price = $price;

        return $this;
    }

    /**
     * Get price
     *
     * @return float
     */
    public function getPrice()
    {
        return $this->price;
    }

    /**
     * Set rfid
     *
     * @param \Admin\IdManageBundle\Entity\RFID $rfid
     *
     * @return Product
     */
    public function setRfid(\Admin\IdManageBundle\Entity\RFID $rfid = null)
    {
        $this->rfid = $rfid;

        return $this;
    }

    /**
     * Get rfid
     *
     * @return \Admin\IdManageBundle\Entity\RFID
     */
    public function getRfid()
    {
        return $this->rfid;
    }
}

And a "CellPhoneGlass" Entity: 还有一个“ CellPhoneGlass”实体:

namespace Admin\StorageBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

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

    /**
     * @var string
     *
     * @ORM\Column(name="Phone", type="string", length=255)
     */
    private $phone;

    /**
     * @var string
     *
     * @ORM\Column(name="Model", type="string", length=255)
     */
    private $model;

    /**
     * @ORM\ManyToOne(targetEntity="Admin\StorageBundle\Entity\Product")
     */
    private $product;


    /**
     * Get id
     *
     * @return integer
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set phone
     *
     * @param string $phone
     *
     * @return CellPhoneGlass
     */
    public function setPhone($phone)
    {
        $this->phone = $phone;

        return $this;
    }

    /**
     * Get phone
     *
     * @return string
     */
    public function getPhone()
    {
        return $this->phone;
    }

    /**
     * Set model
     *
     * @param string $model
     *
     * @return CellPhoneGlass
     */
    public function setModel($model)
    {
        $this->model = $model;

        return $this;
    }

    /**
     * Get model
     *
     * @return string
     */
    public function getModel()
    {
        return $this->model;
    }

    /**
     * Set product
     *
     * @param \Admin\StorageBundle\Entity\Product $product
     *
     * @return CellPhoneGlass
     */
    public function setProduct(\Admin\StorageBundle\Entity\Product $product = null)
    {
        $this->product = $product;

        return $this;
    }

    /**
     * Get product
     *
     * @return \Admin\StorageBundle\Entity\Product
     */
    public function getProduct()
    {
        return $this->product;
    }
}

Sonata doesn't shows the product entity with "sonata_model_type. here is Admin class: Sonata未显示带有“ sonata_model_type的产品实体。这是Admin类:

namespace Admin\StorageBundle\Admin;

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

class CellPhoneGlassAdmin extends Admin
{
    /**
     * @param DatagridMapper $datagridMapper
     */
    protected function configureDatagridFilters(DatagridMapper $datagridMapper)
    {
        $datagridMapper
            ->tab("Product")
            ->with("")
            ->add('product', 'sonata_type_model', array(
                'class' => 'Admin\StorageBundle\Entity\Product',
                'property' => 'name',
                'by_reference' => false,
            ))
            ->end()
            ->end()

            ->tab("Glass")
            ->with("")
            ->add('phone')
            ->add('model')
            ->end()
            ->end()
        ;
    }

    /**
     * @param ListMapper $listMapper
     */
    protected function configureListFields(ListMapper $listMapper)
    {
        $listMapper
            ->tab("Product")
            ->with("")
            ->add('product', 'sonata_type_model', array(
                'class' => 'Admin\StorageBundle\Entity\Product',
                'property' => 'name',
                'by_reference' => false,
            ))
            ->end()
            ->end()

            ->tab("Glass")
            ->with("")
            ->add('phone')
            ->add('model')
            ->end()
            ->end()
        ;
    }

    /**
     * @param FormMapper $formMapper
     */
    protected function configureFormFields(FormMapper $formMapper)
    {
        $formMapper
            ->tab("Product")
            ->with("")
            ->add('product', 'sonata_type_model', array(
                'class' => 'Admin\StorageBundle\Entity\Product',
                'property' => 'name',
                'by_reference' => false,
            ))
            ->end()
            ->end()

            ->tab("Glass")
            ->with("")
            ->add('phone')
            ->add('model')
            ->end()
            ->end()
        ;
    }

    /**
     * @param ShowMapper $showMapper
     */
    protected function configureShowFields(ShowMapper $showMapper)
    {
        $showMapper
            ->tab("Product")
            ->with("Content")
            ->add('product', 'sonata_type_model', array(
                'class' => 'Admin\StorageBundle\Entity\Product',
                'property' => 'name',
                'by_reference' => false,
            ))
            ->end()
            ->end()

            ->tab("Glass")
            ->with("Content")
            ->add('phone')
            ->add('model')
            ->end()
            ->end()
        ;
    }
}

Not sure if I understood correctly what you want, I assume that you want to add new Product from the CellPhoneGlass form. 不知道我是否正确理解了您想要什么,我假设您想从CellPhoneGlass表单中添加新产品。

You have to use sonata_type_model_list to do so, the sonata_type_model allows simply to select existing entities : 您必须使用sonata_type_model_list来执行此操作,sonata_type_model允许仅选择现有实体:

protected function configureFormFields(FormMapper $formMapper)
{
    $formMapper
        ->tab("Product")
            ->with("")
                ->add('product', 'sonata_type_model_list')
            ->end()
        ->end()
        ->tab("Glass")
            ->with("")
                ->add('phone')
                ->add('model')
            ->end()
        ->end()
    ;
}

Official documentation : https://sonata-project.org/bundles/doctrine-orm-admin/master/doc/reference/form_field_definition.html#advanced-usage-many-to-one 官方文档: https : //sonata-project.org/bundles/doctrine-orm-admin/master/doc/reference/form_field_definition.html#advanced-usage-many-to-one

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM