我在管理类中有以下回调代码,
<?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 但它给了我一个错误,即该函数不可用。
任何人都可以帮我吗?
谢谢,费萨尔·纳西尔