php - 在 null 上调用成员函数

标签 php symfony

我为添加 formType 创建服务,然后持久化对象并在 Controller 中调用数据,但我在下图中显示了错误:

在 Controller 中,我扩展类 abstractController 内容 getHandler 并且我有 View newskill.html.twig

enter image description here

代码 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(),));

        }


        }

代码 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');
         }
}

代码services.yml:

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

如有任何帮助,我们将不胜感激。

最佳答案

您的 $this->getHandler() 返回 null

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

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

关于php - 在 null 上调用成员函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47053643/

相关文章:

php - cURL Recaptcha 无法正常工作 PHP

php - 生成随机且唯一的字符串

php - 一个查询,一张表,两个计数()

php - (Elasticsearch)将Unix格式的数据转换为时间戳(无需更改映射)

symfony - 将数组传递给 twig 并显示它

php - centos中如何设置PHP访问的环境变量?

symfony - 在哪里放置应用程序的全局实体?

php - 如何让 Symfony2 直接加载 CSS,JS 文件而不是通过 PHP?

php - Controller 没有容器集,是不是忘记定义为服务订阅者了

php - 如何在 Symfony2 中处理具有多对一关系的文件上传?