[英]CodeIgniter MSSQL get last_id() of composite primary key
I am working on an application for our company that requires a composite primary key. 我正在为我们公司的需要复合主键的应用程序工作。
Per the requirements, the composite is made up of RegionID and AuthorizationID but AuthroizationID does not need to be unique to itself. 根据需求,组合由RegionID和AuthorizationID组成,但是AuthroizationID不必自己唯一。 In other words they want it to do this:
换句话说,他们希望它这样做:
RegionID AuthorizationID
300 1
300 2
100 1
So I can't create a simple Identity on Authorization then just stick RegionID on the front. 因此,我不能在授权上创建简单的身份,而只能将RegionID放在前面。 (Believe me I tried my best to not make it this way)
(相信我,我竭尽所能不要这样)
Anyway, I created an INSERT trigger in MSSQL 2008 that that OUTPUTs the newly inserted RegionID and AuthorizationID, and encapsulated it in dbo.vwCreateRA. 无论如何,我在MSSQL 2008中创建了一个INSERT触发器,该触发器输出新插入的RegionID和AuthorizationID,并将其封装在dbo.vwCreateRA中。 It works great in MSSQL Studio.
它在MSSQL Studio中效果很好。
However to make it at all usable, I want to be able to get the Composite PK back from an insert inside codeigniter, to redirect the user to the newly created record. 但是,要使其完全可用,我希望能够从codeigniter中的插入取回Composite PK,以将用户重定向到新创建的记录。
$sql = "INSERT INTO ReturnAuthorizations.dbo.vwCreateRA (
[Region],
[CustomerNumber],
/* a bunch of other fields... */
[UserID]
)
VALUES
?,
?,
/* a bunch of other values */
?)";
$query = $this->db->query($sql, [
$customer['SalespersonNumber'],
$customer['CustomerName'],
/* A bunch of other variables... */
$username
]);
die(var_dump($query->last_id());
Gives me: Fatal error: Call to a member function last_id() on a non-object
给我:
Fatal error: Call to a member function last_id() on a non-object
How can I grab the OUTPUT from the view's INSERT through CodeIgniter, or will I have to rely on something like: 如何通过CodeIgniter从视图的INSERT中获取输出,或者我将不得不依赖于以下内容:
(Please excuse the code, probably not best practices, only intended to make the point of the query) (请原谅代码,可能不是最佳做法,仅是为了说明问题)
$sql = "SELECT TOP 1
RegionID,
Authorization
FROM
AuthorizationTable
WHERE
RegionID = " . $customer['Region'] . "
AND UserID = " . $username . "
ORDER BY
CreateDate DESC";
Thanks! 谢谢!
You should use $this->db->insert_id();
您应该使用
$this->db->insert_id();
Replace 更换
$query->last_id();
with 同
$this->db->insert_id();
UPDATE : 更新:
your select query should be like this : 您的选择查询应如下所示:
$sql = "SELECT TOP 1 RegionID, Authorization
FROM AuthorizationTable
WHERE RegionID = '" . $customer['Region'] . "' AND UserID = '" . $username . "'
ORDER BY CreateDate DESC";
See here : https://www.codeigniter.com/user_guide/database/helpers.html#information-from-executing-a-query 参见此处: https : //www.codeigniter.com/user_guide/database/helpers.html#information-from-executing-a-query
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.