0

我在管理类中有以下回调代码,

<?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

4 回答 4

2

你到底需要什么教义?如果您只需要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)
        )
    ;
}
于 2014-09-11T13:00:41.040 回答
1

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

于 2013-11-08T12:26:45.190 回答
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',
))
于 2015-11-24T17:57:18.537 回答
0
  1. 您可以注入@doctrine@doctrine.orm.entity_manager在您的管理服务中并在您的preUpdate
  2. 正如@tyko所提到的,您可以调用$this->getModelManager()它将返回一个Sonata\DoctrineORMAdminBundle\Model\ModelManager或与Doctrine\ORM\EntityRepository您一样updatecreate或者delete与您的管理类相关的对象
  3. 您可以按照 Sonata Admin 图片上传配方http://symfony.com/doc/current/bundles/SonataAdminBundle/cookbook/recipe_file_uploads.html
  4. 最后你可以做一个非常肮脏的工作调用:

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

    //或者

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

于 2016-11-08T17:25:37.710 回答