douguwo2275 2018-06-08 14:02
浏览 71
已采纳

CodeIgniter MSSQL获取复合主键的last_id()

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. 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. (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. It works great in 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.

$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

How can I grab the OUTPUT from the view's INSERT through CodeIgniter, or will I have to rely on something like:

(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!

  • 写回答

1条回答 默认 最新

  • dtlh12053 2018-06-08 14:07
    关注

    You should use $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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么