简体   繁体   English

Symfony2数据库创建实体

[英]Symfony2 Database Create Entities

I've recently inherited a bunch of Symfony2 apps from a former colleague. 我最近从前一位同事那里继承了许多Symfony2应用程序。 I've played with Symfony2 enough to where I understand the basics of what is going on from the Acme demo. 我已经与Symfony2一起玩了足够的东西,以至于我了解了Acme演示中发生的事情的基本知识。 My boss wants me to start looking at some of the code he has written as some things need to get updated right away. 我的老板希望我开始查看他编写的一些代码,因为有些事情需要立即进行更新。 I should note, I started on a fresh install of symfony and have started to go in and recreate what I see on my own -- at each point I'm looking at how I did it compared to what my former colleague did. 我应该注意,我开始全新安装的symfony,并且开始尝试重新创建自己所看到的内容-在每一点上,我都在寻找与前同事相比我是如何做的。

I'm at the point now where I need to create entities from the established database. 现在,我需要从已建立的数据库创建实体。 I've set the database configuration in app/config/parameters.yml 我已经在app / config / parameters.yml中设置了数据库配置

What is the best way to create the entities now to match the database? 现在创建实体以匹配数据库的最佳方法是什么? I've use php app/console generate:doctrine:entities but after I type all the fields out, I keep getting a matching error where symfony will look for "status_id3" instead of just "status". 我使用了php app/console generate:doctrine:entities但是在我输入所有字段之后,我不断收到匹配错误,其中symfony将查找“ status_id3”而不是“ status”。 Here is the error I'm getting: 这是我得到的错误:

An exception occurred while executing 'SELECT t0.id AS id1, t0.library_id AS library_id2, t0.status_id AS status_id3, t0.name AS name4, t0.total AS total5, t0.available AS available6, t0.in_use AS in_use7, t0.offline AS offline8 FROM Lab t0 ORDER BY t0.name ASC':

SQLSTATE[42S02]: Base table or view not found: 1146 Table 'labstats.Lab' doesn't exist

Many thanks for your help! 非常感谢您的帮助!

Doctrine is a Database Abstraction Layer. 原则是数据库抽象层。 This means all of the code that you write involving Doctrine entities and getter/setter functions are a high-level abstraction of what Doctrine actually queries MySQL for (with AS statements and highly unique names, along with fetching the ID of objects instead of the actual object, which would only get loaded lazily.) 这意味着您编写的所有涉及Doctrine实体和getter / setter函数的代码都是Doctrine实际向MySQL查询的内容的高级抽象(带有AS语句和高度唯一的名称,以及获取对象的ID而不是实际的ID)。对象,只会延迟加载。)

There's a lot of details to all of this that you can learn on your own from the Doctrine documentation, but this is a quick synopsis. 您可以从Doctrine文档中自行学习所有这些细节,但这只是一个简短的提要。

Either way, you're misinterpreting the MySQL error. 无论哪种方式,您都会误解MySQL错误。 Why would you think that MySQL is complaining about t0.status_id AS status_id3 when the error clearly states that the entire table labstats.Lab doesn't exist? 当错误明确指出整个表labstats.Lab不存在时,为什么您会认为MySQL在抱怨t0.status_id AS status_id3 The likely culprit is that your database schema doesn't match the metadata of the Doctrine Entities. 可能的罪魁祸首是您的数据库架构与“学说实体”的元数据不匹配。

The doctrine:generate:entities command will happily create your getter/setter functions for you automatically, but the database schema isn't touched until you run doctrine:schema:update from your console as per this documentation. doctrine:generate:entities命令将为您自动创建您的getter / setter函数,但是直到您按照本文档从控制台运行doctrine:schema:update ,数据库架构才会被使用 Without the --force flag, Doctrine will just verify the schema and let you know the number of SQL queries that need to happen to update the schema (with the option to see them using --dump-sql .) With the --force flag, Doctrine will run them for you (which is not recommended on a production environment. Instead you should use migrations .) 没有--force标志,学说只会验证架构,并让你知道要发生更新架构的SQL查询的数量(有看到他们使用选项--dump-sql )。随着--force标志,Doctrine将为您运行它们(在生产环境中不建议这样做。相反,您应该使用migrations 。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM