注意人们将这个问题标记为与他人一样,但它不是= - 我不使用Symfony和Doctrine作为网络框架,我正在使用Symfony使用SlimphP作为实际的Web框架时,具有学说的组件。
因此,虽然您可能认为我需要AppKernel
,但我不需要。我正在使用Symfony组件。这里唯一的问题是@UniqueEntity
不起作用。
我不知道我在做什么。我遇到了错误:
Fatal error: Class 'doctrine.orm.validator.unique' not found in /var/www/html/image_upload_app/vendor/symfony/validator/ConstraintValidatorFactory.php on line 46
我的AppKernel.php
看起来像:
namespace ImageUploader;
use SymfonyComponentHttpKernelKernel;
use SymfonyComponentConfigLoaderLoaderInterface;
class AppKernel extends Kernel {
public function registerBundles() {
$bundles = array(
new DoctrineBundleDoctrineBundleDoctrineBundle()
);
return $bundles;
}
public function registerContainerConfiguration(LoaderInterface $loader) {}
}
从那里我创建了一个带有以下内容的bootstrap.php
:
/** ---------------------------------------------------------------- **/
// Lets Setup Doctrine.
/** ---------------------------------------------------------------- **/
require_once 'vendor/autoload.php';
$loader = require 'vendor/autoload.php';
DoctrineCommonAnnotationsAnnotationRegistry::registerLoader(array($loader, 'loadClass'));
use DoctrineORMToolsSetup;
use DoctrineORMEntityManager;
/**
* 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();
}
use ImageUploaderAppKernel;
$kernel = new AppKernel();
$kernel->loadClassCache();
我不确定为什么要遇到此错误,我有一个看起来像这样的模型(实体):
namespace ImageUploaderModels;
use DoctrineORMMapping as ORM;
use SymfonyComponentValidatorConstraints as Assert;
use SymfonyBridgeDoctrineValidatorConstraintsUniqueEntity;
/**
* @ORMEntity
* @ORMTable(name="users")
* @UniqueEntity(fields="userName")
* @UniqueEntity(fields="email")
*/
class User {
/**
* @ORMId
* @ORMColumn(type="integer")
* @ORMGeneratedValue
*/
protected $id;
/**
* @ORMColumn(type="string", length=32, nullable=false)
* @AssertNotBlank()
*/
protected $firstName;
/**
* @ORMColumn(type="string", length=32, nullable=false)
* @AssertNotBlank()
*/
protected $lastName;
/**
* @ORMColumn(type="string", length=100, unique=true, nullable=false)
* @AssertNotBlank(
* message = "Username cannot be blank"
* )
*/
protected $userName;
/**
* @ORMColumn(type="string", length=100, unique=true, nullable=false)
* @AssertNotBlank()
* @AssertEmail(
* message = "The email you entered is invalid.",
* checkMX = true
* )
*/
protected $email;
/**
* @ORMColumn(type="string", length=500, nullable=false)
* @AssertNotBlank(
* message = "The password field cannot be empty."
* )
*/
protected $password;
/**
* @ORMColumn(type="datetime", nullable=true)
*/
protected $created_at;
/**
* @ORMColumn(type="datetime", nullable=true)
*/
protected $updated_at;
}
当我去验证模型时,我会收到上面列出的错误。我已经安装了学说捆绑包,我认为我正确设置了东西。但显然不是。知道我在做什么错吗?我是否配置了错误或根本不配置某些东西?
您不需要使用Appkernel,我在其他问题中误解了您的问题。
问题是您正在使用桥梁中使用UniqueValidator
。这旨在使用全堆栈框架,而不是独立使用(仅组件是独立的)。
这意味着您不能使用此约束。相反,您可以使用纯教义解决方案:@UniqueConstraint
。