字段数据一张表,不多.对于drupal7 [英] Field data in one table, not many. for drupal7

查看:12
本文介绍了字段数据一张表,不多.对于drupal7的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用商务模块创建一个在线商店.我正在修改产品 .install 文件以创建内容类型(据我所知,这是必需的),作为该内容类型的一部分,我需要创建大量字段.该列表将包含大约 50-60 条不同的信息.

I am working with the commerce module to create an online store. I am modifying the products .install file to create a content type (as I have been told this is required) and as part of that content type, I need to create lots of fields. The list will be around 50-60 different pieces of information.

理想情况下,我希望将这些信息存储在一个表中,其中 productID 开头,所有其他信息也一起存储,但情况似乎并非如此;所有字段都存储在不同的表中.

Ideally I would like to store these in a single table with the productID at the beginning and all the other information along, but this doesn't seem to be the case; all the fields are stored in different tables.

我注意到,也用于 commerce 的地址"模块创建了一个字段类型,其中大约 15 个不同的值都存储在同一个框中.这怎么可能?我注意到,例如,如果我将基数设置为 5,它会创建不同的行.我只想要一张包含以下内容的表格:

I noticed that the "Address" module that is also used with commerce creates a field-type that has about 15 different values all stored in the same box. How is this possible? I noticed that if I set the cardinality up to 5 for example, it creates different rows. I just want a table with the following:

ID - value1 - value2 - value3 等等

ID - value1 - value2 - value3 etc etc.

我也不需要任何模块/扩展,因为这一切都需要写入文件.我也不认为更改为 mongoDB(我认为)是一种选择,那么在这种情况下我的选择是什么?

I also don't need any modules/extensions as this all needs to be written in the files. I also don't think that changing to the mongoDB ( I think ) is an option, so what are my options in this situation?

推荐答案

恐怕 Drupal 字段系统不是这样工作的,一个字段 == 一个表(如果你实际上是 2 个表包括每个字段的修订表).

That's not how the Drupal field system works I'm afraid, one field == one table (well actually 2 tables if you include the revision table for each field).

地址模块使用hook_field_schema() 为该特定字段定义几个(查看address.install,您就会明白我的意思).

The Address module uses hook_field_schema() to define several columns for that particular field (have a look in address.install and you'll see what I mean).

因此,如果您想将所有内容放在一个表中,您只需定义自己的字段类型(请参阅 示例模块,特别是 field_example 以获得帮助).

So if you want to put everything in one table you'll simply have to define your own field type (see the examples module, specifically field_example for help with that).

请记住,一旦安装了模块,您在 hook_field_schema() 中定义的列数将是静态的,并且您可以增加/减少它的唯一方法为您的自定义模块提供 _update 钩子.

Bear in mind though that the number of columns you define in hook_field_schema() will be static once the module is installed, and the only way you're going to be able to increase/decrease it is with an _update hook for your custom module.

此外,如果您正在破解 Commerce 模块中包含的文件...停止!:Commerce 仍处于起步阶段,您可能需要尽快对其进行更新...一旦完成,您的代码更改就会消失,您的网站很有可能会处于不一致的状态.

Also, if you're hacking at files that are included in the Commerce module...stop!: Commerce is still very much in it's infancy and you will likely have to update it soon...once you've done that your code changes will be gone and there's a good chance your site will be in an inconsistent state.

Drupal 的全部意义在于,所有内容都被挂钩/外包,以便系统的其他部分可以对其进行更改.product.install 中的任何更改都不能通过在另一个模块中实现 Drupal 挂钩来完成.

The whole point to Drupal is that everything is hooked/farmed out so that it can be altered by other parts of the system. There's nothing you can change in product.install that can't be done by implementing a Drupal hook in another module.

如果您不确定,请发布另一个问题,详细说明您通过直接编辑 contrib 模块文件尝试完成的工作,并且 SO 上的 Drupal 专家之一将为您指明正确的方向 :-)

If you're unsure, post another question detailing what you're trying to accomplish by directly editing a contrib module file and one of the Drupal gurus on SO will point you in the right direction :-)

编辑

只是说我在 Drupal 7 中使用 Ubercart 已经有一段时间了,发现它非常非常很好的解决方案(很多 Commerce 贡献的模块仍然在 dev/alpha/beta 中;对于 Ubercart 贡献的模块来说,情况就不那么好了).可能值得一看.

Just to say I've been working with Ubercart in Drupal 7 for quite some time now and find it a very, very good solution (a lot of Commerce contributed modules are still in dev/alpha/beta; this is less so for Ubercart contributed modules). It might be worth a look.

更多信息

我认为您在这里基本上有两个选择,但无论哪种方式,您都需要创建一个自定义模块(这里有一组出色的说明).

I think you've basically got two options here but either way you'll need to create a custom module (excellent set of instructions here).

选项 1:创建自定义字段

Option 1: Create a custom field

如果您是 Drupal 编码初学者,我建议这可能是完成您想要的最简单的方法,但它仍然不是完全直截了当.从上面的 Drupal 示例模块链接中获取 field_example 模块并查看 .install 文件,特别是 field_example_field_schema() 函数.这定义了将在该字段的表中的列.然后看看 field_example.module...几乎每个用 Implements hook_x 注释的函数都是您想要复制到模块中并进行调整的函数满足你自己的需要.

If you're a Drupal coding beginner I'd suggest this is probably the easiest way to accomplish what you want, but it's still not totally straight forward. Grab the field_example module from the Drupal Examples module link above and have a look in the .install file, specifically the field_example_field_schema() function. That defines the columns that will be in the table for that field. Then have a look in field_example.module...pretty much every function that's commented with Implements hook_x is one that you're going to want to copy into your module and tweak for your own needs.

我认为这会更容易,因为 Drupal 会为您处理表格/表单字段的创建这样您就不必弄乱数据库、架构或表单 API.

I think this will be easier because Drupal will handle the table/form field creation for you so you don't have to mess with the database, schema or form APIs.

选项 2:创建自定义模块

Option 2: Create a custom module

此选项涉及实现您自己的表(就像您在评论中建议的那样),其中主键是产品的实体 ID,并且还包含您的所有自定义列.(有关这方面的帮助,请参阅 Schema API 文档).

This option involves implementing your own table (like you suggest in your comment) where the primary key would be the entity ID of the product and would also contain all of your custom columns. (See the Schema API documentation for help with this).

然后你会实现 hook_form_alter() 添加用户输入数据所需的表单字段,然后实现 hook_node_insert()hook_node_update() 持久化将此数据添加到您的数据库表中.如果不实际编写代码,很难深入了解更多细节,而且代码相当多!

Then you'd implement hook_form_alter() to add the form fields necessary for a user to input the data, and then implement hook_node_insert() and hook_node_update() to persist this data to your database table. It's quite hard to go into any more detail without actually writing code and it's quite a bit of code!

希望对您有所帮助,对不起,我不能再具体了,但是如果不了解情况的所有来龙去脉,这并不容易

Hope that helps, sorry I can't be any more specific but it's not easy without knowing all the ins and outs of the situation

这篇关于字段数据一张表,不多.对于drupal7的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