Php 用户问题中的symfony 2.3 call entitymanager类仍未解决

Php 用户问题中的symfony 2.3 call entitymanager类仍未解决,php,symfony,doctrine-orm,Php,Symfony,Doctrine Orm,问题在于我的上一个问题: 我仍然无法访问subscriber类中的条令存储库类。 我已经遵循了塞浦路斯人DonCallisto建议的步骤 错误 Whoops, looks like something went wrong. 1/1 ContextErrorException: Catchable Fatal Error: Argument 1 passed to Collegelife\CommonBundle\Form\CityType::__construct() must be an

问题在于我的上一个问题:

我仍然无法访问subscriber类中的条令存储库类。 我已经遵循了塞浦路斯人DonCallisto建议的步骤

错误

Whoops, looks like something went wrong.
1/1 ContextErrorException: Catchable Fatal Error: Argument 1 passed to Collegelife\CommonBundle\Form\CityType::__construct() must be an instance of Collegelife\CommonBundle\Form\EventListner\ValidateCountryCitySubscriber, none given, called in /opt/lampp/htdocs/collegelife/src/Collegelife/AdminBundle/Controller/CityController.php on line 19 and defined in /opt/lampp/htdocs/collegelife/src/Collegelife/CommonBundle/Form/CityType.php line 17

in /opt/lampp/htdocs/collegelife/src/Collegelife/CommonBundle/Form/CityType.php line 17
at ErrorHandler->handle('4096', 'Argument 1 passed to Collegelife\CommonBundle\Form\CityType::__construct() must be an instance of Collegelife\CommonBundle\Form\EventListner\ValidateCountryCitySubscriber, none given, called in /opt/lampp/htdocs/collegelife/src/Collegelife/AdminBundle/Controller/CityController.php on line 19 and defined', '/opt/lampp/htdocs/collegelife/src/Collegelife/CommonBundle/Form/CityType.php', '17', array()) in /opt/lampp/htdocs/collegelife/src/Collegelife/CommonBundle/Form/CityType.php line 17
at CityType->__construct() in /opt/lampp/htdocs/collegelife/src/Collegelife/AdminBundle/Controller/CityController.php line 19
at CityController->__processForm(array('entity' => object(City), 'path' => '_admin_city_insert', 'button_label' => 'Add')) in /opt/lampp/htdocs/collegelife/src/Collegelife/AdminBundle/Controller/CityController.php line 53
at CityController->insertAction(object(Request))
at call_user_func_array(array(object(CityController), 'insertAction'), array(object(Request))) in /opt/lampp/htdocs/collegelife/app/bootstrap.php.cache line 2889
at HttpKernel->handleRaw(object(Request), '1') in /opt/lampp/htdocs/collegelife/app/bootstrap.php.cache line 2863
at HttpKernel->handle(object(Request), '1', true) in /opt/lampp/htdocs/collegelife/app/bootstrap.php.cache line 2992
at ContainerAwareHttpKernel->handle(object(Request), '1', true) in /opt/lampp/htdocs/collegelife/app/bootstrap.php.cache line 2272
at Kernel->handle(object(Request)) in /opt/lampp/htdocs/collegelife/web/app_dev.php line 28
我现在的代码:

Citytype.php

<?php namespace Collegelife\CommonBundle\Form;
use Collegelife\CommonBundle\Form\EventListner\ValidateCountryCitySubscriber;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Symfony\Component\Validator\Constraints;
class CityType extends AbstractType
{
// Validate CountryCity Subscriber

protected $validateCountryCitySubscriber;

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


/**
 * @param FormBuilderInterface $builder
 * @param array $options
 */
public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
    ->add("country", "entity", array("class" => "CollegelifeCommonBundle:Country", "property" => "countryname", "empty_value" => "-- Select Country --", "required" => false, "constraints" => new Constraints\NotBlank(array("message" => "Country should not be empty"))))
    ->add("cityname", "text", array("label" => "Name", "required" => false))
    ->add("isactive", "checkbox", array("required" => false, "label" => "Is active?", "mapped" => true))

    //->add('createdat')
    //->add('updatedat')
    ;
    $builder->addEventSubscriber($this->validateCountryCitySubscriber);

    //$builder->addEventSubscriber(new EventListner\ValidateCountryCitySubscriber());

    /* $builder->addValidator(new CallbackValidator(function(FormInterface $form) {
     $country = $form->get("country")->getData();
            if (!$country) {
            $form['country']->addError(new FormError("Country should not be emtpy"));
            }
            }
    )); */
}

/**
 * @param OptionsResolverInterface $resolver
 */
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
    $resolver->setDefaults(array(
            // "data_class" => "Collegelife\CommonBundle\Entity\City",
            "csrf_token" => true,
            "csrf_token_name" => "_token",
            "method" => "POST",
            // to generate unique key token per form
            "intention" => "city",
            "country" => true,
            "mapped" => false,
            'allowExtraFields' => true
            // validation group
            //"validation_groups" => false,
    ));
}

/**
 * @return string
 */
