【问题标题】:Validation doesn't work using Symfony2.8使用 Symfony2.8 验证不起作用
【发布时间】:2018-03-16 14:04:31
【问题描述】:

我是使用 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');
    }
}

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

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

在实体中有静态内容,它抱怨它找不到静态内容。

   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 目录/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;

使用他们的 getter 和 setter。

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: {  }

【问题讨论】:

  • 您是否检查了 StaticContent 命名空间、文件和类名(包括大小写和文件扩展名)以搁置实体上的自动加载失败?
  • 命名空间 AppBundle\Entity;
  • 类静态内容
  • 你也可以发布 yml 映射吗? (StaticContent.orm.yml)

标签: symfony symfony-2.8


【解决方案1】:

请尝试在学说配置下启用捆绑并告诉它是否有效:

 # 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: ~

更改应用扩展:

<?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'));
    }

}

【讨论】:

  • 嗯,我不确定我是否理解正确。但是知道它不起作用,因为它抱怨 orm 配置。
  • [Symfony\Component\Config\Definition\Exception\InvalidConfigurationException] “doctrine.orm”下无法识别的选项“naming_strategy, auto_mapping”
  • 默认设置好了。
  • 哦,对不起。刚才我看到默认是其他颜色的。
  • 还是同样的问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-02
  • 1970-01-01
  • 2013-02-06
  • 2018-06-19
相关资源
最近更新 更多