我有一个在Sonata Admin Bundle之上开发的仪表板,我遇到了问题。这就是方法configureFormFields(FormMapper $formMapper)
在CompanyAdmin
管理员控制器上的样子:
protected function configureFormFields(FormMapper $formMapper)
{
$image = $this->getSubject();
$fileFieldOptions = array('required' => false, 'label' => 'Logo');
if ($image && ($webPath = $image->getLogoUrl())) {
$fileFieldOptions['help'] = '<img src="'.$webPath.'" class="admin-preview img-rounded img-responsive"/>';
}
$em = $this->modelManager->getEntityManager('PDI\PDOneBundle\Entity\Brand');
$query = $em->createQueryBuilder('b')
->select('b')
->from('PDOneBundle:Brand', 'b')
->where('b.company IS NULL');
$formMapper
->with('Information', array('class' => 'col-md-6'))
->add('name')
->add('division')
->add('inactive')
->add(
'brands',
'sonata_type_model',
array(
'multiple' => true,
'by_reference' => false,
'query' => $query,
)
)
->end()
->with('Logo', array('class' => 'col-md-6'))
->add('file', 'file', $fileFieldOptions)
->end();
}
此处的问题是,当我向brands
分配company
并尝试在我看不到品牌字段后编辑公司时,为什么?我在这里缺少什么?我可以得到一些帮助吗?
以下是图片示例。请注意,当我编辑公司时,我无法看到品牌,但当我编辑品牌时,我可以看到公司:
更新
如在Profiler上显示,这是为了在编辑表单上获得结果而执行的查询:
SELECT
t0. NAME AS name_1,
t0.generic_name AS generic_name_2,
t0.logo_url AS logo_url_3,
t0.description AS description_4,
t0.isi_required AS isi_required_5,
t0.isi_text AS isi_text_6,
t0.isi_pdf_url AS isi_pdf_url_7,
t0.pi_required AS pi_required_8,
t0.pi_text AS pi_text_9,
t0.pi_pdf_url AS pi_pdf_url_10,
t0.inactive AS inactive_11,
t0.template_name AS template_name_12,
t0.id AS id_13,
t0.createdAt AS createdAt_14,
t0.updatedAt AS updatedAt_15,
t0.companies_id AS companies_id_16
FROM
brands t0
WHERE
t0.companies_id = 2
这是来自DB的该查询的输出:
+------------+----------------+------------------------------------------------------------+---------------+----------------+------------+---------------+---------------+-----------+---------------+-------------+------------------+-------+---------------------+---------------------+-----------------+
| name_1 | generic_name_2 | logo_url_3 | description_4 | isi_required_5 | isi_text_6 | isi_pdf_url_7 | pi_required_8 | pi_text_9 | pi_pdf_url_10 | inactive_11 | template_name_12 | id_13 | createdAt_14 | updatedAt_15 | companies_id_16 |
+------------+----------------+------------------------------------------------------------+---------------+----------------+------------+---------------+---------------+-----------+---------------+-------------+------------------+-------+---------------------+---------------------+-----------------+
| Brand Test | | https://someurl.com/no_image_available.png | | 0 | NULL | NULL | 0 | NULL | NULL | 0 | NULL | 2 | 2015-10-22 10:45:14 | 2015-10-26 08:04:53 | 2 |
+------------+----------------+------------------------------------------------------------+---------------+----------------+------------+---------------+---------------+-----------+---------------+-------------+------------------+-------+---------------------+---------------------+-----------------+
因此当前公司(我正在编辑的记录)和品牌之间存在关系,为什么不在编辑表单上显示?
更新:实体
以下是Company
和Brands
的实体定义:
/**
* @ORM\Entity
* @ORM\Table(name="companies", options={"collate"="utf8_general_ci"})
* @ORM\HasLifecycleCallbacks()
*/
class Company
{
use IdentifierAutogeneratedTrait;
use TimestampableEntity;
use UploadTrait;
/**
* @var string
* @ORM\Column(type="string", length=45)
* @Assert\NotBlank()
*/
protected $name;
/**
* @var string
* @ORM\Column(type="string", length=255, nullable=true)
*/
protected $logo_url = 'https://someurl.com/no_image_available.png';
/**
* @var string
* @ORM\Column(type="string", length=45, nullable=true)
*/
protected $division;
/**
* @var bool
* @ORM\Column(type="boolean")
*/
protected $inactive = false;
/**
* @var Brand
* @ORM\OneToMany(targetEntity="Brand", mappedBy="company", cascade={"persist"})
**/
protected $brands;
public function __construct()
{
$this->brands = new ArrayCollection();
}
...
}
/**
* @ORM\Entity
* @ORM\Table(name="brands")
* @ORM\Entity(repositoryClass="PDI\PDOneBundle\Entity\Repository\BrandRepository")
* @ORM\HasLifecycleCallbacks()
*/
class Brand
{
use IdentifierAutogeneratedTrait;
use TimestampableEntity;
use UploadTrait;
/**
* @var string
* @ORM\Column(type="string", length=45)
* @Assert\NotBlank()
*/
protected $name;
/**
* @var string
* @ORM\Column(type="string", length=45)
* @Assert\NotBlank()
*/
protected $generic_name;
/**
* @var string
* @ORM\Column(type="string", length=255, nullable=true)
*/
protected $logo_url = 'https://someurl.com/no_image_available.png';
/**
* @var string
* @ORM\Column(type="text")
* @Assert\NotBlank()
*/
protected $description;
/**
* @var bool
* @ORM\Column(type="boolean", nullable=true)
*/
protected $isi_required;
/**
* @var string
* @ORM\Column(type="text", nullable=true)
*/
protected $isi_text;
/**
* @var string
* @ORM\Column(type="string", length=255, nullable=true)
*/
protected $isi_pdf_url;
/**
* @var bool
* @ORM\Column(type="boolean", nullable=true)
*/
protected $pi_required;
/**
* @var string
* @ORM\Column(type="text", nullable=true)
*/
protected $pi_text;
/**
* @var string
* @ORM\Column(type="string", length=255, nullable=true)
*/
protected $pi_pdf_url;
/**
* @var bool
* @ORM\Column(type="boolean")
*/
protected $inactive = false;
/**
* @var string
* @ORM\Column(type="string", length=45, nullable=true)
*/
protected $template_name;
/**
* @var Company
* @ORM\ManyToOne(targetEntity="Company", inversedBy="brands")
* @ORM\JoinColumn(name="companies_id", referencedColumnName="id", nullable=true)
*/
protected $company;
/**
* @ORM\OneToMany(targetEntity="TargetBrand" , mappedBy="brand", cascade={"persist"}, orphanRemoval=true)
* */
protected $targetBrand;
/**
* @ORM\OneToMany(targetEntity="TerritoryBrand" , mappedBy="brand", cascade={"persist"}, orphanRemoval=true)
* */
protected $territoryBrand;
public function __construct()
{
$this->targetBrand = new ArrayCollection();
$this->territoryBrand = new ArrayCollection();
}
...
}
答案 0 :(得分:1)
如果我每次加载表单时都理解正确,那么您在brands
字段中填写查询。此查询选择没有公司的品牌。因此,当您尝试编辑某个公司时,该字段将填充此查询。您在编辑时应该进行不同的查询,可以使用$this->getSubject();
获取对象并检查它是否是新的。
示例解决方案:
$obj = $this->getSubject()->getId();
if(empty($obj)) {
$query = $em->createQueryBuilder('b')
->select('b')
->from('PDOneBundle:Brand', 'b')
->where('b.company IS NULL');
} else {
$query = $em->createQueryBuilder('b')
->select('b')
->from('PDOneBundle:Brand', 'b')
->where('b.company == :company_id');
$query->setParameter('company_id', $obj->getCompany());
}