We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 29860
    • 59 Posts
    Hi
    How do I instantiate the xPDO object to use the current connection, so I can write my own sql? I am running this within a snippet.

    I have much more complex SQL which would be much easier to write in SQL using prepared statements so I rather not use the ORM for now.

    At the moment, I am doing :

    $dsn = 'mysql:host='.$hostname.';dbname='.$dbname;
    $xpdo= new xPDO($dsn,$username,$password);
    $stmt = $xpdo->prepare("INSERT INTO outlinks_log
                                        (`trackTy`,`tracking_code`)
                                        VALUES(':trackTy',':tracking_code')");
    $stmt->bindParam(':trackTy', $this->dcstormTracking);
    $stmt->bindParam(':tracking_code', $this->a8id);
    $stmt->execute();
    
    


    Do I have to get the db details separately from the config?
      • 26903
      • 1,336 Posts
      The MODx class extends xPDO so you should just be able to do $modx->prepare etc.

        Use MODx, or the cat gets it!
        • 22303 MODX Staff
        • 10,725 Posts
        NOTE: But do not ever change the connection MODx is using. If you need to connect to a different database, you will need your own PDO (or xPDO) instance.