how to print query in CakePHP - cakephp-2.2

Actually,
I have written this code in AppController.php in CakePHP 2.2.2
class AppModel extends Model
{
function getLastQuery()
{
$dbo = $this->getDatasource();
$logs = $dbo->getLog();
$lastLog = end($logs['log']);
return $lastLog['query'];
}
}
I tried following code to print output query
echo $this->AssetModel->getLastQuery();
$this->render('sql');
Is any body know to the point solution for that....?

I use this in CakePHP 2.6.11 to print Queries from Model in browser
debug($this->YOURMODEL->getDataSource()->getLog(false, false)); exit;

Hi you can use the below line to put into your layout file
<?php echo $this->element('sql_dump'); ?>
this will print all running query running in current action.
You can also use cakephp debugkit plugin.

Related

Ho to call variables items on Joomla Component

I'm trying to create my first component in Joomla but I can't understand how it works really :D
I create a basic component with "Component Creator". (I saved a lot of time...).
Now, I have a table in my DB where I've put my data:
id = 1
name = Andrea
note = Ciao
Now, I want to call it from the page using Joomla Language.
On my models/component.php i wrote:
class Variabili extends JModelLegacy
{
function estraivariabili()
{
$db =& JFactory::getDBO();
$db->setQuery("SELECT * FROM #__table")->loadObjectList();
return $value;
}
}
and on my default.php I wrote
$model=$this->Variabili();
//call the method
$items=$model->estraivariabili();
//print
print_r($items);
But on the page I have this error:
0 Call to undefined method Calcolo_imposteViewCalcoloonline::Variabili()
Where is a mistake?
Please be gentle with me because I'm a beginner: D
Thanks in advance
Andrea
You have committed a few wrongs. I've rewritten your functions so that you can get it. Let's have a look-
The model, it looks almost okay. Just change it like-
class Variabili extends JModelLegacy
{
// Make the function public
public function estraivariabili()
{
$db = &JFactory::getDBO();
// Put the result into a variable first, then return it.
$value = $db->setQuery("SELECT * FROM #__table")->loadObjectList();
return $value;
}
}
And now call the model functions not from default.php, rather than write your code inside view.html.php file.
Inside the display function of view.html.php file first, get the model instance by using getModel() function.
$model = $this->getModel();
Now you can get the items by using this $model class instance.
$this->items = $model->estraivariabili();
This will bring you the data from the database table. And you can use the data at default.php file.
Just try at default.php file-
print_r($this->items);

How to check if a YII app is running from a console or from browser?

I'm new to YII framework and i'd like to know if there's way to know/check if you are running from console or in a browser?
Thanks!
This reply is a bit late but there is a Yii-specific way to do this:
In Yii1 you can do:
if (Yii::app() instanceof CConsoleApplication)
In Yii2 that would be:
if (Yii::$app instanceof Yii\console\Application)
Hope that's useful to someone...
You should also be able to do:
echo get_class(Yii::app());
which will tell you what type of app you're in ...
Same way you would determine if a PHP application is being run in the console or not.
What is the canonical way to determine commandline vs. http execution of a PHP script?
check Yii::$app->id
when running from console Yii::$app->id = 'app-console'
when running from frontend (browser) Yii::$app->id = 'app-frontend'
The most efficient way seems to define in the root file index.php this line :
define ('WEBAPP', true)
Later you can check in any point the application
if (defined('WEBAPP')) {
echo "This is webapp";
} else {
echo "app was launched via console";
}
Checked in Yii 1.7
You can use
if(is_a(Yii::$app,'yii\console\Application'))
for console, and
if(is_a(Yii::$app,'yii\web\Application'))
for web.
https://stackoverflow.com/a/30635800/4916039
I am using Yii 1 and I use this function to check
public static function isWebRequest()
{
return Yii::app() instanceof CWebApplication;
}
public static function isConsoleRequest()
{
return Yii::app() instanceof CConsoleApplication; //!self::isWebRequest();
}
I put these functions in a helper(Componenet) Class and I use it as:
if(MyRandomHelper::isConsoleRequest())
{
Email::shoot();
}

How to call a console command in web application action in Yii?

