1

我正在 Symfony 用户 Varspool/WebsocketBundle 中做我的第一个 WebSocket 应用程序。我需要为投票服务器端插入数据。为此,我制作了一个控制器,当用户选择投票答案时,它会在数据库中插入数据。

遵循此文档:http ://symfony.com/doc/2.0/cookbook/controller/service.html我有一个“调用未定义的方法 forward()”错误。应该是因为我在应用程序中而不是在控制器中

我看到很多事情,比如在控制器中重新启动内核,还有很多其他奇怪的事情,比如:

$kernel = new \AppKernel('dev', true);
$kernel->loadClassCache();
$kernel->boot();
$controller->setContainer($kernel->getContainer()); 

[编辑] 我向您展示了我最后一次尝试显示“对非对象上的成员函数 has() 的调用”(我不太了解 oO)

我的控制器即服务

Edit1 - 更新 __construct

use Doctrine\ORM\EntityManager;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Krum\AGBundle\Application\ExempleApplication;
use Krum\AGBundle\Form\AGVoteSetType;
use Krum\AGBundle\Form\AGVoteAffichType;
use Krum\AGBundle\Form\AGVoteType;
use Krum\AGBundle\Entity\AGDummy;
use Krum\AGBundle\Entity\AGVote;
use Krum\AGBundle\Entity\AGReponse;
use Krum\AGBundle\Entity\AGScrutin;
use Krum\KBundle\Entity\Users;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

use Symfony\Component\DependencyInjection\ContainerInterface;


class AGServicesController extends Controller {

    protected $em;    

    public function __construct($entityManager)
    {
        $this->entityManager = $entityManager;
    }




    public function get($service){           
        return $this->container->get($service);
    }

   public function voteeffective($vote, $reponse, $users) {
       $em = $this->em;
       $userbyFOS = $em->createQueryBuilder('u')
              ->select('u')
              ->from('KrumKBundle:Users', 'u')
              ->leftJoin('u.utilisateur', 'ut')
              ->where('ut.id = ?1')
              ->setParameter(1, $users)
              ->getQuery();
      $user = $userbyFOS->getOneOrNullResult();      
      $userstatus = $em->createQueryBuilder('scru')
              ->select('scru')
              ->from('KrumAGBundle:AGScrutin', 'scru')
              ->where('scru.users = ?1')
              ->andWhere('scru.AGVote = ?2')
              ->andWhere('scru.AGReponse = ?3')
              ->setParameters(array(
                      1 => $user,
                      2 => $vote,
                      3 => $reponse
                      ))
              ->getQuery();
       $stat = $userstatus->getResult();

      if ($stat == NULL) {
         $votety = $em->getRepository('KrumAGBundle:AGVote')->findOneById($vote);
         $reponsety = $em->getRepository('KrumAGBundle:AGReponse')->findOneById($reponse);

         $scrutinty = new AGScrutin();
         $scrutinty->setAGReponse($reponsety);
         $scrutinty->setAGVote($votety);
         $scrutinty->setUsers($user);
         $scrutinty->setType("AGScrutin");
         $em->persist($scrutinty);
         $em->flush();

         return "vote-ok:".$scrutinty->getId();
      } else {
         return "vote-false";
      }
    }  

}

我的应用程序(WebSocket 服务器)

namespace Krum\AGBundle\Application;

use Varspool\WebsocketBundle\Application\Application;
use Varspool\WebsocketBundle\Application\NamedApplication;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use  Krum\AGBundle\Controller\AGServicesController;


class ExempleApplication extends Application {

   protected $clients = array();


   public function onData($data, $client) {
      foreach ($this->clients as $sendto) {
         $scrutin = (json_decode($data) != NULL) ? true : false;
             if ($scrutin == true ){   
                 $vote = $scrutin["AGVote"];
                 $reponse = $scrutin["AFreponse"] ;
                 $users = $scrutin["users"];
             $scrutinservice = new AGServicesController();
             $scrutinsend = $controller->voteeffective($vote, $reponse, $users);

             $sendto->send($scrutinsend);
              }
            }
     else
        $sendto->send($data);

   }
 }
}

我的 config.yml

parameters:
    agservices.class:      Krum\AGBundle\Controller\AGServicesController

services :
  agservices:
    class : %agservices.class%
    arguments :
      container : "@service_container"

服务.yml

编辑 1:serices.yml 配置

krum.agservices:
    class: Krum\AGBundle\Controller\AGServicesController    
    arguments: ["@doctrine.orm.default_entity_manager"]

所有的答案是或想法是可观的!

谢谢你的收看;)

**编辑 1:现在,我有另一个 ExceptionError

警告:缺少 Krum\AGBundle\Controller\AGServicesController::__construct() 的参数 1,在第 62 行的 /opt/lampp/htdocs/Krum/src/Krum/AGBundle/Application/ExempleApplication.php 中调用并在 /opt/ 中定义lampp/htdocs/Krum/src/Krum/AGBundle/Controller/AGServicesController.php 第 34 行 *

我认为 services.yml 应该自动添加正确的学说实体管理器的参数,或者它只是指定参数的类型,我必须在调用控制器时手动添加它?

4

0 回答 0