Jump to content

Custom queries on the database


Recommended Posts

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

Link to comment
Share on other sites

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).

Link to comment
Share on other sites

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

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...