My situation is that I have data to be added in two tables Login and Customer Details in MySQL. The data is being collected by Zend Framework 2 one form and being added into Database at the same time. Both Tables in MySQL has a Customer_ID. In both table the customer_id is Auto increment.I want to it to be such that when add the data via the form the Customer_ID in Customer Details is auto incremented and its value is copied onto the Login table Customer_ID.
我的情况是我有数据要添加到MySQL中的两个表登录和客户详细信息中。这些数据由Zend Framework 2收集,并同时添加到数据库中。 MySQL中的两个表都有一个Customer_ID。在两个表中,customer_id都是自动增量。我希望它是这样的,当通过表单添加数据时,客户详细信息中的Customer_ID会自动递增,其值将被复制到Login表Customer_ID上。
So far I have tried it with LastInserverValue(), I have tried making Login's Customer_ID a FK, I have tried to manually assign the customer_ID to Login with no luck. With the above methods I used they only added but Customer_ID are not the same.
到目前为止,我已经尝试使用LastInserverValue(),我尝试将Login的Customer_ID设为FK,我试图手动将customer_ID分配给Login而没有运气。使用上面提到的方法,他们只添加了但Customer_ID不一样。
Note: The data is being added not Updated.
注意:正在添加数据而不是更新。
UPDATE: I know I can get using last insert ID but because I am using two tablegateways for the two tables in ZF2 i cant figure out how to get the last insert value from tablegateway1 to tablegateway 2
更新:我知道我可以使用最后一个插入ID,但因为我在ZF2中使用两个表格网关于两个表我无法弄清楚如何从tablegateway1到tablegateway 2获取最后一个插入值
3 个解决方案
#1
0
Your auto increment should only be on the Customer table. You may also have an auto increment for the login id on the login table, but you're quite right to have the login.customer_id a foreign key.
您的自动增量应仅在Customer表上。您也可以在登录表上自动增加登录ID,但是您可以将login.customer_id作为外键。
In the login process you want to get the customer.customer_id then create an insert into the login table
在登录过程中,您希望获得customer.customer_id,然后在登录表中创建一个插入
E.g Structure:
Customer
customer_id INT PK, AI
customer_id INT PK,AI
first_name
last_name
username
password
Login
id PK, AI
id PK,AI
customer_id (FK) customer.customer_id
customer_id(FK)customer.customer_id
created_at
#2
0
In both table the customer_id is Auto increment
在两个表中,customer_id都是自动增量
First remove the auto increment for customer_id from another table LOGIN. the insert values in Customer Table using tablegateway1 and get the last insert value now update this value in Login table for customer_id using table gateway2.
首先从另一个表LOGIN中删除customer_id的自动增量。使用tablegateway1在Customer Table中插入值并获取最后一个插入值现在使用table gateway2在customer_id的登录表中更新此值。
This should work.
这应该工作。
#3
0
As mentioned by Paul Sellars, if you will remove AI from one table, then your problem will be solved. In the table, where AI is defined, insert the data into their first and and get last_insert_id from there. With that value, update/insert the record into the other table where AI is not defined.
正如保罗塞拉斯所提到的,如果您将AI从一个表中移除,那么您的问题将得到解决。在定义AI的表中,将数据插入到第一个,然后从那里获取last_insert_id。使用该值,将记录更新/插入到未定义AI的另一个表中。
#1
0
Your auto increment should only be on the Customer table. You may also have an auto increment for the login id on the login table, but you're quite right to have the login.customer_id a foreign key.
您的自动增量应仅在Customer表上。您也可以在登录表上自动增加登录ID,但是您可以将login.customer_id作为外键。
In the login process you want to get the customer.customer_id then create an insert into the login table
在登录过程中,您希望获得customer.customer_id,然后在登录表中创建一个插入
E.g Structure:
Customer
customer_id INT PK, AI
customer_id INT PK,AI
first_name
last_name
username
password
Login
id PK, AI
id PK,AI
customer_id (FK) customer.customer_id
customer_id(FK)customer.customer_id
created_at
#2
0
In both table the customer_id is Auto increment
在两个表中,customer_id都是自动增量
First remove the auto increment for customer_id from another table LOGIN. the insert values in Customer Table using tablegateway1 and get the last insert value now update this value in Login table for customer_id using table gateway2.
首先从另一个表LOGIN中删除customer_id的自动增量。使用tablegateway1在Customer Table中插入值并获取最后一个插入值现在使用table gateway2在customer_id的登录表中更新此值。
This should work.
这应该工作。
#3
0
As mentioned by Paul Sellars, if you will remove AI from one table, then your problem will be solved. In the table, where AI is defined, insert the data into their first and and get last_insert_id from there. With that value, update/insert the record into the other table where AI is not defined.
正如保罗塞拉斯所提到的,如果您将AI从一个表中移除,那么您的问题将得到解决。在定义AI的表中,将数据插入到第一个,然后从那里获取last_insert_id。使用该值,将记录更新/插入到未定义AI的另一个表中。