I am using cakephp framework. I need SQL for following statement:
$vehicle = $this->Vehicle->find('all');
How can I do that?
Please guide me.
Thank you,
Trupti
For Cakephp 1.x, you could use following code to get the last query
$dbo = $this->Vehicle->getDatasource();
$logs = $dbo->getLog();
$lastLog = end($logs['log']);
echo $lastLog['query'];
Alternatively, To get all the Queries executed in the Current HTTP Request, you can use the following code
$db =& ConnectionManager::getDataSource('default');
$db->showLog();
You have to set the Debug Mode to 2 for this to work.
Related
I am working with Laravel and I am using a database in Sql Server. I need to execute a function that was do in Sql Server but I do not how to execute it.
The function in Sql Server is something that it:
Select dbo.functionName (848,95,37)
I try to execute it with this:
DB::connection('database')->select('Select dbo.functionName ?,?,?',array(848,95,37))
but Laravel return it: Cannot access empty property
I hope you can help me.
I had this problem too, i solved that like this:
DB::connection('database')->select('Select dbo.functionName (?,?,?) as result',array(848,95,37))
May be this will work (Added parenthesis)
DB::connection('database')->select('Select dbo.functionName (?,?,?)',array(848,95,37))
I am using the below code to connect to the database in the module to write the several database queries which are not correct it seems, so please tell me what is the correct way of connecting to the database from the new module.
$con = mysql_connect(_DB_SERVER_, _DB_USER_, _DB_PASSWD_);
mysql_select_db(_DB_NAME_);
use
$query ="select * from name_table"; /*example query*/
Db::getInstance()->executeS($query);
you do not need anything else to make a query
Please, before read this guidelines to create a new module: How to create a module
And then this: Best practices of the Db Class
Including the files like the elPresta says it's old and deprecated method, after that to make a simple query, read what he wrote Matteo Enna.
Just include these files and thats it!
include(dirname(__FILE__).'/../../config/config.inc.php');
include(dirname(__FILE__).'/../../init.php');
And after all you can use methods as Matteo told u. Or read more here ( http://doc.prestashop.com/display/PS15/DB+class+best+practices )
As you know, we can use the method DB::getQueryLog() to print the sql statement in Laravel 4.
But it doesn't work in Laravel 5.
Query log is disabled by default in L5. Call
DB::connection()->enableQueryLog();
$queries = DB::getQueryLog();
before trying to log it. See the docs for more info
.
if you want to see the currently running SQL query you can use this
https://github.com/itsgoingd/clockwork
installation instructions are clearly mentioned in the README file
Using the PHPUnit administrative connection to the database would avoid "polluting" any logging or other things going on inside of our app code with an SQL command that's only being used to implement the test.
I'd like to use $this->getConnection() to grab the administrative PHPUnit connection to the database rather than call our SystemDB::query() function directly, but I can't seem to get the syntax correct. Any thoughts would be appreciated.
This worked:
$result = $this->getConnection()->getConnection();
$query = $result->query( $sql );
$my_array = $query->fetchAll();
where $sql is the query and getConnection() is implemented according to the main phpUnit manual page http://phpunit.de/manual/current/en/database.html
In certain functions I may need to do a couple of queries like so:
$user = & JFactory::getUser();
$db = & JFactory::getDBO();
$query = $db->getQuery(true);
$query->select('id');
$query->from($db->quoteName('#__users'));
$query->where('username='.$db->quote($response->username));
$db->setQuery($query);
$user_id = $db->loadResult();
if ($user_id == "")
{
//do something
}
$query1 = $db->getQuery(true);
$query1->select('app_id');
$query1->from($db->quoteName('#__app_ids'));
$query1->where('app_descr='.$db->quote($this->app_descr).' AND app_valid=TRUE');
$db->setQuery($query1);
$app_id = $db->loadResult();
I find if I don't change query to query1 I can't get this to work for the subsequent queries. Outside of Joomla I've never had to do this as I close the mysql connection use the same variable as long as it is in the right order, all is well.
Two questions:
Is this right? Or is there a better way to do this?
Do I need to check for mysql failure of loadResult? How would I go about this. Looking at the Joomla core often I see nothing but sometimes there is a mix of things to handle this.
1) It should work with the same variable name, since you are getting a new query object since your method parameter is set to true. Try calling $query->clear(); just after getting query object
$query = $db->getQuery(true);
$query->clear();
$query->select('app_id');
2) In Joomla 3 it should be something like
try
{
$db->setQuery($query);
$user_id = $db->loadResult();
}
catch (RuntimeException $e)
{
$e->getMessage();
}
And in Joomla 2.5
if ($db->getErrorNum()) {
JError::raiseWarning(500, $db->getErrorMsg());
}
Also, change
$user = & JFactory::getUser();
$db = & JFactory::getDBO();
to
$user = JFactory::getUser();
$db = JFactory::getDBO();
Objects are returned by reference anyway in PHP 5, and it will throw a warning since php 5.3+
Well, on Joomla the JFactory::getDBO() always returns the same instance of connection,(at the atual version today) so each time you set the SQL you, in true, are rewriting the previous SQL. In resume this code need multiple connections, and this way dont support, you will need to write your own version of JDatabaseDriver, maybe your own version of a global static array to control your own pool of mysqli connections. PHP is poor about pool connections, isn't as JAVA or .NET, to quick code you will need to think the logic and to try to use only one connection each time, one opened query each time, always think that the JFactory::getDBO() isn't thread safe, take care about this. You never can start one transaction inside one php without commit ou rollback, it causes problems on execution of other pages.