如何在回调Symfony2中访问getDoctrine

如何在回调Symfony2中访问getDoctrine,symfony,symfony-2.3,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\Adm

我在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);
    }
}

试试$this->getModelManager()看看你能通过这个对象得到什么。

试试$this->getModelManager()看看你能通过这个对象得到什么。

试试$this->getModelManager()看看你能通过这个对象得到什么。

试试$this->getModelManager()看看你能从这个物体中得到什么。

你到底需要从教义中得到什么?如果您只是需要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)
        )
    ;
}

你到底需要从教义中得到什么?如果您只是需要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)
        )
    ;
}

你到底需要从教义中得到什么?如果您只是需要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)
        )
    ;
}

你到底需要从教义中得到什么?如果您只是需要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)
        )
    ;
}
你可以做:

$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',
))
你可以做:

$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',
))
你可以做:

$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',
))
你可以做:

$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',
))
  • 您可以在您的管理服务中插入
    @doctor
    @doctor.orm.entity\u manager
    ,并在您的
    预更新中使用它
  • 如前所述,您可以调用
    $this->getModelManager()
    ,它将返回一个
    Sonata\DoctrineORMAdminBundle\Model\ModelManager
    或与
    Doctrine\ORM\EntityRepository\code>相同的
    命令,以便
    更新
    创建
    删除
    与您的管理类相关的对象
  • 您可以按照索纳塔管理图像上传食谱
  • 最后,你可以做一个非常肮脏的工作,叫:

    $doctrine=$this->getConfigurationPool()->getContainer()->get('doctrine')

    //或

    $em=$this->getConfigurationPool()->getContainer()->get('doctrine.orm.entity_manager')

  • 您可以在您的管理服务中插入
    @doctor
    @doctor.orm.entity\u manager
    ,并在您的
    预更新中使用它
  • 如前所述,您可以调用
    $this->getModelManager()
    ,它将返回一个
    Sonata\DoctrineORMAdminBundle\Model\ModelManager
    或与
    Doctrine\ORM\EntityRepository\code>相同的
    命令,以便
    更新
    创建
    删除
    与您的管理类相关的对象
  • 您可以按照索纳塔管理图像上传食谱
  • 最后,你可以做一个非常肮脏的工作,叫:

    $doctrine=$this->getConfigurationPool()->getContainer()->get('doctrine')

    //或

    $em=$this->getConfigurationPool()->getContainer()->get('doctrine.orm.entity_manager')

  • 您可以在您的管理服务中插入
    @doctor
    @doctor.orm.entity\u manager
    ,并在您的
    预更新中使用它
  • 如前所述,您可以调用
    $this->getModelManager()
    ,它将返回一个
    Sonata\DoctrineORMAdminBundle\Model\ModelManager
    或与
    Doctrine\ORM\EntityRepository\code>相同的
    命令,以便
    更新
    创建
    删除
    与您的管理类相关的对象
  • 您可以按照索纳塔管理图像上传食谱
  • 最后,你可以做一个非常肮脏的工作,叫:

    $doctrine=$this->getConfigurationPool()->getContainer()->get('doctrine')

    //或

    $em=$this->getConfigurationPool()->getContainer()->get('doctrine.orm.entity_manager')

  • 您可以在您的管理服务中插入
    @doctor
    @doctor.orm.entity\u manager
    ,并在您的
    预更新中使用它
  • 如前所述,您可以调用
    $this->getModelManager()
    ,它将返回一个
    Sonata\DoctrineORMAdminBundle\Model\ModelManager
    或与
    Doctrine\ORM\EntityRepository\code>相同的
    命令,以便
    更新
    创建
    删除
    与您的管理类相关的对象
  • 您可以按照索纳塔管理图像上传食谱
  • 最后,你可以做一个非常肮脏的工作,叫:

    $doctrine=$this->getConfigurationPool()->getContainer()->get('doctrine')

    //或

    $em=$this->getConfigurationPool()->getContainer()->get('doctrine.orm.entity_manager')


  • 事实上,我在任何地方都没有看到这方面的记录。还有其他人吗?这里刚刚提到:-我会密切关注它,并为docs recipes improvement添加PR。问题已揭晓,PR将跟进:事实上,我在任何地方都没有看到此文档。还有其他人吗?这里刚刚提到:-我会密切关注它,并为docs recipes improvement添加PR。问题已揭晓,PR将跟进:事实上,我在任何地方都没有看到此文档。还有其他人吗?这里刚刚提到:-我会密切关注它,并为docs recipes improvement添加PR。问题已揭晓,PR将跟进:事实上,我在任何地方都没有看到此文档。还有其他人吗?这里刚刚提到:-我将密切关注它,并添加一个文档配方改进的PR。问题已打开,PR如下: