使用Symfony2.8验证无效

时间:2018-03-16 14:04:31

标签: symfony symfony-2.8

我是使用Symfony2.8的新手,我遇到了验证问题。

这是依赖注入的代码 -

    <?php
namespace AppBundle\DependencyInjection;

use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;

class AppExtension extends Extension
{
    public function load(array $configs, ContainerBuilder $container) {
        $r = new \ReflectionClass(get_class($this));
        $dir = dirname($r->getFileName());

        $loader = new YamlFileLoader($container, new FileLocator($dir.'/../Resources/config'));
        $loader->load('validation.yml');
    }
}

然后我试图验证这两列是否是唯一的。 (的UniqueConstraint)。

# src/AppBundle/Resources/config/validation.yml
AppBundle\Entity\StaticContent:
    constraints:
        - Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity:
              fields: [build, name]

使用实体中的StaticContent并且它抱怨它无法找到StaticContent。

   nebo@localhost:/var/www/html/iclei$ php app/console doctrine:schema:update --dump-sql


  [Symfony\Component\DependencyInjection\Exception\InvalidArgumentException]                                                                                                                               
  There is no extension able to load the configuration for "AppBundle\Entity\StaticContent" (in /var/www/html/iclei/src/AppBundle/DependencyInjection/../Resources/config/validation.yml). Looked for nam  
  espace "AppBundle\Entity\StaticContent", found none  

我做错了什么?

我也在里面
app/config/config.yml 

framework:
    #esi: ~
    #translator: { fallbacks: ['%locale%'] }
    secret: '%secret%'
    router:
        resource: '%kernel.root_dir%/config/routing.yml'
        strict_requirements: ~
    form: ~
    csrf_protection: ~
    validation: { enabled: true, enable_annotations: true }

这是我们正在建设的Bundle的结构。 的 DIR /src/AppBundle/Resources/config/doctrine/StaticContent.orm.yml 的 DIR /src/AppBundle/DependencyInjection/AppExtension.php

这是我的学说配置:

# Doctrine Configuration
doctrine:
    dbal:
        driver: pdo_mysql
        host: '%database_host%'
        port: '%database_port%'
        dbname: '%database_name%'
        user: '%database_user%'
        password: '%database_password%'
        charset: UTF8
        # if using pdo_sqlite as your database driver:
        #   1. add the path in parameters.yml
        #     e.g. database_path: '%kernel.root_dir%/data/data.db3'
        #   2. Uncomment database_path in parameters.yml.dist
        #   3. Uncomment next line:
        #path: '%database_path%'

    orm:
        entity_managers:
            default:
                mappings:
                    AppBundle: ~
        auto_generate_proxy_classes: '%kernel.debug%'
        naming_strategy: doctrine.orm.naming_strategy.underscore
        auto_mapping: true

在I13请求中,我添加了我的实体。

<?php

namespace AppBundle\Entity;

/**
 * StaticContent
 */
class StaticContent
{
    /**
     * @var int
     */
    private $id;

    /**
     * @var int
     */
    private $build;

    /**
     * @var string
     */
    private $name;

    /**
     * @var string
     */
    private $type;

    /**
     * @var string
     */
    private $data;

与他们的吸气剂和二传手。

AppBundle\Entity\StaticContent:
type: entity
table: null
repositoryClass: AppBundle\Repository\StaticContentRepository
id:
    id:
        type: integer
        id: true
        generator:
            strategy: AUTO
fields:
    name:
        type: string
        length: 255
    data:
        type: blob
manyToOne:
    build:
        targetEntity: AppBundle\Entity\Build
        inversedBy: staticContents
        joinColumn:
            name: build_id
            referencedColumnName: id
lifecycleCallbacks: {  }

1 个答案:

答案 0 :(得分:0)

请尝试在doctrine配置下启用bundle并告诉它是否有效:

 # Doctrine Configuration
    doctrine:            
        dbal:
            driver: pdo_mysql
            host: '%database_host%'
            port: '%database_port%'
            dbname: '%database_name%'
            user: '%database_user%'
            password: '%database_password%'
            charset: UTF8
        orm:
            auto_generate_proxy_classes: "%kernel.debug%"
            entity_managers:
                default:
                    auto_mapping: true                        
                    mappings:
                        YOURBundle: ~

更改AppExtension:

<?php

       namespace AppBundle\DependencyInjection;

use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\DependencyInjection\Loader;


class AppExtension extends Extension {

    /**
     * {@inheritdoc}
     */
    public function load(array $configs, ContainerBuilder $container) {
        $configuration = new Configuration();
        $config = $this->processConfiguration($configuration, $configs);

        $loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));
    }

}