简体   繁体   English

Zend Framework 2和MySQL查询

[英]Zend Framework 2 And mySql Query

My situation is that I have data to be added in two tables Login and Customer Details in MySQL. 我的情况是我要在MySQL中的两个表Login和Customer Details中添加数据。 The data is being collected by Zend Framework 2 one form and being added into Database at the same time. 数据将由Zend Framework 2一种形式收集并同时添加到数据库中。 Both Tables in MySQL has a Customer_ID. MySQL中的两个表都有一个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. 在两个表中,customer_id都是自动递增的。我希望这样,以便通过``客户详细信息''中的形式添加数据时,Customer_ID会自动递增,并将其值复制到登录表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. 到目前为止,我已经使用LastInserverValue()进行了尝试,已经尝试将Login的Customer_ID设置为FK,并且尝试手动将customer_ID分配给Login时没有碰到麻烦。 With the above methods I used they only added but Customer_ID are not the same. 使用上面的方法,我只添加了它们,但是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中的两个表使用两个tablegateway,所以我不知道如何从tablegateway1到tablegateway 2获取最后一个插入值。

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. 您可能还会对登录表上的登录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,然后在登录表中创建一个插入

Eg 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 created_at

In both table the customer_id is Auto increment 在两个表中,customer_id为自动递增

First remove the auto increment for customer_id from another table LOGIN. 首先从另一个表LOGIN中删除customer_id的自动增量。 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. 现在,使用tablegateway1在Customer Table中插入值并获取最后一个插入值,然后使用表gateway2在Login表中为customer_id更新此值。

This should work. 这应该工作。

As mentioned by Paul Sellars, if you will remove AI from one table, then your problem will be solved. 如Paul Sellars所述,如果您要从一张桌子中删除AI,那么您的问题将得到解决。 In the table, where AI is defined, insert the data into their first and and get last_insert_id from there. 在定义AI的表中,将数据插入到它们的first中,然后从那里获取last_insert_id。 With that value, update/insert the record into the other table where AI is not defined. 使用该值,将记录更新/插入到未定义AI的另一个表中。

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

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