I have a console command to do a consumer time, AND I need to know how to call (execute) it in a web application action in YII.
class MyCommand extends CConsoleCommand{
public function actionIndex(){
$model = new Product();
$model->title = 'my product';
...
$model->save();
.
.
.
}
}
I want to execute this code.
try this:
Yii::import('application.commands.*');
$command = new MyCommand("test", "test");
$command->run(null);
The 2 parameters with value "test" must be set but do not have an impact, they are used for the --help option when using the console.
/**
* Constructor.
* #param string $name name of the command
* #param CConsoleCommandRunner $runner the command runner
*/
public function __construct($name,$runner)
{
$this->_name=$name;
$this->_runner=$runner;
$this->attachBehaviors($this->behaviors());
}
https://github.com/yiisoft/yii/blob/master/framework/console/CConsoleCommand.php#L65
Try this
Yii::import('application.commands.*');
$command = new GearmanCommand('start', Yii::app()->commandRunner);
$command->run(array('start', '--daemonize', '--initd'));
where array('start', '--daemonize', '--initd') is a action and action parameters
I had same problem - i need to call action from inside controller and from command
I said same problem because it actually same - you have action which you need to call from console, and call it from controller too.
If you need to call an action(command) as a part of controller action, then i think you need to modify this solution a little. Or is my solution is enough for you?
So here is my solution:
first create action as said in http://www.yiichina.net/doc/guide/1.1/en/basics.controller#action
class NotifyUnsharedItemsAction extends CAction
{
public function run()
{
echo "ok";
}
}
then in controller action is loaded as usuall:
class TestController extends Controller
{
public function actions() {
return array(
'notifyUnsharedItems'=>'application.controllers.actions.NotifyUnsharedItemsAction',
);
}
and in command i run action in such way:
class NotifyUnsharedItemsCommand extends CConsoleCommand
{
public function run($args)
{
$action = Yii::createComponent('application.controllers.actions.NotifyUnsharedItemsAction',$this,'notify');
$action->run();
}
}
Accepting that we are on linux server, for Yii 1.1 real life example would be:
$run = '/usr/bin/php ' . Yii::getPathOfAlias('root').'/yiic' [command]
exec(sprintf("%s > %s 2>&1 & echo $! >> %s", $run, '/dev/null', '/dev/null'));
This will run Yii console command in the background.
Yii is PHP -> you can use the standard php constructs specified at http://php.net/manual/en/function.exec.php and the related methods near the bottom of the page, depending on what exactly you want to achieve.
Also, another very clean solution from cebe on gist:
<?php
// ...
$runner=new CConsoleCommandRunner();
$runner->commands=array(
'commandName' => array(
'class' => 'application.commands.myCommand',
),
);
ob_start();
$runner->run(array(
'yiic',
'idbrights',
));
echo nl2br(htmlentities(ob_get_clean(), null, Yii::app()->charset));
Yii::app()->end();
Typically what you should do in these situations is refactor.
Move the "common" code out of the MyCommand and place it into a class located in the components folder.
Now you can place any head on top of the "common" code without altering your functionality. For example:
protected/components/Mywork.php:
<?php
class Mywork
{
public function doWork()
{
$model = new Product();
$model->title = 'my product';
...
$model->save();
...
}
}
protected/controller/MyworkController.php:
<?php
class MyworkController
{
public function actionDowork()
{
$mywork = new Mywork;
...
}
}
protected/commands/MyworkCommand.php:
<?php
class MyworkCommand extends CConsoleCommand
{
public function run($args)
{
$mywork = new Mywork;
...
}
}
This approach makes testing easier too as you can test Mywork as a single unit outside of the view you are using.

wsadmin: jacl: AdminApp list <scope?> WebSphere 5.x

I am trying to list applications installed on particular server below command works fine on WAS 6.x and 7 however I cannot make the same on WAS 5.x
wsadmin> $AdminApp list /WebSphere:cell=cell01,node=node01,server=server1/
Also, $AdminApp help list does not show optional scope parameter.
Could you please advise ?
Thanks
I don't have access to v5 right now to test, but something like this might work:
proc listAppsByTarget {target} {
global AdminApp
set result []
regsub -all / $target "" target
foreach app [$AdminApp list] {
foreach line [split [$AdminApp view $app -MapModulesToServers] "\r\n"] {
if [regexp "^Server: ${target}($|,)" $line] {
lappend result $app
break
}
}
}
return $result
}
This will print any application that has a module targetted to the specified server. Used like this:
wsadmin>listAppsByServerTarget /WebSphere:cell=cell,node=node,server=server1/
DefaultApplication
I found the way, however it's not the same output, it needs to be parsed to get the details.
wsadmin > $AdminControl queryName type=Application,node=node01,process=server1
In case there is another way please let me know.

How to put an variable in a language file of Codeigniter?

I'm using Codeigniter 2.0.1 and I'd like to put an variable in a language line. For example: if an user wants to register an account, and that username already exists I would like to put in my language line "This username $username is alrady in use". I saw in the validation error language lines that they used %s as variable. But if I put this in my custom authentication error lang file I just get a plain %s instead of a variable.
It doen't look like it is possible with the default Lang class. Personally I did it that way.
First a i18n_helper :
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
if ( ! function_exists('line_with_arguments'))
{
function line_with_arguments($line, $swap)
{
return str_replace('%s', $swap, $line);
}
}
and then I call it in my controller :
<?php
class Home extends CI_Controller
{
public function index()
{
$this->lang->load('test', 'english');
$this->load->helper('i18n');
echo line_with_arguments($this->lang->line('test'), 'Matt');
}
}
and my lang file :
<?php
$lang['test'] = 'Hello %s';