Cake PHP 2.x - view variables not defined as expected - php

So we are running into a problem that is recurring several times, caught through logs. We are getting loads of undefined variable notices. When we trace where they are supposedly undefined, they are always defined in the matching controller action to the view where it is complaining. We can't figure out how someone is getting to the view either without hitting the controller action, or with hitting it but losing variable definitions somehow.
We tried logging the controller and action whenever a view does this (by checking for undefined vars at thee top of the view), and it always says the correct controller action, so we don't have any unexpected controller actions where the variables aren't defined. It's problematic partially because it's filling up our logs, but we also worry actual users may be getting unexpected results. Does anyone have any idea of a scenario that could cause this?
It has happened with several views that do work fine when tested on our end (we've had several people hit the appropriate urls and the problem doesn't happen). We also checked with a code search, and no other controllers render these views. We have basic safety checks going on for when certain variables are expected in the url; for example, if an id is missing, it will throw a not found exception rather than trying to continue, so we know that's not the cause either.

Related

Fatal error: Call to undefined method Mage_Adminhtml_Model_System_Config_Source_Yesno::setAttribute() in Abstract.php on line 389

I've looked everywhere and can't find how to fix this error. It doesn't show up in any error logs I'm aware of. I can only see it if I look at the page source. It completely stops the page from rendering half way through. How can I fix this?
I also ran into this error so I'm going to assume Freejoy was trying to do the same as me, which was using custom attributes on the customer entity. The Mage_Adminhtml_Model_System_Config_Source_Yesno source model will not work with the customer entity, or any EAV entity for that matter.
Changing to Mage_Eav_Model_Entity_Attribute_Source_Boolean as a source model fixed it for me.
I hope this can help others too.
You've truncated your error message, this
in Abstract.php on
should be pointing to a full file path. Without knowing that full file path, it will be hard to diagnose the problem.
My assumption is Abstract.php is part of a custom module, and that custom module has instantiated a Mage_Adminhtml_Model_System_Config_Source_Yesno (maybe with code that looks like this)
$model = Mage::getModel('adminhtml/system_config_source_yesno');
$model->setAttribute();
and is then trying to call the setAttribute method -- which doesn't exist on this model.
Another possibility is you have a custom modules (or hacked core file) where you've used the model alias adminhtml/system_config_source_yesno in a place where Magento expects a different alias type.
Again, very hard to tell without knowing the exact file the error happens in.

Cannot load custom controller in Opencart 2

I have created a custom controller in "admin/controller/mycustomcontroller/mycustomcontroller.php" to handle some AJAX requests among other things for my back-end modules. This custom controller contains an add() method that I intend to use.
Situation:
I gave read/write permissions to the Administrator user group.
Calling the add method of my custom controller through AJAX works fine.
Same as accessing it directly through url route=mycustomcontroller/mycustomcontroller.php/add
Problem:
I cannot load the controller using:
$this->load->controller('mycustomcontroller/mycustomcontroller/add');
I tried to load it in the admin/controller/catalog/product.php file and inside my model file but it returns nothing (no result, no error, nothing).
I am new to Opencart and I don't know what's the problem, in the worst case scenario I will just cURL the controller file but that doesn't feel right.
After many tests I finally found the problem, and it's quite silly.
Let's say you want your controller to return an encoded json string so that you can use it in your javascript, you could write (by habit):
$this->response->setOutput(json_encode($result));
Big mistake! Setting the output through this function won't be taken into account when the load->controller() method is called, so even though your AJAX/JS will still work fine, nothing else will. instead use:
return json_encode($result)
The fact that no one had stumbled upon this issue before astonishes me, I am either stupid or missing something important in OC documentation.

Non-existant model class properties, no errors, and ZendFramework

We are using SocialEngine, produced using ZendFramework. This is not directly a question about SocialEngine, though it relates. This is not specifically a question about ZendFramework either; I imagine the lessons here are broadly applicable to most PhP MVC frameworks.
We have a problem whereby apparently under conditions of high load, some parts of the website stop showing up. For example, an entire widget's content will just fail to load without any apparent errors. The lack of errors to diagnose the problem is driving me crazy.
This is not at all a question about load or optimization.
I can reproduce the similar problem by calling a nonexistant class property from the ZendFramework view, model or controller, e.g.:
<?php echo $this->article->fubar; ?>
At that point the widget it is part of will fail to display without any apparent error. This is problematic when the widget itself is the main display part of the page.
I suspect these problems are ultimately caused by a class variable which has not been initialised but the codebase is too large to identify where without better error reporting. It could also be the result of a chained object failure, e.g. something like:
<?php $this->article->getCategory()->getTitle(); ?>
... could theoretically cause this behaviour if the getCategory() method returned null.
Furthermore, I believe the dearth of error reporting to be caused by 'magic' properties within the ZendFramework's data model, which allows for dynamic class properties such that if the data model has some field "blablabla", $this->article->blablabla is populated with the contents of that field. However, if the property to be accessed is not a database field this magical behaviour does not help very much, especially if it disables normal error reporting. This also explains why $this->article->fubar(); caused the widget to fail to load in the same manner.
How can I get accurate errors out of ZendFramework when faced with this behaviour?
Have you tried to catch the error by putting below code in controller's action or widget controller file?
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
It can be possible that some of your article do not associated with any category or associated category has been deleted from category table, so $this->article()->getCategory() is coming null. It should not happen if category is required field for the article and category is exist in the category table. You can avoid the error by putting the !empty()/instanceof check for such code.

How can I find out what controller method was called in a Laravel4 NotFoundHttpException?

I am new to Laravel4 and I find it very annoying. One of my frustrations is that when I get a NotFoundHTTPException it does not tell me which Action was trying to be called! Because of this, I don't know if I have a problem with my routes.php file, whether for form's Form::open(array('url'=>'postUpload') location is bad or anything. With my previous experience with Zend the error would tell you exactly which action was trying to be called so I could troubleshoot exactly where the problem is. I feel with Laravel, I have to make changes to so many files (routes.php, blade.php, config.php, or Controller files) just to get to the bottom of which file is the culprit.
So my question is: Is there a way I can extend, or show the action that the application attempted to call on the NotFoundHttpException screen? I'm not posting any code here because it won't help the situation. I need to be able to tell which action the application is trying to call.
Thanks!!
Yeah, this is not cool at all, I agree and also have some troubles with it, when it only shows at the log and has no other comments, no Whoops message, for example.
But... Not Found HTTP = route not found. The problem is in your routes, so take a look at the QUERY_STRING in the Whoops message, it should tell your wich one is broken.
When a controller action is not found, you'll receive a 'Controller method not found'.

check for error

I have my own AppCotroller and using the beforeRender method to make changes to $this->viewPath based on the desired output format.
Is there a way I can check if Cake is currently outputting an error message? If I change the viewPath and then it's displaying an error (like can't load model, etc) it will error on the error :)
By the time Cake is displaying the error it should already be too late to do something about it. Not quite sure why you'd get an error about missing models when you change the viewPath, I hope that was just an example.
You might have some luck overriding or extending the ErrorHandler to intercept errors, but I wouldn't recommend doing that. Errors don't exist to be hidden, they're there to tell you something.
Creating a custom View might be a good idea depending on what you want to do (see the MediaView as an example of an alternative view).
The best thing to do though should be to avoid triggering errors by only allowing certain, predefined views to be set or making sure that a certain view file exists before trying to invoke it.

Categories