没有找到类'doctrine.orm.validator.unique' [英] Class 'doctrine.orm.validator.unique' not found

查看:265
本文介绍了没有找到类'doctrine.orm.validator.unique'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我不知道这是什么问题,或者这个类甚至如何加载。但是我的模型(或者实际上称为实体)看起来像这样:

So I am not sure what the issue is here, or how this class even gets loaded. But my model (or as their actually called, entity) looks like this:

<?php

namespace ImageUploader\Models;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;

/**
 * @ORM\Entity
 * @ORM\Table(name="users")
 * @UniqueEntity(fields="userName")
 * @UniqueEntity(fields="email")
 */
class User {

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

    /**
     * @ORM\Column(type="string", length=32, nullable=false)
     * @Assert\NotBlank()
     */
    protected $firstName;

    /**
     * @ORM\Column(type="string", length=32, nullable=false)
     * @Assert\NotBlank()
     */
    protected $lastName;

    /**
     * @ORM\Column(type="string", length=100, unique=true, nullable=false)
     * @Assert\NotBlank(
     *    message = "Username cannot be blank"
     * )
     */
    protected $userName;

    /**
     * @ORM\Column(type="string", length=100, unique=true, nullable=false)
     * @Assert\NotBlank()
     * @Assert\Email(
     *    message = "The email you entered is invalid.",
     *    checkMX = true
     * )
     */
    protected $email;

    /**
     * @ORM\Column(type="string", length=500, nullable=false)
     * @Assert\NotBlank(
     *  message = "The password field cannot be empty."
     * )
     */
    protected $password;

    /**
     * @ORM\Column(type="datetime", nullable=true)
     */
    protected $created_at;

    /**
     * @ORM\Column(type="datetime", nullable=true)
     */
    protected $updated_at;
}

在我的我有一个动作叫做 createAction 当用户尝试注册时被调用。它看起来像这样:

In my I have an action called createAction that gets called when a user attempts to sign up. It looks like this:

public static function createAction($params){
  $postParams = $params->request()->post();

  if ($postParams['password'] !== $postParams['repassword']) {
    $flash = new Flash();
    $flash->createFlash('error', 'Your passwords do not match.');
    $params->redirect('/signup/error');
  }

  $user = new User();

  $user->setFirstName($postParams['firstname'])
       ->setLastName($postParams['lastname'])
       ->setUserName($postParams['username'])
       ->setEmail($postParams['email'])
       ->setPassword($postParams['password'])
       ->setCreatedAtTimeStamp();

  $validator = Validator::createValidatorBuilder();
  $validator->enableAnnotationMapping();

  $errors = $validator->getValidator()->validate($user);

  var_dump($errors);
}

当调用此操作时,我会收到以下错误:

When this action is called I get the following error:

Fatal error: Class 'doctrine.orm.validator.unique' not found in /var/www/html/image_upload_app/vendor/symfony/validator/ConstraintValidatorFactory.php on line 47

我不知道如何解决这个问题。我的作曲家档案是这样的:

I am not sure how to resolve this issue. My composer file is as such:

{
  "require": {
    "doctrine/orm": "2.4.*",
    "doctrine/migrations": "1.0.*@dev",
    "symfony/validator": "2.8.*@dev",
    "symfony/doctrine-bridge": "2.8.*@dev",
    "slim/slim": "~2.6",
    "freya/freya-exception": "0.0.7",
    "freya/freya-loader": "0.2.2",
    "freya/freya-templates": "0.1.2",
    "freya/freya-factory": "0.0.8",
    "freya/freya-flash": "0.0.1"
  },
  "autoload": {
    "psr-4": {"": ""}
  }
}

所以我不知道我是否缺少一个包或如果我做错了...

So I am not sure if I am missing a package or if I am doing something wrong ....

我的 bootstrap.php 文件中包含以下内容:

My bootstrap.php file has the following contents in it:

require_once 'vendor/autoload.php';

$loader = require 'vendor/autoload.php';
\Doctrine\Common\Annotations\AnnotationRegistry::registerLoader(array($loader, 'loadClass'));

use Doctrine\ORM\Tools\Setup;
use Doctrine\ORM\EntityManager;

/**
 * Set up Doctrine.
 */
class DoctrineSetup {

    /**
     * @var array $paths - where the entities live.
     */
    protected $paths = array(APP_MODELS);

    /**
     * @var bool $isDevMode - Are we considered "in development."
     */
    protected $isDevMode = false;

    /**
     * @var array $dbParams - The database paramters.
     */
    protected $dbParams = null;

    /**
     * Constructor to set some core values.
     */
    public function __construct(){
        if (!file_exists('db_config.ini')) {
            throw new \Exception(
                'Missing db_config.ini. You can create this from the db_config_sample.ini'
            );
        }

        $this->dbParams = array(
            'driver' => 'pdo_mysql',
            'user' => parse_ini_file('db_config.ini')['DB_USER'],
            'password' => parse_ini_file('db_config.ini')['DB_PASSWORD'],
            'dbname' => parse_ini_file('db_config.ini')['DB_NAME']
        );
    }

    /**
     * Get the entity manager for use through out the app.
     *
     * @return EntityManager
     */
    public function getEntityManager() {
        $config = Setup::createAnnotationMetadataConfiguration($this->paths, $this->isDevMode, null, null, false);
        return EntityManager::create($this->dbParams, $config);
    }
}

/**
 * Function that can be called through out the app.
 *
 * @return EntityManager
 */
function getEntityManager() {
    $ds = new DoctrineSetup();
    return $ds->getEntityManager();
}

/**
 * Function that returns the conection to the database.
 */
function getConnection() {
    $ds = new DoctrineSetup();
    return $ds->getEntityManager()->getConnection();
}

我需要添加其他东西才能使此错误消失?

Do I need to add something else to it to get this error to go away?

所以我继续设置了 AppKernel ,因为我以前没有一个,因为我不相信我需要一个 config.yml (至少还没有)。一切似乎都在工作 - 内核明智,但错误仍然存​​在。

So I went ahead and set up the AppKernel as I didn't have one before, and because I don't believe I need a config.yml (at least not yet). Everything seems to be working - kernel wise, but the error still persists.

namespace ImageUploader;

use Symfony\Component\HttpKernel\Kernel;
use Symfony\Component\Config\Loader\LoaderInterface;

class AppKernel extends Kernel {

    public function registerBundles() {
        $bundles = array(
            new \Doctrine\Bundle\DoctrineBundle\DoctrineBundle()
        );

        return $bundles;
    }

    public function registerContainerConfiguration(LoaderInterface $loader) {}
}

然后我在引导文件中启动内核,添加:

I then started the kernel in the bootstrap file, by adding:

use \ImageUploader\AppKernel;

$kernel = new AppKernel();
$kernel->boot();

从我读过的一切都是正确的 - 减去不应该丢失的配置文件一个问题。但是我仍然收到错误的问题

Everything, from what I have read, is correct - minus the missing config file that shouldn't be an issue. But I still receive the error in question

推荐答案

你错过了将教义整合到Symfony框架中的Doctrine软件包。使用 composer require doctrine / doctrine-bundle 安装它,然后将其注册到您的 AppKernel 中。

You are missing the Doctrine bundle, which integrates doctrine into the Symfony framework. Install it with composer require doctrine/doctrine-bundle and then register it in your AppKernel.

您还需要一些配置并设置原则ORM。没有这个配置,ORM服务(像这里缺少的)没有加载。

You still need some configuration and set up the doctrine ORM. Without this configuration, ORM services (like the one missing here) are not loaded.

这篇关于没有找到类'doctrine.orm.validator.unique'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