字段数据在一个表中,不多。为drupal7 [英] Field data in one table, not many. for drupal7

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

问题描述

我正在与商务模块合作创建一个网上商店。我正在修改产品.install文件以创建内容类型(因为我被告知这是必需的),作为该内容类型的一部分,我需要创建大量的字段。该列表将是大约50-60个不同的信息。



理想情况下,我想将它们存储在一个单一的表格中,其中开始的是productID,所有其他信息但是,似乎并非如此;所有的字段都存储在不同的表格中。



我注意到,与商业一起使用的地址模块创建了一个具有大约15个不同值的字段类型存储在同一个框中。这怎么可能?我注意到,如果我将基数设置为5,例如,它创建不同的行。我只想要一张含有以下内容的表:



ID - value1 - value2 - value3等等。



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

解决方案

这不是Drupal现场系统的工作原理我害怕,一个字段==一个表(实际上,如果你包括每个字段的修订表,实际上是2个表) 。



地址模块使用 hook_field_schema() 为该特定字段定义几个(看看 address.install ,你会看到我的意思)。



所以如果你想把一切放在一张表中只需要定义自己的字段类型(请参阅示例模块,具体来说是 field_example 为了帮助)。



请记住,您在中定义的列数一旦模块安装,_field_schema()将是静态的,您将能够增加/减少的唯一方法是使用 _update 钩子为您的自定义模块。



另外,如果你是黑客在包含在Commerce模块的文件... 停止! >:商业仍处于起步阶段,您很可能需要更新它,一旦您完成代码更改,您的网站将会处于不一致的状态。 p>

Drupal的全部内容是,所有内容都被挂接/整理,以便系统的其他部分可以更改。在 product.install 中,您无法通过在另一个模块中实现Drupal钩子而无法做到这一点。



如果您不确定,请发布另一个问题,详细说明您要通过直接编辑contrib模块文件来完成哪些工作,而其中一个Drupal大师将指向您在正确的方向: - )



编辑



现在已经在Drupal 7中与 Ubercart 一起工作了很长一段时间,找到了一个非常非常好的解决方案(很多商业贡献模块仍然在dev / alpha / beta;这对于Ubercart提供的模块来说更少)。这可能值得一看。



某些更多信息



我想你在这里基本上有两个选择,但是您需要创建一个自定义模块(这里的优秀指令)。



选项1:创建自定义字段



如果您是Drupal编码初学者,我建议这是可能是完成你想要的最简单的方法,但它还不是完全直截了当。从上面的Drupal示例模块链接中获取 field_example 模块,并查看 .install 文件, code> field_example_field_schema()函数。这定义了该字段的表中的列。然后看看 field_example.module ...几乎每个功能被评论与实现hook_x 是一个你/ />

我认为这样会更容易,因为Drupal会为您处理表/表单字段的创建
,所以你不必弄乱数据库,模式或表单API。



选项2:创建自定义模块



此选项涉及实现您自己的表(如您在注释中建议的)主键将是产品的实体ID,并且还包含所有自定义列。 (请参阅 Schema API文档以获取帮助)。



然后,您将实施 hook_form_alter() 添加用户输入数据所需的表单字段,然后实现 hook_node_insert() hook_node_update()将此数据保存到数据库表中。很难进行任何更详细的细节,而不需要编写代码,而且它还有一些代码!



希望有帮助,对不起,我不能再具体了不了解情况的所有内容并不容易。


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.

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.

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 etc etc.

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?

解决方案

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).

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).

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).

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.

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.

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.

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 :-)

EDIT

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.

Some more info

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).

Option 1: Create a custom field

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.

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.

Option 2: Create a custom module

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).

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天全站免登陆