如何在回调Symfony2中访问getDoctrine

时间:2013-11-08 10:41:36

标签: symfony symfony-2.3

我在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,但它给出了一个错误,表明该函数不可用。

任何人都可以帮帮我吗?

谢谢, 费萨尔纳西尔

4 个答案:

答案 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)

  1. 您可以在管理服务中注入@doctrine@doctrine.orm.entity_manager,并在preUpdate
  2. 中使用它
  3. 正如@tyko所述,您可以致电$this->getModelManager()Sonata\DoctrineORMAdminBundle\Model\ModelManagerDoctrine\ORM\EntityRepository,以便updatecreatedelete与您的管理类相关的对象
  4. 您可以按照Sonata Admin图片上传方法http://symfony.com/doc/current/bundles/SonataAdminBundle/cookbook/recipe_file_uploads.html
  5. 进行操作
  6. 最后你可以做一个非常脏的工作:

    $ 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;);