Marc
Marc

Reputation: 493

Cakephp send data to model function

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

Answers (2)

Anil kumar
Anil kumar

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

Martin Bean
Martin Bean

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

Related Questions