Reputation: 493
I have the following function in my Website model
public function update_website_status($id = null,$status ){
$this->saveField('is_approved',$status,array('website_id' => $id));
}
Now if i wish to call this function from a controller
how would i set the $id
and the $status
?
I know this is rather basic but i simply couldn't find an example in the Cake Cookbook (Documentation).
Also is the way i am "searching" aka. making sure its the right website
_id the correct way of doing it?
Upvotes: 0
Views: 113
Reputation: 4177
just try like this
$this->Website->update_website_status($id, $status);
in your WebsitesController
if you want to call this function other than from websites controller just load the website model before calling the function, i.e.
$this->loadModel('Website');
$this->Website->update_website_status($id, $status);
Upvotes: 1
Reputation: 39389
If you have the website ID already in your controller, then you could just update the site directly, like this:
public function controller_action() {
$this->Website->id = $id; // $id has been set somewhere
$this->Website->saveField('is_approved', $status); // $status has bee set somewhere too
}
If you wanted a method in your model so you could call it in multiple controllers, then make sure you load the model:
<?php
class FooController extends AppController {
public $uses = array('Website');
}
And then create your model method as you have
<?php
class Website extends AppModel {
public function updateWebsiteStatus($id, $approved) {
$this->id = $id;
$this->saveField('is_approved', $approved);
}
}
And call it in your controller methods:
public function controllerAction() {
$this->Website->updateWebsiteStatus($id, $approved);
}
Upvotes: 1