简体   繁体   English

使用cakephp和HABTM将关联数据保存到数据库中

[英]Save associated data into database with cakephp and HABTM

I have 2 tables: release_servers and release_components 我有2个表: release_serversrelease_components
I have a link table release_server_to_components 我有一个链接表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语句:

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). 我要添加的是具有系统ID的功能-此系统ID将是服务器上的每个组件(不是每个服务器,也不是每个组件,而是每个服务器上的每个组件)。 I want to be able to easily add the system ID per component and insert it into the database. 我希望能够轻松地为每个组件添加系统ID,并将其插入数据库中。

I can provide code for models and controllers if needed. 如果需要,我可以提供模型和控制器的代码。

You want to "fake" Ruby's through association with CakePHP. 您想通过与CakePHP的关联“伪造” Ruby。

Why do this over HABTM? 为什么通过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. 使用Cake的HABTM很难保存有关关联的数据,因为您真正拥有的唯一一个就是JOIN表。 You need something more powerful than this. 您需要比这更强大的功能。

First, get rid of the $hasAndBelongsToMany property in your model. 首先,摆脱模型中的$hasAndBelongsToMany属性。 Next we'll be refitting your release_server_to_components table as your "through" table. 接下来,我们将把您的release_server_to_components表重新设置为“通过”表。

So, in ReleaseComponent and ReleaseServer model you would have an association like this: 因此,在ReleaseComponentReleaseServer模型中,您将具有以下关联:

$hasMany = array('ReleaseServerToComponent');

Now, in your new ReleaseServerToComponent model you would have an association like this: 现在,在新的ReleaseServerToComponent模型中,您将具有以下关联:

$belongsTo = array('ReleaseComponent', 'ReleaseServer');

Now, you can access this table just like a normal Cake model, ie $this->ReleaseServer->ReleaseServerToComponent->find() . 现在,您可以像普通的Cake模型一样访问此表,即$this->ReleaseServer->ReleaseServerToComponent->find() You can add additional fields to the through table, like server_component_name . 您可以将其他字段添加到穿透表,例如server_component_name You already have a unique identifier for specific, server components with the primary key of the release_server_to_components table. 您已经具有带有release_server_to_components表的主键的特定服务器组件的唯一标识符。

You could save this data using Cake's saveAll() method . 您可以使用Cake的saveAll()方法保存此数据。 Alternatively you could generate your own data to save, simply plugging in the server ID and component ID from form fields. 或者,您可以生成自己的数据进行保存,只需从表单字段中插入服务器ID和组件ID。 At the top of that link is the format saved data should be in when you pass it to the model's save method. 在该链接的顶部是将格式的数据传递给模型的save方法时应采用的格式。

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

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