Laravel 4 does not extend Eloquent class - php

I have a Product.php file in app/models folder. Here is the content of the file:
class Product extends Eloquent{
protected $table = 'productinfo';
}
However, when I tried to run $product = Product::all();, Laravel say Call to undefined method Product::all(). It seems like Eloquent class is not extended.
I don't see anything I do wrong here. Why does it not working? Could it be any other configuration in Laravel which I mess up?
--update--
After renaming the Product.php file, it is now no longer showing error message. But the problem is, it does not properly return the result from database. Instead, the returned result is {"incrementing":true,"timestamps":true,"exists":true}. Not the result from database.
So, I tried to run a clean new Laravel project from composer and the only files that I modified is the config/database.php file to set it work with MySQL database, route.php file, and the models/Products.php file. When I browse it on browser, again it shows the same result: {"incrementing":true,"timestamps":true,"exists":true}, not database result.
I really don't know what causing it. I have tried with it in one my experiment Laravel project folder which has lots of junk in it, and it works perfectly fine here.
Do you know why it doesn't work? How to solve it?
Thank you.

Related

CakePHP shell: Switch CAKE_ENV on the Go

I have a little problem in here. I'm new at cakephp and now have to developing a cakephp shell script to saving data into its database. The problem is, I work on default environment and need to save data into another environment. I'm using this code to switch environment:
ConnectionManager::alias($env, 'default');
It seems good since I've got right output when try to get database.
$this->out($datasource->config()['database']);
And then I load my model:
$model = $this->Model;
But It's load the model data from default environment. Is my approach wrong? Or, there is another method to switch environment on the go with cakephp?
That should work just fine, and a quick test shows that it does. You'd probably have to show a little more context, but I guess you are loading the model (what you're showing there isn't loading but accessing) before the connection alias is being created, hence the model will use the original connection that it received when it has been instantiated.
So either make sure that you load the model afterwards, respectively that you create the alias before the model is being loaded (that is when TableRegistry::get() is being invoked), or change the connection of the specific model on the fly in case applicable:
$connection = ConnectionManager::get($env);
$model->setConnection($connection); // use connection($connection) in CakePHP < 3.4

Phalcon pdo excpetion model first

I have problem with phalcon framework namely with models methods...
As you know models has included methods find() and findFirst()
I have generated model with phalcon-dev tools and now I am trying to do Model::find on it but I am getting an exception but dont know why...
There is some more informations (e.g stacktrace) :
http://exception.mateuszmarzecki.pl/
You can try change methods in model file
public static function find($parameters = array())
{
return self::find($parameters);
}
Does not look like your passing it the right parms.
SELECT FROM `nacionality`
Notice that your not selecting any fields from the database, and that is why your getting the Exception.
So... after some time of debugging I've found the problem...
For the next generation... if you don't want to lose a week as I did. Just read carefully your application config.
Problems occurs because I missed table and column annotations as well.
In my application config I have something like:
$metaData->setStrategy(new \Engine\Db\Model\Annotations\Metadata());
so Phalcon was looking for annotations in my model files, more info about this you can find there:
https://forum.phalconphp.com/discussion/1933/column-types-for-model-annotations
Happy New Year

Non-object error when inadvertently loading view from app/storage/views/?

