向Symfony BaseModel添加属性

时间:2018-07-09 02:13:45

标签: php symfony orm doctrine

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

我尝试先运行php bin /控制台学说:database:drop,然后运行php bin /控制台学说:database:create,希望它能进行必要的映射,但没有成功,我得到以下错误:{{1 }}

这似乎过于复杂,我相信有一种更简单的方法可以完成此任务。我感谢任何建议。

composer.json代码段

You have requested a non-existent service "doctrine".

NoticeSettings.php

        "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",

config.yml

<?php

namespace AppBundle\Model;

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

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

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

1 个答案:

答案 0 :(得分:1)

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

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

接下来,您将需要理论配置。 You can check the repository if you need a reference configuration

# 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

还要确保您的parameter.yml和parameters.yml.dist包含配置所需的值。同样,您可以使用the file from the official repository as a reference