列 "brands" 没有出现在编辑表单中,为什么?
Column "brands" does not apper on the edit form, why?
我有一个基于 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();
}
...
}
如果我在每次加载表单时都理解正确,那么您就是在 brands
字段中填写查询。此查询 select 个拥有 none 个公司的品牌。因此,当您尝试编辑某个公司时,该字段会填满此查询。你应该在编辑的时候做一个不同的查询,你可以用$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());
}
我有一个基于 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();
}
...
}
如果我在每次加载表单时都理解正确,那么您就是在 brands
字段中填写查询。此查询 select 个拥有 none 个公司的品牌。因此,当您尝试编辑某个公司时,该字段会填满此查询。你应该在编辑的时候做一个不同的查询,你可以用$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());
}