I am working on a Laravel 4 application that is configured to use the file $_SESSION driver, which appears to be causing some problems for me. The problem arises randomly, and does not happen very often, and tends to happen when I try and click on an <a href="..."> quickly before the entire page is done loading (there is a large amount of information loaded on each page so page loads can be a bit slow). The error will also appear at times after waiting for the page to load completely.
Here is the error message I'm getting:
From what I understand, it looks like whenever my application decides it wants to load a (cached?) view from app/storage/views, it is not is not grabbing one or more of the objects that is passed to the view from my controller, and that is why I am getting a non-object error message.
I have tried experimenting by deleting the specific storage view in both the controller before the view is loaded and in the actual view itself (in app/views) with the following:
<?php
$badFile1 = storage_path() . '/views/d84ddef7152ff1956fc7aa87ddf29ba0';
if(file_exists($badFile1))
{
$delete = unlink($badFile1);
}
?>
But even this will still throw the error (although without showing the line #) from the file in app/storage/views, even though the file doesn't even exist:
Can anyone explain why this is happening and/or what I can do to fix this problem?
i thing you $catalog object is null. so not called getID() method.
Please check, $catalog object is assigned or not.
I've managed to fix the problem, as in, I have not been able to reproduce this random error message anymore.
It seems the error was caused by getting my models code tangled up, and not by $catalogs being null. I had two models:
Suggestions.php which does some data manipulation and Catalogs.php which ultimately fetches the $catalogs object which I needed for my view.
I was grabbing $catalogs like this in my controller:
$s = new Suggestions();
$catalogs = $s->getSuggestions($catalogID);
and then returning $catalogs in my view. The problem arises from the fact that I was also calling the Catalogs.php model (which contains the getID() function) from within the Suggestions.php model, and was not even instantiating a new instance of Catalogs to do so, just calling it straight away.
So most of the time Laravel would return $catalogs as an object of the Catalogs model like I wanted it to, but it would sometimes return it as an object of the Suggestions model, and when my view tried to call the getID() method, it was trying to call a method that did not exist in Suggestions model.
I fixed this by refactoring the Suggestions model into a private function as opposed to the model, that way $catalogs is always returning as the Catalogs object type.

Changing the behaviour of view in Codeigniter

I am using codeigniter for a project that is used by a variety of companies.
The default version of our software is up and running and works fine - however some of our customers want slightly different view files for their instance of the system.
Ideally what I would like to do is set a variable (for example VIEW_SUFFIX) and whenever a view file is loaded it would first check if there was a suffix version available if there was use that instead.
For example if the system had a standard view file called 'my_view.php' but one client had a VIEW_SUFFIX of 'client_1' - whenever I called $this->load->view('my_view') if the VIEW_SUFFIX was set it would first check if my_view_client_1 existed (and if it did use that) or if not use the default my_view.php.
I hope that my question is clear enough... If anyone has done this before or can think of a way to do it I would really appreciate it.
EDIT:
Ideally I would like a solution that works without me changing every place that I am calling the view files. Firstly because there are a few files that may want different client versions and also because the view files are called from a lot of controllers
I had a similar requirement for which I created a helper function. Among other things, this function can check for a suffix before loading the specified view file. This function can check for the suffix and check if the file exists before loading it.
Unfortunately, the file checking logic would be a bit brittle. As an alternative, you can implement a MY_Loader class that will override the basic CI_Loader class.
Something like this in your application/core/MY_Loader.php:
class MY_Loader extends CI_Loader {
protected function _ci_load($_ci_data)
{
// Copy paste code from CI with your modifications to check prefix.
}
}
Could you not do this
// some method of creating $client
// probably created at login
$_SESSION['client'] = 'client_1';
$client = (isset($_SESSION['client'])) ? $_SESSION['client'] : '';
$this->load->view("your_view{$client}", $data);

Can not find cakephp model

I am having extreme issues trying to get cakephp to find a model. i been working on this project for 4 months now and created ton's of models but for some reason, cake can not find this model. This is how it is set up at minimum
app/controllers/arbitrations_controller.php
<?php
class ArbitrationsController extends AppController {
var $name = 'Arbitrations';
}
app/models/arbitration.php
<?php
class Arbitration extends AppModel {
var $name = 'Arbitration';
}
database name: arbitrations
Error Message
Controller::paginate() - can't find model Arbitration in controller
ArbitrationsController [CORE\cake\libs\controller\controller.php, line
1102]
I've tried clearing tmp/cache/models and tmp/cache/persistent
I got it to work. I just deleted my arbitrations_controller.php file and recreated and it worked. Must be some type of glitch or something. I think it may have had something to do with even going about deleting the tmp files and recreating the files again.
Dont know what it was, but it wasted 3 hours of my life.
I just set this up using CakePHP v1.3.x and it worked out of the box using scaffolding. A couple of things you can try:
Double check the spelling on everything. Arbitration can be easily
misspelled.
Add the var $uses = array('Arbitration') to the top of the
controller and see if that changes anything.
Double check the model spellings in the controller
$this->Arbitration->recursive = -1;
If all of those things are set and working as expected it should work. Another thing to try is to load it in a clean app to see if it works in a stand alone environment.

Categories