[英]CodeIgniter MSSQL get last_id() of composite primary key
我正在為我們公司的需要復合主鍵的應用程序工作。
根據需求,組合由RegionID和AuthorizationID組成,但是AuthroizationID不必自己唯一。 換句話說,他們希望它這樣做:
RegionID AuthorizationID
300 1
300 2
100 1
因此,我不能在授權上創建簡單的身份,而只能將RegionID放在前面。 (相信我,我竭盡所能不要這樣)
無論如何,我在MSSQL 2008中創建了一個INSERT觸發器,該觸發器輸出新插入的RegionID和AuthorizationID,並將其封裝在dbo.vwCreateRA中。 它在MSSQL Studio中效果很好。
但是,要使其完全可用,我希望能夠從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());
給我: Fatal error: Call to a member function last_id() on a non-object
如何通過CodeIgniter從視圖的INSERT中獲取輸出,或者我將不得不依賴於以下內容:
(請原諒代碼,可能不是最佳做法,僅是為了說明問題)
$sql = "SELECT TOP 1
RegionID,
Authorization
FROM
AuthorizationTable
WHERE
RegionID = " . $customer['Region'] . "
AND UserID = " . $username . "
ORDER BY
CreateDate DESC";
謝謝!
您應該使用$this->db->insert_id();
更換
$query->last_id();
同
$this->db->insert_id();
更新:
您的選擇查詢應如下所示:
$sql = "SELECT TOP 1 RegionID, Authorization
FROM AuthorizationTable
WHERE RegionID = '" . $customer['Region'] . "' AND UserID = '" . $username . "'
ORDER BY CreateDate DESC";
參見此處: https : //www.codeigniter.com/user_guide/database/helpers.html#information-from-executing-a-query
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.