php - 向 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'
解决方案
看起来 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 包含配置所需的值。同样,您可以从官方存储库中获取文件作为参考。
推荐阅读
- django - Pythonanywhere 在部署 django 应用程序期间无法加载静态文件
- 3d - 我可以读取应用的 SCNConstraint 的变换矩阵吗?
- javascript - 如何在浏览器中运行 React Native
- javascript - 如何根据 obj 标志的状态查找数组长度
- angular - 茉莉和可观察的
- maven - Selenium Grid Docker-通过 Jenkins 实现自动化
- ios - 自动 TableView 单元格高度无法正常工作
- javascript - MVC 5 中的搜索参数
- ios - 无法安装cordova-plugin-accountkit
- ios - iOS 本地化使用 Xib 和 Swift:有人可以帮助我使用 XIB(不是 Storyboard)和 Swift 制作多语言应用程序吗?