how do i see cakephp database save errors? - database

if i have a cake php saveAll method like so:
if ($this->Video->saveAll($this->data)){
... // stuff that never happens, sadly
} else {
...
$this->Session->setFlash('boo! hss! error here');
}
how do i print out the database error? I tried:
$this->Session->setFlash('boo! hss! error here' . print_r($this->Video->validationErrors,true);
but that didn't work (it just showed me an empty array)
cheerio!
UPDATE:
ah. So, the problem is that, while normally i'd get the database error, i was using the old prg mechanism, and cake doesn't (magically) show the db errors on redirect pages.
Fair enough, but in the future, how the heck am i meant to see the db errors on a redirect page (that is, the question still stands, its just that most people probably just SEE the error, and don't need to do anything to get it)

make sure debug is set to 2 in config/core.php
print error messages to the log file like so:
$this->log(print_r($this->Video->validationErrors, true));

Related

How to debug delete() in CakePHP

how to debug delete() in CakePHP? When I do:
debug($this->Sessions->delete($s));
I get this error:
{
"message": "SQLSTATE[42S22]: Column not found: 1054 Unknown column \u0027session_id\u0027 in \u0027where clause\u0027",
"url": "\/api\/sessions\/delete\/2856",
"code": 500,
"file": "\/var\/www\/vendor\/cakephp\/cakephp\/src\/Database\/Statement\/MysqlStatement.php",
"line": 39
}
EDIT: Figured out my problem, I had called the session_id column "sessions_id" in the table by mistake. I updated this, and it solved my problem. However I'd really like to know how to debug delete() and find() properly. I noticed there is a debugging console bin/cake console. Then help to debug stuff, but I dont know how to use it, and cant seem to find any documentation on this. Can anyone help out? Thanks.
The error message stems directly from the DBMS, not from CakePHP, and it tells you pretty much all you need to know about why the query failed, there's not really a way to get more info on that.
Unless you're talking about the line where the exception is being thrown, pointing to a specific line would be rather complicated, as in this case there isn't an exact line causing a problem, the CakePHP side is working perfectly fine, you just mis-configured/named things, which led to a non-working SQL query being generated.
I would suggest to use a proper IDE and a debugger like XDebug that can halt on exceptions, then you have your stacktrace and a bunch of contextual information right at hand, and you can easily step through the code. A plain stacktrace can also be found in the logs (/logs/*.log).

How i can get the error when cake are saving?

I have a form, with some fields. When i try save, not happens and I don´t know what´s the problem.
How can i see whats happening on the save moment ?!
Thanks you!.
These are the most likely causes for a failed save:
incorrect data array structure, debug data passed for saving
failing validation, debug($this->SomeModel->validationErrors) after save
beforeSave/beforeValidate callback in model or attached behavior not returning true
sql error, turn up debug to 2
Anywhere in your view file, you get the errors like this :
<?php debug($this->AnyModel->validationErrors);?>
Aside from debugging, you display field-by-field error like this :
<?php $this->Form->error('fieldname'); ?>
This will return the message from validation rules, only if the field has thrown an error.
Hope this helps!

CakePHP 1.3: Issue with saveField

I dont understand why I am not able to update a field on the database based on the following code:
$this->User->id = 1;
$this->User->saveField('image','img/default_pic.png');
Basically, I want to change the current image in the Db with a new one.
The code above just clears the value that is currently in the image field, but does not add anything.
As an example, this is what happens:
id username image
=============================
1 admin mypic.jpg
2 john johnPic.jpg
After the code above is executed I get the following result
id username image
=============================
1 admin
2 john johnPic.jpg
I am confused at what is actually happening
SOLVED!
I decided to go back and check on my user.php model class and realized that I had attempted to use MeioUpload before and gave up, but I never removed the var actsAs entry.
As soon as I commented it out, I am now able to upload pictures.
For anyone else that might come across this issue, beforeSave might also caused these kind of problems, according to the following blog: http://blog.phplabs.net/2011/11/cakephp-savefield-not-working.html
Thanks,
What does the sql log say is happening?
My guess is you are actually doing something like
$this->User->saveField('image', $variable)
and $variable is either misspelled or is empty.

Cakephp 2.0 ReCaptcha plugin always wrong

I've taken a reCaptcha plugin from this guy
(github link of the plugin)
I've entered the following code form in my view:
[form creation]
[table]
[inputs]
[/table]
echo $this->Recaptcha->show(array('theme' => 'white'));
echo $this->Recaptcha->error();
[/form]
I've followed the steps suggested, and the reCaptcha window appears properly, but no matter what I enter in the captcha, it never gets verified and I always receive the 'message' field of beforeValidate (I've set it to "You've entered a wrong message" etc).
I'm not even sure how to debug it to see at which point it fails. Even if I just replace all the code in checkRecaptcha function with "return true" to try and skip the validation with the keys and just see if the rule itself is correct, it still remains the same, and I'm generally not getting any of the specific incorrect-captcha-sol messages that I read around.
Am I correct to assume that the only code I need inside my controller function (assuming I've already included the component and helper in the controller) is Configure::load('Recaptcha.key'); and no further manual validation checks?
(unfortunately I can't link you my whole project due to rights)
I had a similar issue. Try removing the 2 response and challenge field lines in the component and overwrite them with these:
$controller->$modelClass->set('recaptcha_response_field',
$controller->request->data['recaptcha_response_field']);
$controller->$modelClass->set('recaptcha_challenge_field',
$controller->request->data['recaptcha_challenge_field']);

Controller Redirect issue in CakePHP

I have a controller with name users_controller, within the login action I want to redirect to my affiliate_redirect_controller.php, now I the following code in the users controller to redirect
$this->redirect(array(
'controller'=>'affiliate_redirect',
'action'=>'logRedirect' ));
And then I get the following error which I can't seem to resolve
Error: The requested address '/affiliate_redirect/logRedirect' was not found on this server.
I honestly do not know what this could be, quite new to cakePHP and none of the solutions found work for me.
the contents of affiliate_redirect_controller.php looks like this
class AffiliateRedirectController extends AppController
{
var $name = 'AffiliateRedirect';
function logRedirect(){
}
}
I can see there is a mistake in your code its because of naming convention.
$this->redirect(array(
'controller'=>'affiliate_redirects',
'action'=>'logRedirect' ));
Please see the above changes when you are writing your controller name in lowercase like above it should be plural affiliate_redirects and should not be affiliate_redirect
Apart from this you can use directly redirect as like this also.
$this->redirect('affiliate_redirects/logRedirect');
Please try, it should work.
Do you have a table in your database that corresponds to affiliate redirect controller?
You might want to rethink your logic, and use CakePHP routes to set the URL to what you want. Having a controller named affiliate_redirect_controller doesn't follow CakePHP's naming conventions.
Since I don't know exactly what you're trying to do, I don't know if this will work for you, but maybe consider redirecting to a separate action in UsersController like /users/affiliate_redirect/
Or you can create an AffiliatesController and then redirect to /affiliates/redirect/
Also, if you don't have debug mode set to 2, you should do that. It may help reveal what the actual issue is.
What debug level do you have in app/config/core.php ? Most of the time, when you get the message
Error: The requested address '/controller/action' was not found on this server.
it means you have a debug level set to 0 and increasing it to 1 or 2 allows to get more details about the error.

Resources