【问题标题】:Can't call parent class method from extended class无法从扩展类调用父类方法
【发布时间】:2019-09-10 06:37:24
【问题描述】:

我使PlaceInfoAdminController 扩展自Sonata\AdminBundle\Controller\CRUDController

use Sonata\AdminBundle\Controller\CRUDController;

class PlaceInfoAdminController extends CRUDController
{

    /**
    * Edit action.
    *
    * @param int|string|null $id
    *
    * @throws NotFoundHttpException If the object does not exist
    * @throws \RuntimeException     If no editable field is defined
    * @throws AccessDeniedException If access is not granted
    *
    * @return Response|RedirectResponse
    */
   public function editAction($id = null)
   {
       $request = $this->getRequest();
       // the key used to lookup the template
       $templateKey = 'edit';

       $id = $request->get($this->admin->getIdParameter());
       $existingObject = $this->admin->getObject($id);

       if (!$existingObject) {
            throw $this->createNotFoundException(sprintf('unable to find the object with id: %s', $id));
       }

       $this->checkParentChildAssociation($request, $existingObject); // calling parent's method

当我调用父类方法时,出现错误。

LogicException:
Call to undefined method Sonata\AdminBundle\Controller\CRUDController::checkParentChildAssociation

  at vendor/sonata-project/admin-bundle/src/Controller/CRUDController.php:89
  at Sonata\AdminBundle\Controller\CRUDController->__call('checkParentChildAssociation', array(object(Request), object(PlaceInfo)))
     (src/Controller/PlaceInfoAdminController.php:35)
  at App\Controller\PlaceInfoAdminController->editAction('19')
     (vendor/symfony/symfony/src/Symfony/Component/HttpKernel/HttpKernel.php:151)
  at Symfony\Component\HttpKernel\HttpKernel->handleRaw(object(Request), 1)
     (vendor/symfony/symfony/src/Symfony/Component/HttpKernel/HttpKernel.php:68)
  at Symfony\Component\HttpKernel\HttpKernel->handle(object(Request), 1, true)
     (vendor/symfony/symfony/src/Symfony/Component/HttpKernel/Kernel.php:200)
  at Symfony\Component\HttpKernel\Kernel->handle(object(Request))
     (web/app_dev.php:28)

从子类调用父类的checkParentChildAssociation()会出现这个错误,但是父类中有函数checkParentChildAssociation()

怎么了??

【问题讨论】:

标签: php symfony symfony4


【解决方案1】:

如果您没有覆盖该方法,则需要使用 parent 关键字来调用它。

parent::checkParentChildAssociation($request, $existingObject);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-09
    • 1970-01-01
    • 2012-02-11
    • 1970-01-01
    相关资源
    最近更新 更多