首页 > 解决方案 > 向 Symfony BaseModel 添加属性

问题描述

我有以下 symfony 模型,我需要向name现有模型添加一个属性(见下文),然后相应地更新数据库。实现此目的的正确方法是什么?我很感激任何建议。

我尝试运行 php bin/console dictionary:database:drop 然后 php bin/console dictionary:database:create,希望它会进行必要的映射,但没有任何成功,我收到以下错误:You have requested a non-existent service "doctrine".

这似乎过于复杂,我相信有一种更简单的方法可以实现这一点。我很感激任何建议。

composer.json 片段

        "php": ">=5.5.9",
        "symfony/symfony": "3.0.*",
        "doctrine/orm": "^2.5",
        "doctrine/doctrine-bundle": "^1.6",
        "doctrine/doctrine-cache-bundle": "^1.2",
        "symfony/swiftmailer-bundle": "^2.3",
        "symfony/monolog-bundle": "^2.8",
        "sensio/distribution-bundle": "^5.0",
        "sensio/framework-extra-bundle": "^3.0.2",

NoticeSettings.php

<?php

namespace AppBundle\Model;

class NoticeSettings extends BaseModel
{
    protected $table      = "notice_settings";

    /**
     * @Assert\Type(type="string")
     */
    protected $notice;

   /**
    * @Assert\Type(type="string")
    */
   protected $name;
}

配置.yml

imports:
    - { resource: parameters.yml }
    - { resource: security.yml }
    - { resource: services.yml }
parameters:
    locale: en
    uploads_directory: '%kernel.root_dir%/../uploads'

framework:
    #esi:             ~
    #translator:      { fallbacks: ["%locale%"] }
    secret:          "%secret%"
    router:
        resource: "%kernel.root_dir%/config/routing.yml"
        strict_requirements: ~
    form:            ~
    csrf_protection: ~
    validation:      { enable_annotations: true }
    #serializer:      { enable_annotations: true }
    templating:
        engines: ['twig']
    default_locale:  "%locale%"
    trusted_hosts:   ~
    trusted_proxies: ~
    session:
        # http://symfony.com/doc/current/reference/configuration/framework.html#handler-id
        handler_id:  session.handler.native_file
        save_path:   "%kernel.root_dir%/../var/sessions/%kernel.environment%"
    fragments:       ~
    http_method_override: true
    assets:
        version: 15

# Twig Configuration
twig:
    debug:            "%kernel.debug%"
    strict_variables: "%kernel.debug%"
    form_themes:      ["form/fields.html.twig"]
    globals:
        user: "@app.user"
        badges: '@app.sidebar_badges'
    paths:
        '%kernel.root_dir%/../src/AppBundle/Resources/views': ~
        '%kernel.root_dir%/../src/AppBundle/Resources/TwigBundle/views': 'Twig'

标签: phpsymfonyormdoctrine

解决方案


看起来 Doctrine 没有在您的应用程序中完全配置。这可能也是无法找到该服务的原因。

请确保在您的 AppKernel.php 中注册了 DoctrineBundle(搜索new Doctrine\Bundle\DoctrineBundle\DoctrineBundle())。

接下来,您将需要学说配置。如果需要参考配置,可以检查存储库

# 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:
        auto_generate_proxy_classes: "%kernel.debug%"
        naming_strategy: doctrine.orm.naming_strategy.underscore
        auto_mapping: true

还要确保您的 parameters.yml 和 parameters.yml.dist 包含配置所需的值。同样,您可以从官方存储库中获取文件作为参考


推荐阅读