public function getName()
{
    return 'collegelife_commonbundle_city';
}
}?>
<?php 
namespace Collegelife\CommonBundle\Form\EventListner;
use Doctrine\ORM\EntityManager;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\Form\FormError;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Doctrine\ORM\EntityRepository;
use Symfony\Bridge\Doctrine\ManagerRegistry;

class ValidateCountryCitySubscriber implements EventSubscriberInterface
{
public static function getSubscribedEvents()
{
    return array(FormEvents::POST_SUBMIT => 'postSubmit');
}

public function postSubmit(FormEvent $event)
{
    $data = $event->getData();
    $form = $event->getForm();
    $country = $event->getForm()->getData()->country->getId();
    $cityname = $data->getCityname();
    $this->em->getRepository("CommonBundle:CountryCity")->validateCountryCity($country, $city);     
    exit();
    //$form->addError(new FormError("City is already exists for this country"));
}
}?>
<?php 
namespace Collegelife\CommonBundle\Form\EventListner;
use Doctrine\ORM\EntityManager;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\Form\FormError;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Doctrine\ORM\EntityRepository;
use Symfony\Bridge\Doctrine\ManagerRegistry;

class ValidateCountryCitySubscriber implements EventSubscriberInterface
{
public static function getSubscribedEvents()
{
    return array(FormEvents::POST_SUBMIT => 'postSubmit');
}

public function postSubmit(FormEvent $event)
{
    $data = $event->getData();
    $form = $event->getForm();
    $country = $event->getForm()->getData()->country->getId();
    $cityname = $data->getCityname();
    //$this->em->getRepository("CommonBundle:CountryCity")->validateCountryCity($country, $city);
    //var_dump($data);
    //        echo "Query:" . $this->er->createQueryBuilder()->select("cc.id")
    //                ->from("CountryCity", "cc")
    //                ->join("cc", "City", "ci", "cc.cityid = ci.id")
    //                ->getQuery();
    exit();
    //$form->addError(new FormError("City is already exists for this country"));
}
}?>
<?php namespace Collegelife\AdminBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Collegelife\CommonBundle\Form\CountryType;
use Collegelife\CommonBundle\Form\CityType;
use Collegelife\CommonBundle\Entity\Country;
use Collegelife\CommonBundle\Entity\CountryCity;
use Collegelife\CommonBundle\Entity\City;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\JsonResponse;

class CityController extends Controller
{
private function __processForm($params)
{
    $form = $this->createForm(new CityType(), $params["entity"], array("action" => $this->generateUrl($params["path"])));
    if ($params['button_label'] == "Update") {
        $form->add("cid", "hidden");
    }
    $form->add('submit', 'submit', array("label" => $params['button_label'], "attr" => array("class" => "btn btn-success")));
    return $form;
}

public function addAction(Request $request)
{
    if (false === $this->get("security.context")->isGranted("ROLE_ADMIN")) {
        throw new AccessDeniedException("Unable access this page");
    }
    $entity = new City();
    $params = array(
            "entity" => $entity,
            "path" => "_admin_city_insert",
            "button_label" => "Add"
    );
    $addForm = $this->__processForm($params);
    return $this->render("CollegelifeAdminBundle:City:add.html.twig", array("form" => $addForm->createView(), "header_label" => "Add"));
}

public function insertAction(Request $request)
{
    if (false === $this->get("security.context")->isGranted("ROLE_ADMIN")) {
        throw new AccessDeniedException("Unable access this page");
    }
    $entity = new City();
    $params = array(
            "entity" => $entity,
            "path" => "_admin_city_insert",
            "button_label" => "Add"
    );
    $addForm = $this->__processForm($params);
    $addForm->handleRequest($request);
    $req = $request->request->get("collegelife_commonbundle_city");
    if ($addForm->isValid() && $addForm->get("submit")->isClicked()) {
        $req = $request->request->get("collegelife_commonbundle_city");

        $this->getDoctrine()->getManager()->persist($entity);
        $this->getDoctrine()->getManager()->flush();

        //$this->get("session")->getFlashBag()->add("message", "City add successfully");
        //return $this->redirect($this->generateUrl("_admin_country_list"));
    }
    return $this->render("CollegelifeAdminBundle:City:add.html.twig", array("form" => $addForm->createView(), "header_label" => "Add"));
}

public function listAction()
{
    if (false === $this->get("security.context")->isGranted("ROLE_ADMIN")) {
        throw new AccessDeniedException("Unable access this page");
    }
    $entity = $this->getDoctrine()->getRepository("CollegelifeCommonBundle:CountryCity")->findAll();
    return $this->render("CollegelifeAdminBundle:City:list.html.twig", array("entity" => $entity));
}
}?>

你有很多很多问题。恐怕你得花些时间阅读手册,学习如何把东西组合在一起。也许从这里开始:但我真的建议你仔细阅读表格一章并学习示例。感谢亲爱的Cerad的回复,但我之前已经做了2-3次了。我想这会对你有所帮助:这是我在ketan后提出的问题
parameters:
     collegelife_common_citytype_form.class: Collegelife\CommonBundle\Form\CityType

services:
collegelife_validate_countrycity_subscriber:
    class: %collegelife_common_citytype_form.class%
    arguments: ["@doctrine.listner"]
    tags:
      - { name: form.type, alias: Colleglife_commonbundle_citytype }