{"id":425,"date":"2012-11-21T21:26:09","date_gmt":"2012-11-21T20:26:09","guid":{"rendered":"http:\/\/codingexplained.com\/?p=425"},"modified":"2017-06-11T22:22:25","modified_gmt":"2017-06-11T20:22:25","slug":"using-transactions-in-concrete5","status":"publish","type":"post","link":"https:\/\/codingexplained.com\/coding\/php\/concrete5\/using-transactions-in-concrete5","title":{"rendered":"Using Transactions in concrete5"},"content":{"rendered":"<p>Using transactions in concrete5 can often be desirable, but it is not available by default. It can, however, be enabled with a single line of code. concrete5 makes use of a database abstraction layer called ADODB. Looking in <a href=\"http:\/\/phplens.com\/lens\/adodb\/docs-adodb.htm\" rel=\"external nofollow\" title=\"ADODB manual\">ADODB&#8217;s manual<\/a>, it is clear that transactions are indeed supported.<\/p>\n<p>So why is it not enabled by default in concrete5? Well, it appears that concrete5 uses the ADODB_mysql driver class by default. Digging into this class&#8217; source code, you will find the following:<\/p>\n<pre><code class=\"php\">function BeginTrans() {\r\n\tif ($this->debug) {\r\n\t\tADOConnection::outp(\"Transactions not supported in 'mysql' driver. Use 'mysqlt' or 'mysqli' driver\");\r\n\t}\r\n}<\/code><\/pre>\n<p>As you can probably imagine, it can be hard to figure out why transactions are not working; all of the transaction methods are indeed available and no errors are generated. The calls are simply ignored, and you may be left wondering why your transaction does not roll back on failure. As the code clearly states, we have to make use of another driver in order to use transactions. Luckily, this is as simple as adding the line below to concrete5&#8217;s <span class=\"code\">config\/site.php<\/span> file.<\/p>\n<pre><code class=\"php\">define('DB_TYPE', 'mysqlt');<\/code><\/pre>\n<p>Alternatively, you should be able to enter &#8220;mysqli&#8221; instead, but I have not personally tested this. The database object, which you retrieve from concrete5&#8217;s loader helper, actually represents ADODB&#8217;s connection object. That is why you can make use of the methods exposed in the previously referenced ADODB manual. Below is given an example of using ADODB&#8217;s so-called <a href=\"http:\/\/phplens.com\/lens\/adodb\/docs-adodb.htm\" title=\"ADODB Smart Transactions\" rel=\"external nofollow\">Smart Transactions<\/a> in concrete5.<\/p>\n<pre><code class=\"php\">$db = Loader::db();\r\n\r\ntry {\r\n\t$db->StartTrans();\r\n\r\n\t$db->Execute('INSERT INTO MyFirstTable (first, second) VALUES (?, ?)', array('first', 'second'));\r\n\t$db->Execute('INSERT INTO MySecondTable (first, second) VALUES (?, ?)', array('first', 'second'));\r\n\r\n\t$db->CompleteTrans(); \/\/ Commits or rolls back automatically\r\n} catch (Exception $e) {\r\n\t\/\/ An exception may be thrown before calling CompleteTrans() above, so we must handle rolling back here as well\r\n\r\n\t$db->FailTrans(); \/\/ Force rollback (just to be sure)\r\n\t$db->CompleteTrans(); \/\/ Do rollback (the actual rollback happens here and not by calling FailTrans())\r\n}<\/code><\/pre>\n<p>For more information on how to use transactions in ADODB (and thereby concrete5), I encourage you to take a look in the <a href=\"http:\/\/phplens.com\/lens\/adodb\/docs-adodb.htm#ex11\" title=\"ADODB Smart Transactions\" rel=\"external nofollow\">manual<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you have been trying to use transactions in concrete5, then you might have been left wondering why on Earth it is not working. As it turns out, this functionality is not enabled by default. See why and how to enable it here, along with an example of how to use transactions in concrete5.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"jetpack_post_was_ever_published":false,"jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false}}},"categories":[55],"tags":[62,148,40,63],"series":[],"jetpack_publicize_connections":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.4 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Using Transactions in concrete5<\/title>\n<meta name=\"description\" content=\"Using transactions in concrete5 is easy, but not enabled by default. In this article, we will show you how to enable transactions and how to use them.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/codingexplained.com\/coding\/php\/concrete5\/using-transactions-in-concrete5\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Using Transactions in concrete5\" \/>\n<meta property=\"og:description\" content=\"Using transactions in concrete5 is easy, but not enabled by default. In this article, we will show you how to enable transactions and how to use them.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/codingexplained.com\/coding\/php\/concrete5\/using-transactions-in-concrete5\" \/>\n<meta property=\"og:site_name\" content=\"Coding Explained\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/codingexplained\" \/>\n<meta property=\"article:author\" content=\"https:\/\/www.facebook.com\/codingexplained\" \/>\n<meta property=\"article:published_time\" content=\"2012-11-21T20:26:09+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2017-06-11T20:22:25+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/codingexplained.com\/wp-content\/uploads\/2015\/11\/codingexplained-fb-promote.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"444\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Bo Andersen\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@codingexplained\" \/>\n<meta name=\"twitter:site\" content=\"@codingexplained\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Bo Andersen\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/codingexplained.com\/coding\/php\/concrete5\/using-transactions-in-concrete5\",\"url\":\"https:\/\/codingexplained.com\/coding\/php\/concrete5\/using-transactions-in-concrete5\",\"name\":\"Using Transactions in concrete5\",\"isPartOf\":{\"@id\":\"https:\/\/codingexplained.com\/#website\"},\"datePublished\":\"2012-11-21T20:26:09+00:00\",\"dateModified\":\"2017-06-11T20:22:25+00:00\",\"author\":{\"@id\":\"https:\/\/codingexplained.com\/#\/schema\/person\/e19c92ec991f571605f047cefeaa950d\"},\"description\":\"Using transactions in concrete5 is easy, but not enabled by default. In this article, we will show you how to enable transactions and how to use them.\",\"breadcrumb\":{\"@id\":\"https:\/\/codingexplained.com\/coding\/php\/concrete5\/using-transactions-in-concrete5#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/codingexplained.com\/coding\/php\/concrete5\/using-transactions-in-concrete5\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/codingexplained.com\/coding\/php\/concrete5\/using-transactions-in-concrete5#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/codingexplained.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Using Transactions in concrete5\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/codingexplained.com\/#website\",\"url\":\"https:\/\/codingexplained.com\/\",\"name\":\"Coding Explained\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/codingexplained.com\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/codingexplained.com\/#\/schema\/person\/e19c92ec991f571605f047cefeaa950d\",\"name\":\"Bo Andersen\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/codingexplained.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/28f5826f9d5d544b0c5e1ec321dfdfb8?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/28f5826f9d5d544b0c5e1ec321dfdfb8?s=96&d=mm&r=g\",\"caption\":\"Bo Andersen\"},\"description\":\"I am a back-end web developer with a passion for open source technologies. I have been a PHP developer for many years, and also have experience with Java and Spring Framework. I currently work full time as a lead developer. Apart from that, I also spend time on making online courses, so be sure to check those out!\",\"sameAs\":[\"https:\/\/codingexplained.com\",\"https:\/\/www.facebook.com\/codingexplained\",\"https:\/\/www.linkedin.com\/in\/ba0708\",\"https:\/\/twitter.com\/codingexplained\",\"https:\/\/www.youtube.com\/c\/codingexplained\"],\"url\":\"https:\/\/codingexplained.com\/author\/andy\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Using Transactions in concrete5","description":"Using transactions in concrete5 is easy, but not enabled by default. In this article, we will show you how to enable transactions and how to use them.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/codingexplained.com\/coding\/php\/concrete5\/using-transactions-in-concrete5","og_locale":"en_US","og_type":"article","og_title":"Using Transactions in concrete5","og_description":"Using transactions in concrete5 is easy, but not enabled by default. In this article, we will show you how to enable transactions and how to use them.","og_url":"https:\/\/codingexplained.com\/coding\/php\/concrete5\/using-transactions-in-concrete5","og_site_name":"Coding Explained","article_publisher":"https:\/\/www.facebook.com\/codingexplained","article_author":"https:\/\/www.facebook.com\/codingexplained","article_published_time":"2012-11-21T20:26:09+00:00","article_modified_time":"2017-06-11T20:22:25+00:00","og_image":[{"width":1200,"height":444,"url":"https:\/\/codingexplained.com\/wp-content\/uploads\/2015\/11\/codingexplained-fb-promote.png","type":"image\/png"}],"author":"Bo Andersen","twitter_card":"summary_large_image","twitter_creator":"@codingexplained","twitter_site":"@codingexplained","twitter_misc":{"Written by":"Bo Andersen","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/codingexplained.com\/coding\/php\/concrete5\/using-transactions-in-concrete5","url":"https:\/\/codingexplained.com\/coding\/php\/concrete5\/using-transactions-in-concrete5","name":"Using Transactions in concrete5","isPartOf":{"@id":"https:\/\/codingexplained.com\/#website"},"datePublished":"2012-11-21T20:26:09+00:00","dateModified":"2017-06-11T20:22:25+00:00","author":{"@id":"https:\/\/codingexplained.com\/#\/schema\/person\/e19c92ec991f571605f047cefeaa950d"},"description":"Using transactions in concrete5 is easy, but not enabled by default. In this article, we will show you how to enable transactions and how to use them.","breadcrumb":{"@id":"https:\/\/codingexplained.com\/coding\/php\/concrete5\/using-transactions-in-concrete5#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/codingexplained.com\/coding\/php\/concrete5\/using-transactions-in-concrete5"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/codingexplained.com\/coding\/php\/concrete5\/using-transactions-in-concrete5#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/codingexplained.com\/"},{"@type":"ListItem","position":2,"name":"Using Transactions in concrete5"}]},{"@type":"WebSite","@id":"https:\/\/codingexplained.com\/#website","url":"https:\/\/codingexplained.com\/","name":"Coding Explained","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/codingexplained.com\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/codingexplained.com\/#\/schema\/person\/e19c92ec991f571605f047cefeaa950d","name":"Bo Andersen","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/codingexplained.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/28f5826f9d5d544b0c5e1ec321dfdfb8?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/28f5826f9d5d544b0c5e1ec321dfdfb8?s=96&d=mm&r=g","caption":"Bo Andersen"},"description":"I am a back-end web developer with a passion for open source technologies. I have been a PHP developer for many years, and also have experience with Java and Spring Framework. I currently work full time as a lead developer. Apart from that, I also spend time on making online courses, so be sure to check those out!","sameAs":["https:\/\/codingexplained.com","https:\/\/www.facebook.com\/codingexplained","https:\/\/www.linkedin.com\/in\/ba0708","https:\/\/twitter.com\/codingexplained","https:\/\/www.youtube.com\/c\/codingexplained"],"url":"https:\/\/codingexplained.com\/author\/andy"}]}},"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p3mJkW-6R","_links":{"self":[{"href":"https:\/\/codingexplained.com\/wp-json\/wp\/v2\/posts\/425"}],"collection":[{"href":"https:\/\/codingexplained.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codingexplained.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codingexplained.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codingexplained.com\/wp-json\/wp\/v2\/comments?post=425"}],"version-history":[{"count":7,"href":"https:\/\/codingexplained.com\/wp-json\/wp\/v2\/posts\/425\/revisions"}],"predecessor-version":[{"id":3047,"href":"https:\/\/codingexplained.com\/wp-json\/wp\/v2\/posts\/425\/revisions\/3047"}],"wp:attachment":[{"href":"https:\/\/codingexplained.com\/wp-json\/wp\/v2\/media?parent=425"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codingexplained.com\/wp-json\/wp\/v2\/categories?post=425"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codingexplained.com\/wp-json\/wp\/v2\/tags?post=425"},{"taxonomy":"series","embeddable":true,"href":"https:\/\/codingexplained.com\/wp-json\/wp\/v2\/series?post=425"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}