I am getting the following error message turning up in my apache error logs:
PHP Fatal error: Call to undefined method MyWebsite\Model\ContentMedia::getImagePath() in /var/www/www.website.com/application/libraries/MyWebsite/Model/ContentVersion.php on line 544
On line 544 of ContentVersion.php the call is made:
$cm->getImagePath('img', 'small');
The ContentMedia class extends another class called Model that does have that method:
class ContentMedia extends \MyWebsite\Model{
... the Model class:
namespace MyWebsite;
class Model{
public function getImagePath($field, $size = null){
...
}
I get this error show up every now and then in the log files, so it may be one particular instance where a user comes across this, but I don't know where. This method is used throughout the site and it works. If the method is definitely defined how can this error be thrown?
I thought about checking the access logs to match an exact requested URL to the timestamp of the error, but the access logs are insanely large of course, so any advice as to how to output just a chunk of time from a large access log would be amazing.
I am using Doctrine 2 and Codeigniter 2.
My first thought is to check that you definitely have an instance of model present within the script. Have you tried creating another function for the sake of testing that will just echo something like "Object is instantiated and inherited functions properly".
That way we might be able to narrow down the problem a little
Related
I am using this script http://fpdf.org/en/script/script50.php and I am getting the error:
Fatal error: Call to undefined method PDF::FPDF() in MyPath/html_table.php on line 55
it is this call to FPDF that is throwing the exception :
$this->FPDF($orientation,$unit,$format);
I don't understand why, knowing that the pdf class extends FPDF and I have the fpdf.php file in the same directory as the html_table.php file, is there any way to fix this error? Thank you
Fixed it.
I actually needed to replace :
$this->FPDF($orientation,$unit,$format);
by:
$this->__construct($orientation,$unit,$format);
The original script has this error, so for anyone wanting to use the script don't forget to fix this error first. Good luck.
I can't find $this->FPDF anywhere in the script you shared. When you extend a class, the extended class is in the $this of the class you extended it with.
The constructor of the extended class will always be ran when you create a new instance of this class unless you define a constructor yourself, which you did in the PDF-class as the script you shared shows.
If you want to run the constructor of the class you extended, you should do this from within the contructor of the extending class using parent::__construct(); which tells PHP that it should at that moment run the contructor of the parent class (the extended class).
This is already the case in the script you shared:
//Call parent constructor
parent::__construct($orientation,$unit,$format);
So when you run new PDF() it will call the contructor of the PDF-class, which will call the constructor of FPDF.
When you call the constructor again as mentioned in your answer by using the $this->__construct($orientation,$unit,$format); line, this will result in the PDF-contructor to be called twice.
I have 2 controllers: UsersController and AnalyticsController.
When I run:
//UsersController:
function dummyFunction(){
$this->Analytic->_loadChartFromId($chart_id);
}
the output is:
Query: _loadChartFromId
Warning (512): SQL Error: 1064: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '_loadChartFromId' at line 1 [CORE\cake\libs\model\datasources\dbo_source.php, line 684]
The _loadChartFromId() function takes $chart_id as an argument and returns an array as output. I have no idea why Query: _loadChartFromId appears.
You don't call other controller methods from your controller.
In your users controller, $this->Analytic is an instance of the Analytic model, not the AnalyticsController. So CakePHP thinks you are trying to call a public method called _loadChartFromId() on the Analytic model, which, as you know, doesn't exist.
The reason you get the error is because if you try to call a non-existent method of a model, CakePHP tries to convert it to one of its Magic Find Types. Of course, it's not a valid Magic Find Type either, so you get a SQL error.
Solution
It's difficult to provide a complete solution as we only have part of your code, but you are perhaps violating the concept of MVC with the way you're coding your app.
You need to do one of two things:
Move _loadChartFromId() to your users controller. This seems to me like it would be counter-intuitive, as it probably has nothing to do with the User.
Move the method to your Analytic model. You would need to make it public so the controller can access it, and in your users controller you would need to make sure you have the Analytic model loaded.
class Analytic extends AppModel {
public function _loadChartFromId($chart_id) {
// ...
}
}
Then you can call the method as you were doing before, from your users controller.
I could have opted to close this question as an exact duplicate of at least 5 other questions (if you search for "cakephp another controller").
But the answers there are just terrible. They actually try to invoke new Dispatchers or requestAction().
So if your question is about another controller method:
The short answer is: You don't.
The long answer: You still dont. That's a typical beginners mistake.
You should put the functionality into a component if it is mainly business logic. The component then can be accessed from multiple controllers.
If it is more like model data (as in your example), put the functionality into the model layer (in an appropriate model). this way you can also access it from anywhere in your application.
Also: Accessing protected methods from other objects is never a good idea. Use public methods if you intend to use it from "outside" the object.
If your question is about a model method:
You need to include your model in your controller before you can use it.
Either by using public $uses or by using loadModel('ModelName') or even ClassRegistry::init('ModelName').
Deployed application to production and running into the following error on views that contain a call to a render function:
Fatal error: No matching function for overloaded 'render'
Example
/fuel/app/views/profile.php:
echo render('_validation');
Render is defined in /fuel/core/classes/view.php. The Autoloader should be making this available.
Ideas on environmental issues that may be causing this?
Edit: Both APPPATH and COREPATH hold the correct file paths
Try to use more clear function names, so you won't get in trouble with standart PHP functions.
It looks like your render-function is declared inside a class, if it is so you have to initialize an object of that class or use an existing object to call the method
echo $object->render('_validation');
Unsure if it is an issue specific with 5.3.3 but we just changed render() to View::forge() across the applications and all is well.
Does anyone have a clue about this? PHP 5.2.13. Results not wholly consistent i.e. could get a good result with a page at one time, then get an error at another.
The error is fatal - class does not have method.
But the following are true:
The class is defined in only one place and has the relevant method in the code.
At the point where failure occurs: reflection shows that the method exists.
At the point where failure occurs: method_exists says the method does not exist.
Previous calls (they're all static - not my choice) earlier in the code worked.
May be it's related: http://bugs.php.net/bug.php?id=51425
But I think here we have some cache-related problem. Do you have some cache enabled? Like APC or any other accelerators?
Be sure that the file containing the method is included. If the method is in a class, make sure the class instance is created and the method is called through the class.
Maybe you are missing the class instance?
I'm getting this error:
Fatal error: Cannot redeclare class Customer
Then I added the following code:
if (!class_exists('Customer')) {
include('include/customer.class.php');
}
Why do I still get that error?
I have a file (file1.php) which has the Customer() class declared.
In file1.php I make an ajax call to file2.php
In file2.php I declare the Customer() class again.
In file2.php there is only 1 declaration of Customer() class.
Check if your server runs opcode cacher like APC - that's the cause of an error. I've runned into it recently.
Clearly due to the fact I issue:
if (!class_exists('Customer')) {
The class doesn't exist so the class itself is somehow duplicating itself.
I use this class in numerous other pages in the application without a problem.
I simply removed the whole thing:
if (!class_exists('Customer')) {
include('include/customer.class.php');
}
And it somehow worked which is preplexing!
If the class existed, the class file should never be included...
It doesn't exist therefore, the class is being included.
Once included, it says it's already included...
Very, very odd...
Well, it's working now... I guess i'll leave it be...
Use include_once(). If that still gives you an error, the problem is that you are declaring the class more than once in the file "include/customer.class.php"
http://php.net/include_once
The errors could be caused by a class defined multiple times, for example:
class Foo() {}
class Foo() {} // Fatal error
If you are not sure how many times your class will be included you can two things:
Use include_once() or require_once() in order to be sure that that file is required "once" only.
Write that code you provided every time you are including that file:
if (!class_exists('Customer')) {
include('include/customer.class.php');
}
I'd prefer the first though.
Your problem is the one described above. There must be a place where the class is declared multiple times. Without any code is hard to tell where.
Here's some references:
include_once()
require_once()
PHP: The Basics