How to free/release Joomla db connection after the Query execute? - joomla

Below are my code lines
$db = JFactory::getDbo();
$query =$db->getQuery(true);
$query ->select('id')
->from($db->quoteName('#_menu'))
->where('alias'. "=" ."'about'".'AND published = 1');
$db->setQuery($query);
$result = $db->loadObjectList();
how to release db connection after query execution ?

Trying to close the connection in Joomla is pointless because the core and even 3rd party extensions that call JFactory::getDbo(); so it will be reopened.
If you really need to, then you can use $db->disconnect(); which basically calls mysqli_close() or the equivalent to whatever database drive you're using.

Although i suppose after each query the database connection gets closed. Still there are two methods that can be used
If you want to close or disconnect the database session, you can
use:
$db->disconnect(); Ref:
http://api.joomla.org/cms-3/classes/JDatabaseDriver.html#method_disconnect
You can also use this instead after a query result is passed to a
Variable:
$db->freeResult(); Ref :
http://api.joomla.org/cms-3/classes/JDatabaseDriverMysql.html#method_freeResult

Related

AES_DECRYPT with Codeigniters Active Record

Hi has anyone had experience with using AES_DECRYPT and codeigniters active record. I've tried using the following line:
$query = $this->db->select("AES_DECRYPT(testing,'$key') as testing");
but continue to get an sql syntax error. I've tried using a manual standard sql string which works but would prefer to stick with active record if I can.
CodeIgniter is trying to escape that, and it has no idea how to. Add FALSE as the 2nd parameter to tell it not to escape it.
// We need to escape this value before the query
$key = $this->db->escape($key);
// Tell CodeIgniter not to escape this
$this->db->select("AES_DECRYPT(testing, $key) as testing", FALSE);

Magento Tables and how they work with the database

I been trying to understand Magento and I read many things on the wiki, but I couldn't figure out How does Magento works with database tables? because I didn't see any SQL
I would reccomend read over this blog post from Alan Storm:
http://alanstorm.com/magento_models_orm
He explains quite abit about the Magento ORM system, and in my opinion that entire site is a great resource for any Magneto developer.
If you watch your MySQl log, the calls made by magento can sometimes be 500 lines long or longer ... These calls are dynamically constructed using XML files. The best way to manipulate Magento data manually is to use MAGE:: calls or use a direct database connection by using:
$read = $resource->getConnection('core_read');
$sql = "select * from [YOUR_TABLE] where 1 limit 1";
$result = $read->query($sql);
It's either that or calls that look like:
$value = 'some value';
$item->setData('some_key', $value);
$item->save();
Magento is object oriented, so those are the most commonly accepted and used ways to retrieve/set data in Magento. I hope that helps.
Read chapter 5 onwards from the knowledge base.
You are not really asking a question so no one can help on the specifics, I always find that you learn best by doing, I find the best way to mess around with magento is to create a test.php file in shell/ with the following: (for example)
<?php
require('abstract.php');
class Test extends Mage_Shell_Abstract
{
function run(){ //call your functions here
echo 'running ..';
$this->database();
}
function database() { //you can create as many functions as you like
$entityId = '4449'; //product id
$product=Mage::getModel("catalog/product")->load($entityId);
var_dump($product->getAttributeText('size'));
}
}
$test = new Test();
$test -> run();
Then you can run from console:
php test.php
and it returns in my example
running ..string(11) "Extra Large"
Hope this helps you, next time be more specific.

Joomla 2.5 - how to access module params?

