在null上调用成员函数

时间:2017-11-01 11:19:40

标签: php symfony

我创建服务以添加formType然后持久化对象,并在控制器中调用数据,但我在下面的图像中显示错误:

在控制器中扩展类abstractController内容getHandler,我有查看newskill.html.twig

enter image description here

Code SkillController.php:

<?php 

    namespace AppBundle\Controller\Condidate;

    use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
    use Symfony\Bundle\FrameworkBundle\Controller\Controller;
    use Symfony\Component\HttpFoundation\Request;
    use AppBundle\Entity\Skill;
    use AppBundle\Controller\AbstractController;
    use AppBundle\Form\SkillType;

    /**
     *Class SkillController.
     */
    class SkillController extends AbstractController
    {

         /**
         *function handler.
         */
         protected function getHandler(){
            //var_dump('test');
        }

        /**
         *function addSkill
         * @param Request $request
         * @return \Symfony\Component\Form\Form The form
         */
        public function addSkillAction(Request $request) {
             $skill = $this->getHandler()->post();

             if ($skill instanceof \AppBundle\Entity\Skill) {
                return $this->redirectToRoute('ajouter_info');

        }

           return $this->render('skills/newskill.html.twig', array(
            'form' => $form->createView(),));

        }


        }

Code SkillHandler.php:

<?php 

namespace AppBundle\Handler;

use AppBundle\Handler\HandlerInterface;
use Symfony\Component\HttpFoundation\Request;
use AppBundle\Entity\Skill;
use Doctrine\ORM\EntityManager;
use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\Form\formFactory;

/**
 * SkillHandler.
 */
class SkillHandler implements HandlerInterface {

    /**
     *
     * @var EntityManager 
     */
    protected $em;

    /**
     *
     * @var formFactory 
     */
    private $formFactory;


    /**
     *function construct.
     */
    public function __construct(EntityManager $entityManager, formFactory $formFactory)
        {
            $this->em = $entityManager;
            $this->formFactory = $formFactory;
        }

     /**
       *function post
       */
    public function post(array $parameters, array $options = []) {
        $form = $this->formFactory->create(\AppBundle\Form\SkillType::class, $object, $options);
        $form->submit($parameters);
        if ($form->isValid()) {
            $skill = $form->getData();
            $this->persistAndFlush($skill);
            return $skill;
        }

        return $form->getData();
    }

    /**
     *function persisteAndFlush
     */

    protected function persistAndFlush($object) {
       $this->em->persist($object);
       $this->em->flush();
    }


    /**
     *function get
     */
     public function get($id){
        throw new \DomainException('Method SkillHandler::get not implemented');

     }

     /**
      *function all
      */
      public function all($limit = 10, $offset = 0){
        throw new \DomainException('Method SkillHandler::all not implemented');
      }


    /**
     *function put
     */
    public function put($resource, array $parameters, array $options){
        throw new \DomainException('Method SkillHandler::put not implemented');
    }

    /**
     *function patch
     */
    public function patch($resource, array $parameters, array $options){
        throw new \DomainException('Method SkillHandler::patch not implemented');
    }

    /**
     *function delete
     */
     public function delete($resource){
        throw new \DomainException('Method SkillHandler::delete not implemented');
         }
}

code services.yml:

skill_add:
    class: AppBundle\Handler\SkillHandler
    arguments: 
        - "@doctrine.orm.entity_manager"
        - "@form.factory"
    public: true

任何帮助都将不胜感激。

2 个答案:

答案 0 :(得分:3)

您的$this->getHandler()重新开始null

解决方案可以检查$this->getHandler()是否首先不返回null

if (!$this->getHandler()) {
    throw new \Exception(sprintf('Handler cannot be null')
} else {
    $skill = $this->getHandler()->post();
}

答案 1 :(得分:0)

试试这个,首先你应该把你的处理程序带到你的Controller的getHandler()方法。

protected function getHandler(){
    return $this->get('skill_add');
}