2012-09-19 54 views
0

我有一個用戶類,它具有一些字段的自定義驗證。當我提交表單我收到以下錯誤:Symfony2:使用自定義約束進行驗證會拋出異常

Warning: class_parents() [function.class-parents]: Class does not exist and could not be loaded in ..../vendor/doctrine/lib/Doctrine/ORM/Mapping/ClassMetadataFactory.php line 223

誰能告訴我什麼,我做錯了什麼?

這裏是我的用戶等級:

<?php 
namespace MyBundle\Entity; 
use Doctrine\ORM\Mapping as ORM; 
use FOS\UserBundle\Entity\User as BaseUser; 
use Symfony\Component\Validator\Constraints as Assert; 
use MyBundle\Validation\Constraint as MyAssert; 


/** 
* @ORM\Entity 
* @ORM\HasLifecycleCallbacks 
*/ 
class User extends BaseUser 
{ 

    /** 
    * @ORM\Id 
    * @ORM\Column(type="integer") 
    * @ORM\generatedValue(strategy="AUTO") 
    */ 
    protected $id; 

    /** 
    * Get id 
    * 
    * @return integer $id 
    */ 
    public function getId() 
    { 
     return $this->id; 
    } 

    /** 
    * @var string $email 
    * @Assert\NotBlank(groups={"registration"}) 
    * @Assert\Email(groups={"registration"}) 
    * @MyAssert\Unique(groups={"registration"}) 
    */ 
    protected $email; 
    .... 

堆棧跟蹤:

in /myApp/vendor/symfony/src/Symfony/Component/HttpKernel/Debug/ErrorHandler.php at line 65  
at ErrorHandler ->handle ('2', 'class_parents() [<a href='function.class-parents'>function.class-parents</a>]: Class does not exist and could not be loaded', '/myApp/vendor/doctrine/lib/Doctrine/ORM/Mapping/ClassMetadataFactory.php', '223', array('name' => '', 'parentClasses' => array())) 
at class_parents ('') 
in /myApp/vendor/doctrine/lib/Doctrine/ORM/Mapping/ClassMetadataFactory.php at line 223  
at ClassMetadataFactory ->getParentClasses ('') 
in /myApp/vendor/doctrine/lib/Doctrine/ORM/Mapping/ClassMetadataFactory.php at line 246  
at ClassMetadataFactory ->loadMetadata ('') 
in /myApp/vendor/doctrine/lib/Doctrine/ORM/Mapping/ClassMetadataFactory.php at line 170  
at ClassMetadataFactory ->getMetadataFor ('') 
in /myApp/vendor/doctrine/lib/Doctrine/ORM/EntityManager.php at line 257  
at EntityManager ->getClassMetadata ('') 
in /myApp/vendor/doctrine/lib/Doctrine/ORM/EntityManager.php at line 574  
at EntityManager ->getRepository (null) 
in /myApp/src/comp/MyAppBundle/Validation/Constraint/UniqueValidator.php at line 21  
at UniqueValidator ->isValid ('asdfasdf', object(Unique)) 
in /myApp/vendor/symfony/src/Symfony/Component/Validator/GraphWalker.php at line 176  
at GraphWalker ->walkConstraint (object(Unique), 'asdfasdf', 'registration', 'data.email') 
in /myApp/vendor/symfony/src/Symfony/Component/Validator/GraphWalker.php at line 136  
at GraphWalker ->walkMember (object(PropertyMetadata), 'asdfasdf', 'registration', 'data.email', null) 
in /myApp/vendor/symfony/src/Symfony/Component/Validator/GraphWalker.php at line 119  
at GraphWalker ->walkProperty (object(ClassMetadata), 'email', object(User), 'registration', 'data.email', null) 
in /myApp/vendor/symfony/src/Symfony/Component/Validator/GraphWalker.php at line 111  
at GraphWalker ->walkObjectForGroup (object(ClassMetadata), object(User), 'registration', 'data') 
in /myApp/vendor/symfony/src/Symfony/Component/Validator/GraphWalker.php at line 82  
at GraphWalker ->walkObject (object(ClassMetadata), object(User), 'registration', 'data') 
in /myApp/vendor/symfony/src/Symfony/Component/Validator/GraphWalker.php at line 162  
at GraphWalker ->walkReference (object(User), 'registration', 'data', true) 
in /myApp/vendor/symfony/src/Symfony/Component/Form/Extension/Validator/Validator/DelegatingValidator.php at line 125  
at DelegatingValidator ::validateFormData (object(Form), object(ExecutionContext)) 
at call_user_func (array('Symfony\Component\Form\Extension\Validator\Validator\DelegatingValidator', 'validateFormData'), object(Form), object(ExecutionContext)) 
in /myApp/vendor/symfony/src/Symfony/Component/Validator/Constraints/CallbackValidator.php at line 65  
at CallbackValidator ->isValid (object(Form), object(Callback)) 
in /myApp/vendor/symfony/src/Symfony/Component/Validator/GraphWalker.php at line 176  
at GraphWalker ->walkConstraint (object(Callback), object(Form), 'Default', '') 
in /myApp/vendor/symfony/src/Symfony/Component/Validator/GraphWalker.php at line 104  
at GraphWalker ->walkObjectForGroup (object(ClassMetadata), object(Form), 'Default', '') 
in /myApp/vendor/symfony/src/Symfony/Component/Validator/GraphWalker.php at line 82  
at GraphWalker ->walkObject (object(ClassMetadata), object(Form), 'Default', '') 
in /myApp/vendor/symfony/src/Symfony/Component/Validator/Validator.php at line 62  
at Symfony\Component\Validator\{closure} (object(GraphWalker), 'Default') 
in /myApp/vendor/symfony/src/Symfony/Component/Validator/Validator.php at line 120  
at Validator ->validateGraph (object(Form), object(Closure), null) 
in /myApp/vendor/symfony/src/Symfony/Component/Validator/Validator.php at line 65  
at Validator ->validate (object(Form)) 
in /myApp/vendor/symfony/src/Symfony/Component/Form/Extension/Validator/Validator/DelegatingValidator.php at line 79  
at DelegatingValidator ->validate (object(Form)) 
in /myApp/vendor/symfony/src/Symfony/Component/Form/Form.php at line 556  
at Form ->bind (array('firstname' => '', 'lastname' => '', 'username' => '', 'email' => 'asdfasdf', 'plainPassword' => array('Passwort' => '', 'Passwort wiederholen' => ''), 'code' => '', 'plan' => '10', 'phone' => '', 'street' => '', 'zip' => '', 'city' => '', 'country' => '1')) 
in /myApp/vendor/symfony/src/Symfony/Component/Form/Form.php at line 592  
at Form ->bindRequest (object(Request)) 
in /myApp/src/comp/MyAppBundle/Controller/IndexController.php at line 151  
at IndexController ->registerAction() 
at call_user_func_array (array(object(IndexController), 'registerAction'), array()) 
in kernel.root_dir/cache/dev_local/classes.php at line 3905  
at HttpKernel ->handleRaw (object(Request), '1') 
in kernel.root_dir/cache/dev_local/classes.php at line 3875  
at HttpKernel ->handle (object(Request), '1', true) 
in kernel.root_dir/cache/dev_local/classes.php at line 4879  
at HttpKernel ->handle (object(Request), '1', true) 
in kernel.root_dir/bootstrap.php.cache at line 547  
at Kernel ->handle (object(Request)) 
in /myApp/web/app_dev_local.php at line 10  

回答

1
at EntityManager ->getRepository (null) 
in /myApp/src/comp/MyAppBundle/Validation/Constraint/UniqueValidator.php at line 21  
at UniqueValidator ->isValid ('asdfasdf', object(Unique)) 

錯誤的原因是EntityManager-> getRepository()被調用空值。看起來你的UniqueValidator.php中存在一些問題 如果沒有這個文件,不能準確地告訴問題是什麼。