cakephp data getting posted - cakephp

I have a form and I want to see it in fiddler that what data is getting posted when form is submitted. I am not able to see anything. How do u debug in cakephp. I am a newbie in it
thanks

you must have Configure::write('debug',2); defined.
then you can use debug($data); to debug any data.
also in views or layouts you can have
<?php echo $this->element('sql_dump'); ?>
to output any database queries that had took place.

Inside app/config/core.php make sure the 'debug' value is 1 or 2 . t
If you use 1 warning and errors(if exists) will display
If you use 2 warning and errors(if exists) will display and all the sql query will display(that queries are runs on that page)

Within app/config/core.php make sure the 'debug' value is greater than 0.
For any value you would like to see, call the Cake 'pr' function to output it to the page. pr() will automatically expand any nested arrays and data structures.

you have to print data by using
echo "<pre>";
print_r($this->data);
echo "</pre>";
in this,Not required to 'debug' value is 1 or 2 .

Related

Multiflash session in cakephp

I need to send more than 1 session flash with cakephp, I have found some solutions how to create a for loop and put together a deal but wanted to know if there was any native function of cake for something.
Two days ago I wrote http://www.dereuromark.de/2014/04/21/cakephp-flash-messages-2-0/
Which basically added support for this in CakePHP1.x, and therefore also for CakePHP2.x now.
It stacks multiple messages per type, as well. Details see the Wiki.
Its really not clear what you really wants- but to create multiple flash message as on documentation-
// set a bad message.
$this->Session->setFlash('Something bad.', 'default', array(), 'bad');
// set a good message.
$this->Session->setFlash('Something good.', 'default', array(), 'good');
And on your view-
echo $this->Session->flash('good');
echo $this->Session->flash('bad');
And there is a Helper you can checkout- MultiFlashHelper

Not able pass array to view in cakephp v 1.3

In the index action of my UsersController I want to pass strings and arrays to view, so that my View is render correctly. I don't know what am I doing wrong but my view file is not getting one of my array to view.
$users = $this->User->getUsers();
//Setting all variable that are present in layout
$this->set('title_for_layout','Users - Admin');
$page_headings = array("text" => "Users", "clss" => "icon-head");
$this->set('page_heading', $page_headings);
//Ends here
//This is actually going to action view file
$this->set(compact('users'));
My View file Code is like this
<div><?php echo $page_heading[0]['text']; ?></div>
<?php foreach($users as $user){ ?>
I am able to get $users variable here but noting getting $page_heading.
I am getting this error Undefined variable: page_heading.
I have tried everything like these:
$this->set($page_headings);
$this->set(compact('page_headings');
and yes I have change my variable name in view file also to page_headings after doing above code. I am not able to get it working. Can Please anybody help.
Thanks in advance.
This was happened because I was editing my live server file directly from filezilla's edit option and when I was saving it after editing file, it was somehow becoming the one line code and because of the comment tag all code that I had edited became comments.
I rectified this problem when i took fresh copy from server.
So what I learned today: After say working about half an hour on file, take updated file from server if you editing from filezilla because you don't know how it gonna save the file.
Thanks ndm for your help.

Cakephp SQL dump not showing queries

For some reason my cakephp application is not showing any of the queries made to the database. It prints the table fine, but there are not records. What could cause this?
Check to make sure you are pulling the records correctly.
$models = $this->Model->find('all');
// or
$this->Model->recursive = 0;
$this->set('models', $this->paginate());
Then when you add them in the view, be sure you are looping through them correctly:
foreach ($models as $model) {
echo $model['Model']['field_name'];
}
UPDATE
To show the SQL statements, be sure you have the following set in core.php
Configure::write('debug', 2);
Also, in the Layout, besure you have this included someone between the <body> and </body> tags:
<?php echo $this->element('sql_dump'); ?>
I assume that you are getting an empty table with just the "Nr","Query","Error", etc. column headers?
You are getting the empty table because you have "Configure::write('debug',0);" set somewhere before you have "Configure::write('debug',2);" set. Find the first instance of it and delete it or change it to "2".
I know that you have long since fixed this problem but hopefully it helps somebody else in the future.
The CakePHP debug kit can help you. After you install it, you will notice a small (pie-chart) icon on the top right of your CakePHP pages. Clicking on that will allow you to see various useful information, and most importantly for this issue, all the SQL queries that occurred in the back-end upon the page loading.
I faced a similar problem in cakephp. I found that debug($variable) has limitation to the content size. Since you are fetching huge sized content from database, it is not able to print. Try doing print_r($variable) instead. To format it properly, you can do like this
echo "<pre>".print_r($variable)."</pre>";

Pass data to default.ctp (layout file) from random controller in CakePHP

I am simply trying to pass data from my controller to my default layout file:
users_controller
$this->set('fish', 'trout');
default.ctp (layout file)
echo "You caught a " . $fish. " from the river.";
What I am trying to achieve is: echo $group['Group']['name']; in the default layout file, but the above was my first attempt to understand how the relationship actually works.
Thanks for any advise on this : )
anything that you set to the view is available in the layout.
If you set anything in the default.ctp, it will show in the layout just like you would do in a view. There's no difference.
You might not be seeing anything because you have set debug to 0 and you have an error. Try making it 2 and check it out.
As Thorpe says, you need debug set to non-zero.
Check (and change) in app/core.php (search the file for debug and you'll see the information you need).
If you're not getting any output this is most likely the problem.
debug($aVar); is IMO more useful than echo or pr as it will output the line number even if there is no other output (but you must have debug enabled!).

$session->flash()

I am using cakePHP v1.26.
In the default.ctp file,
I got a single of this code in it:
$session->flash();
I came a corss a web site in which the author suggested using this instead:
if($session->check('Message.flash')){
$session->flash();
}
I do not understand what this line of code is doing:
if($session->check('Message.flash')){...}
what is "Message.flash" in this case?
Is "Message.flash" a custom variable or
a built-in varibale which has been predefined in cakePHP?
Message.flash is the session variable name. It will be defined by cakephp, when you use $this->Session->setFlash('Your message'); from your controller.
if($session->check('Message.flash')){...} checks, if session Message.flash, which contains the flash message, exists.
Note also that contrary to the current manual description, $session->flash() does not echo the result, it just returns it, so you will need to have
echo $session->flash();
in your view.
For latest cakephp version
if(!($this->Session->check('Message.flash')));
// your code
In view section for show messages.
$this->Session->flash();

Resources