How to access a Model that isn't part of a Controller in CakePHP - cakephp

I am currently working in a controller called Content. In this controller, I have a function that will be called when someone goes on to the homepage, and most of the data will be pulled from the Content model.
However, I want to display data from another model called Phones. When I type this code:
$phones = $this->Phone->find('all');
I get the following error:
Fatal error: Call to a member function find() on a non-object
When I change the Phone part of the PHP code to Content it works fine. So I'm guessing that I can't, at the moment, access the Phone model from inside the Content controller.
Is there a way to achieve a way of accessing a model externally from a controller?

$this->loadModel('Phone'); # Important: singular!!!
$this->set('phones', $this->Phone->find('all'));

try with below
$this->loadModel('Phone');
$this->set('phones', $this->Phone->find('all'));
cheers

Related

Codeigniter Editing returned array inside of controller (not view)

CODEIGNITER ISSUE: I can't seem to wrap my head around the array passed back from a model into a controller before sending it to the view.
My controller:
function getLatestComp()
{
$data['latest'] = $this->competitions_model->getLastCompetition();
echo json_encode($data['latest']);
$this->load->view('single_competition_view', $data);
}
How do I manipulate the returned array? Here is the returned array:
[{"comp_id":"25","song_id1":"178512356","song_id2":"119321744","start_date":"2015-03-23","end_date":"0000-00-00"},{"comp_id":"26","song_id1":"179391155","song_id2":"57429423","start_date":"2015-03-23","end_date":"0000-00-00"},{"comp_id":"27","song_id1":"57429423","song_id2":"52255807","start_date":"2015-03-23","end_date":"0000-00-00"},{"comp_id":"28","song_id1":"57429423","song_id2":"52255807","start_date":"2015-03-23","end_date":"0000-00-00"}]
say I just want the second instance's comp_id?
I have tried:
$data['latest'][1]->comp_id
$data['latest']->comp_id[1]
$latest[1]->comp_id
$latest[1]['comp_id']
How do I get the value I'm looking for?
It seems you want to access 2nd element of your object.
I think you returned data from your model like this get()->result()
if you want to get your 2nd element inside controller it should work
$data['latest'][1]->comp_id;//if your php version>=5.4
If your php version is lower then use this
$second_object=$data['latest'][1];
$second_object->comp_id;
similarly if you want get it inside your view use this way
$latest[1]->comp_id;//if php version>=5.4
//otherwise
$second_object=$latest[1];
$second_object->comp_id;
if you return data as array from your model like this ...get()->result_array() then you can accesses it easily
$data['latest'][1]['comp_id'];//inside your controller
$latest[1]['comp_id'];//inside your view
Hope this you understands it and solves your problem.

Creating a custom Store in a view initComponent yields to "Uncaught TypeError: Cannot read property 'apply' of undefined"

I have been using extjs for only 3 days now so I am very new and am trying to get used to some of the concepts.
I have a view called GroupListView which contains multiple GroupViews. Each group view needs to instantiate a ContactStore and invoke an ajax call with the groupId to get all the contacts in that group. So, I thought I can put a var contactStore = Ext.Create('MyApp.store.ContactStore'); in to the initComponent of the GroupView component (remember I don't want a singleton - each group has its own contact data).
It throws an "Uncaught TypeError: Cannot read property 'apply' of undefined" error when I do that. If you remove that line things are fine but obviously I have no contact data.
https://fiddle.sencha.com/#fiddle/5gv
What am I doing wrong? And once I get this working, what is the best practice to get the groupId from the GroupView in the example (so I can pass it in the ajax call to the restful service)? Is it to set a local property, like I do, called groupData in the GroupStore load loop I have? Is there a better way to automatically create a GroupView components for all the items in the GroupStore, like a repeater kind of thing, rather than my manual loop?
If you can kindly just give me a general "yes - you're on the right path" or "no you're doing things not the extjs way" I'd appreciate it. Yes - I realize I am missing controllers :)
Thanks

Fatal error: Call to undefined method View::redirect() cakephp

I am developing an application in CakePHP 1.3. Everything is working fine but one error is there.
When I use redirect() in my .ctp page it gives error
Fatal error: Call to undefined method DebugView::redirect()
As redirect is working fine when I use it in controller.
I have included helpers as follows in my AppController
var $helpers = array('Html', 'Form','Session');
Please help
thanks in advance
You never redirect (or output any header if possible) in the view layer.
Use the controller to do so.
The view then should only render the output according to the desired output format (html, xml, json, ...). Header stuff is part of the reponse and responsibility of the controller (and in 2.x the response class itself).
So your observation that it will work with controllers and not inside views is correct.
"Call to undefined method" always is a good indicator for a method not being available in this scope.
Redirects being part of the "logic" makes them only available in controllers and components. Never ever in the view (output after all logic happened).
In your controller:
$this->redirect( 'url, absolute or relative here' );
Done.

Experiencing strange behaviour with cakephp

I was setting up this application in cakephp.I have homes controller which works well and then i have chokates controller which has an index action.But whenever i run this chokates controller i get an error
Call to a member function charset() on a non-object
I had a print of $this in top of this controller which shows me that html and javascript helpers are loaded already.Then why i get this error i dont know.
Please help
Here is the link http://www.maninactionscript.com/chokate
go to the chocolates link.
Regards
Himanshu Sharma
Well, looking at your error message it appears that a couple things are happening here that should be fixed:
The view for ChokateController::index() was not found.
Ensure that you have an index.ctp file in /app/views/chokate/.
The HtmlHelper object likely isn't being added to the view because you have a manually configured list of $helpers. If you have assigned an array to $helpers in ChokateController ensure that Html is listed.

cakePHP: I need to include a helper for when there is an error

I have made a helper class called Navigation that gets used on every page because it does stuff to my main navigation menu. So in order for this to work I have included the helper in my pages controller like so:
var $helpers = array('Html', 'Javascript', 'Navigation');
however when there is an error like a missing view or something the helper can't be found and I get a reference to a non-object error that messes my page layout up. I'm guessing this is becuase an error page uses a different controller, however there isn't a file error_controller.php or anything in the controllers file. So my question is where do I need to declare the helper so it can be found by an error page. Would I need to make an error controller file or is there already a file that I can add it in to?
Any help would be much appreciated
Thanks
If it's used on every page, why not add it to the AppController?

Resources