Does anyone know how I can get the value of the module's 'access' parameter (e.g. public, registered..etc) from within the module's helper.php? I can only see $params, which are all the module-type specific parameters, not the generic params.
I need to retrieve the access permissions and feed them to a plugin.
Thanks :)
Do you mean when you are rendering a module on a page or you want to know when it isn't being rendered?
JModuleHelper will only render a module if the user has access.
But if you want to find out for other reasons than rendering you need to query jos_modules.
Module access level actually can't be found by querying the jos_extensions table since that does not refer to specific instances of modules (e.g. main menu versus some other menu both of which are instances of mod_menu and which may have different access levels). (Use your own prefix of course.)
P.S. I think it might be worth it to put that in as a pull request so you could just get it from the helper. Seems somewhat arbitrary to me that it's not returned in the array.
you will need to run an SQL query for this.
For 1 specific module:
$db =& JFactory::getDBO();
$query = 'SELECT access FROM #__modules WHERE element="mod_yourmodule"';
$db->setQuery($query);
$result = $db->loadResult();
print_r($result);
Don't forget to change mod_yourmodule to whatever module you want the data for.
For all modules:
$db =& JFactory::getDBO();
$query = 'SELECT access FROM #__modules';
$db->setQuery($query);
$result = $db->loadResult();
print_r($result);
Hope this helps

Can DBI statement handles use cached calls to execute()?

I have an application where the database rarely changes, and the application requires many reads from the database that is slowing down the performance quite significantly. Many of these reads are exactly the same. So I want to get DBI to cache the results of a database read.
For example,
$sth = $dbh->prepare('SELECT a, b FROM a_table WHERE c = ?');
$sth->execute(5);
$sth->execute(2);
$sth->execute(5); # this call loads the cached result set
I first thought this is what prepare_cached does, but I realised that it only caches the statement handle itself and not actual executions of the statement handle.
I suppose I could achieve what I want by wrapping the statement execution inside a memoized sub. But I'm just seeing if there is a shortcut within DBI itself.
as you said, the prepare_cached is related to the statement handle, and you need to cache the results of the execution. Memoize is good, but probably you need invalidate the cache from time to time, and re-execute the query to get a fresh copy from database.
I'd use the Cache (http://search.cpan.org/perldoc?Cache) module. I've just copied this fragment from the introduction:
use Cache::File;
my $cache = Cache::File->new( cache_root => '/tmp/cacheroot' );
my $customer = $cache->get( $name );
unless ($customer) {
$customer = get_customer_from_db( $name );
$cache->set( $name, $customer, '10 minutes' );
}
return $customer;
You can use in memory cache instead of File. This example uses the $customer value from cache if exists and it's valid, otherwise gets a fresh value and store at cache (with 10 minutes of life).
Hope this helps.

Codeigniter pre_system hook for DB driven dynamic controller selection - best approach?

Although I can tentatively see a solution to this, I was wondering if there may be a glaringly obvious simpler approach.
My aim is to use the first segment of a given URI to query the DB as to which controller should be run.
I assume I would have to reform the URI with the resultant controller name in segment 1, then allow the system to continue processing as normal (hence a pre_system hook).
Although not essential I would also like to hold a couple of other variables from the same DB request to be used later in the call stack, and assume this would have to be done using global variables?
Any better suggestions would be gladly received.
Thanks.
Should it be of use to anyone else, here is the code to acheive the desired result. This does however not take into account passing additional variables because I can live without them.
function set_controller()
{
include_once APPPATH.'config/database.php'; //Gather the DB connection settings
$link = mysql_connect($db[$active_group]['hostname'], $db[$active_group]['username'], $db[$active_group]['password']) or die('Could not connect to server.' ); //Connect to the DB server
mysql_select_db($db[$active_group]['database'], $link) or die('Could not select database.'); //Select the DB
$URI = explode('/',key($_GET)); //Break apart the URL variable
$query = 'SELECT * FROM theDomainTable WHERE domainName = "'.$URI[1].'"'; //Query the DB with the URI segment
if($results = mysql_fetch_array(mysql_query($query))){ //Only deal with controller requests that exist in the database
$URI[1] = $results['controllerName']; //Replace the controller segment
$_GET = array(implode('/',$URI)=>NULL); //Reconstruct and replace the GET variable
}
mysql_close($link); //Close the DB link
}
I wouldn't use global variables, Id prefer to store it in a library for retrieval later if possible. Global variables are kind of messy in the context of CI.
Although at pre_system Only the benchmark and hooks class have been loaded at this point. This means you're pretty-much stuck with global variables unless you can find a way to select the controller on pre_controller as all the base-classes are loaded and you can put the data somewhere more logical.

Resources