Toucan Web Design Posted March 9, 2012 Share Posted March 9, 2012 Could anyone tell me how you're supposed to properly write custom queries in CC5? There seems to be a lack of methods to actually support it, is the problem, which is starting to make working on them hard. The current problem we're having is with the database prefix. In the past we used $GLOBALS['db']->_prefix which allowed us to directly access the database prefix. This seemed to work in the past (though I'm not entirely sure why, as having checked some earlier versions, it was also marked as protected and so should have failed?) Have had limited success with putting {$this->_prefix} in the query itself, but that seems to still be throwing errors on some of the pages. Have looked everywhere for a proper way of just accessing basic functionality like that that's needed for custom queries that can't be handled by the build in select functions and such, so wondering if anyone else knows Quote Link to comment Share on other sites More sharing options...
bsmither Posted March 9, 2012 Share Posted March 9, 2012 The database class has a method, query() that developers can use for custom queries. In CC507, see line 432 in database.class.php. There is a complex query I found in one of the scripts that uses this method, and as such, also prepends the database prefix prior to the method call (using $GLOBALS['config']->get('config', 'dbprefix') if I am not mistaken). However, in order to keep my custom queries cleaner, in the query() method, find: $this->_query = $query.' '.$limit; and add before: $query = str_replace('CubeCart_', $this->_prefix.'CubeCart_', str_replace($this->_prefix, '', $query)); What this does is to strip off the table prefix if any exists, then adds the prefix to all table names. This allows for any existing queries to survive, but allows future queries to have prefixes or not (and I prefer not). Quote Link to comment Share on other sites More sharing options...
Toucan Web Design Posted March 12, 2012 Author Share Posted March 12, 2012 I thought I was imagining things when I vaguely remembered something using the get config variable.. bit of an odd place to put it really, thanks though, will give that one a try. Sadly can't go the route of modifying the database as it somewhat ruins the idea of nice modular plugins, that's why was checking there wasn't some method I was missing somewhere Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.