简体   繁体   English

简单键值组表的学说实体设计

[英]Doctrine Entity Design for a simple key-value-group table

I have a data table like this: 我有一个像这样的数据表:

id|name|group

That hold simple hierarchical data, for list reference. 即存放简单的分层数据,以供列表参考。 For exemple I can have on this table a list of countries or cities or a list of product types. 例如,我可以在此表上列出国家或城市列表或产品类型列表。

and in a product table with that structure: 并在具有该结构的产品表中:

id|name|city|country|type

and in each column (city|country|type) an ID (key) of the row in the data table. 数据表中该行的每个列(城市)的ID(关键字)。

I am starting using Symfony2 and I don't know how to represent this kind of data in Doctrine Entity. 我开始使用Symfony2,但我不知道如何在“ Doctrine实体”中表示此类数据。 I dont want to have an entity for each group of data in the data table... 我不想为数据表中的每组数据都有一个实体...

Thank you 谢谢

You could achive just what you want but you need to write an entity . 您可以实现所需的内容,但需要编写一个实体

  1. Write an entity class 写一个实体类
  2. Create custom EntityRepository (maybe, if you'd like to separate queries from controller) 创建自定义EntityRepository (也许,如果您想将查询与控制器分开)
  3. Query the database with query like this: 使用如下查询查询数据库:

     $em = $this->getDoctrine()->getManager(); $em->createQuery('SELECT e FROM MyNamespace:Entity e INDEX BY e.key') ->getArrayResult();` // INDEX keyword is crucial here 
  4. You get the key => value structure as an output. 您将获得key => value结构作为输出。

I know, it a bit overkill... 我知道,这有点矫kill过正...

UPDATE UPDATE

Ok, I scratched a bit of code just to show you an example: http://codepad.org/f2pzjnrd 好的,我只花了一些代码只是为了向您展示一个示例: http : //codepad.org/f2pzjnrd

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

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