使用 cakephp 和 HABTM 将关联数据保存到数据库中
我有 2 个表:release_servers
和 release_components
我有一个链接表 release_server_to_components
我现在拥有它,以便每个服务器可以有多个组件,并且每个组件可以位于多个服务器上。
以下是创建语句:
CREATE TABLE `release_components` (
`id` int(11) NOT NULL auto_increment,
`buildID` varchar(45) default NULL,
`svnNumber` varchar(45) NOT NULL,
`componentType` varchar(45) NOT NULL,
`release_id` varchar(45) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM
CREATE TABLE `release_servers` (
`id` int(11) NOT NULL auto_increment,
`server_name` varchar(45) NOT NULL,
`server_environment` varchar(45) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM
链接表:
CREATE TABLE `release_server_to_components` (
`id` int(11) NOT NULL auto_increment,
`release_component_id` int(11) NOT NULL,
`release_server_id` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM
我要添加的是具有系统 ID 的能力 - 该系统 ID 将是服务器上的每个组件(不是每个服务器,也不是每个组件,而是每个服务器上的组件)服务器)。我希望能够轻松添加每个组件的系统 ID 并将其插入数据库。
如果需要,我可以提供模型和控制器的代码。
I have 2 tables: release_servers
and release_components
I have a link table release_server_to_components
I right now have it so that each server can have multiple components and that each component can be on multiple servers.
The following are the create statements:
CREATE TABLE `release_components` (
`id` int(11) NOT NULL auto_increment,
`buildID` varchar(45) default NULL,
`svnNumber` varchar(45) NOT NULL,
`componentType` varchar(45) NOT NULL,
`release_id` varchar(45) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM
CREATE TABLE `release_servers` (
`id` int(11) NOT NULL auto_increment,
`server_name` varchar(45) NOT NULL,
`server_environment` varchar(45) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM
Link table:
CREATE TABLE `release_server_to_components` (
`id` int(11) NOT NULL auto_increment,
`release_component_id` int(11) NOT NULL,
`release_server_id` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM
What I want to add is the ability to have a system ID -- this system ID would be per component on a server (not per server and not per component, but per the component on each server). I want to be able to easily add the system ID per component and insert it into the database.
I can provide code for models and controllers if needed.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你想要"假“ Ruby 是通过与 CakePHP 关联的。
为什么要通过 HABTM 进行此操作?
因为你想保存有关关联的数据。使用 Cake 的 HABTM 保存有关关联的数据很困难,因为您真正拥有的唯一东西是
JOIN
表。你需要比这更强大的东西。首先,删除模型中的
$hasAndBelongsToMany
属性。接下来,我们将把您的release_server_to_components
表重新调整为“through”表。因此,在
ReleaseComponent
和ReleaseServer
模型中,您将具有如下所示的关联:现在,在新的
ReleaseServerToComponent
模型中,您将具有如下所示的关联:现在,您可以像普通的 Cake 模型一样访问该表,即
$this->ReleaseServer->ReleaseServerToComponent->find()
。您可以向直通表中添加其他字段,例如server_component_name
。您已经拥有特定服务器组件的唯一标识符以及release_server_to_components
表的主键。您可以使用 Cake 的
saveAll()方法
。或者,您可以生成自己的数据进行保存,只需从表单字段中插入服务器 ID 和组件 ID 即可。该链接的顶部是将保存的数据传递给模型的保存方法时应采用的格式。
You want to "fake" Ruby's through association with CakePHP.
Why do this over HABTM?
Because you want to save data about the association. With Cake's HABTM saving data about the association is difficult, because the only thing you really have is a
JOIN
table. You need something more powerful than this.First, get rid of the
$hasAndBelongsToMany
property in your model. Next we'll be refitting yourrelease_server_to_components
table as your "through" table.So, in
ReleaseComponent
andReleaseServer
model you would have an association like this:Now, in your new
ReleaseServerToComponent
model you would have an association like this:Now, you can access this table just like a normal Cake model, ie
$this->ReleaseServer->ReleaseServerToComponent->find()
. You can add additional fields to the through table, likeserver_component_name
. You already have a unique identifier for specific, server components with the primary key of therelease_server_to_components
table.You could save this data using Cake's
saveAll()
method. Alternatively you could generate your own data to save, simply plugging in the server ID and component ID from form fields. At the top of that link is the format saved data should be in when you pass it to the model's save method.