我在admin类中有以下回调代码,
<?php
namespace IFI2\MainProjectBundle\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 CobrandAdmin extends Admin
{
/**
* @param DatagridMapper $datagridMapper
*/
protected function configureDatagridFilters(DatagridMapper $datagridMapper)
{
$datagridMapper
->add('created')
->add('updated')
->add('name')
->add('code')
->add('cobrandedProductsOnly')
;
}
/**
* @param ListMapper $listMapper
*/
protected function configureListFields(ListMapper $listMapper)
{
$listMapper
->add('created')
->add('updated')
->add('name')
->add('code')
->add('cobrandedProductsOnly')
->add("productPrices")
->add('_action', 'actions', array(
'actions' => array(
'show' => array(),
'edit' => array(),
'delete' => array(),
)
))
;
}
/**
* @param FormMapper $formMapper
*/
protected function configureFormFields(FormMapper $formMapper)
{
$id = $this->id($this->getSubject());
if ($id = $this->id($this->getSubject())) {
$formMapper
->add('created')
->add('updated')
->add('name')
->add('code')
->add('cobrandedProductsOnly')
->add('productPrices','entity', array(
'class' => 'IFI2\MainProjectBundle\Entity\ProductPrice',
'multiple' => true,
'required' => false,
'query_builder' => function (\Doctrine\ORM\EntityRepository $er) use ($id)
{
return $er
->createQueryBuilder('pp')
->where('pp.cobrand is null or pp.cobrand = :id')
->setParameter('id',$id);
}
));
}
else {
$formMapper
->add('created')
->add('updated')
->add('name')
->add('code')
->add('cobrandedProductsOnly')
->add('file', 'file', array('label' => 'Logo'))
->add('productPrices','entity', array(
'class' => 'IFI2\MainProjectBundle\Entity\ProductPrice',
'multiple' => true,
'required' => false,
'query_builder' => function (\Doctrine\ORM\EntityRepository $er)
{
return $er
->createQueryBuilder('pp')
->where('pp.cobrand is null');
}
))
;
}
}
/**
* @param ShowMapper $showMapper
*/
protected function configureShowFields(ShowMapper $showMapper)
{
$showMapper
->add('created')
->add('updated')
->add('name')
->add('code')
->add('productPrices')
;
}
/**
* LifeCycle Callback Events
*/
public function prePersist($cobrand) {
foreach ($cobrand->getProductPrices() as $proPrice) {
$proPrice->setCobrand($cobrand);
}
// $cobrand->emptyProductPrice();
$basepath = $this->getRequest()->getBasePath();
$cobrand->preUpload($basepath);
}
public function postPersist($cobrand) {
$this->saveFile($cobrand);
}
public function preUpdate($cobrand) {
**$productPrice = $this->getDoctrine()
->getRepository('IFI2MainProjectBundle:ProductPrice')
->findByCobrand($cobrandId);**
foreach ($cobrand->getProductPrices() as $proPrice) {
$proPrice->setCobrand($cobrand);
}
}
public function preRemove($cobrand) {
foreach ($cobrand->getProductPrices() as $proPrice) {
$proPrice->setCobrand(null);
}
}
public function saveFile($cobrand) {
$basepath = $this->getRequest()->getBasePath();
$cobrand->upload($basepath);
}
}
在函数 preUpdate 中,我正在尝试访问getDoctrine,但它给出了一个错误,表明该函数不可用。
任何人都可以帮帮我吗?
谢谢, 费萨尔纳西尔
答案 0 :(得分:2)
你从Doctrine到底需要什么?如果你只需要 EntityManager @tyko是对的,你可以这样做:
protected function configureDatagridFilters(DatagridMapper $datagridMapper)
{
$builder = $this->getModelManager()->getEntityManager('MyBundle\Entity\Company')->createQueryBuilder();
$companyResults = $builder->select('PARTIAL c.{id, name}')
->from("MyBundle\Entity\Company", 'c')
->getQuery()
->getArrayResult();
$companyChoices = array();
foreach ($companyResults as $row) {
$companyChoices[$row['id']] = $row['name'];
}
$datagridMapper
->add('company', 'doctrine_orm_choice', array(),
'choice',
array('choices' => $companyChoices)
)
;
}
答案 1 :(得分:1)
尝试$ this-&gt; getModelManager()并查看您可以通过此对象获得的内容。
答案 2 :(得分:0)
你可以这样做:
$choides[$company_id] = $company_name;
//....
->add('company', 'doctrine_orm_choice', array(
'field_options' => array(
'choices' => $choices,
'required' => false,
'multiple' => true,
'expanded' => false,
),
'field_type' => 'choice',
))
答案 3 :(得分:0)
@doctrine
或@doctrine.orm.entity_manager
,并在preUpdate
$this->getModelManager()
,Sonata\DoctrineORMAdminBundle\Model\ModelManager
或Doctrine\ORM\EntityRepository
,以便update
,create
或delete
与您的管理类相关的对象最后你可以做一个非常脏的工作:
$ doctrine = $ this-&gt; getConfigurationPool() - &gt; getContainer() - &gt; get(&#39; doctrine&#39;);
//或
$ em = $ this-&gt; getConfigurationPool() - &gt; getContainer() - &gt; get(&#39; doctrine.orm.entity_manager&#39;);