Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CakePHP - How to retrieve data from another model in controller

Tags:

php

cakephp

Currently i have 2 models. Users and Appbreak. They have associations of Users has many appbreak. Appbreaks belongs to users In my appbreak controller, there is a approve function which is called when a button from the view page is pressed.

In this function, i will need to update a value in the table of appbreak and a value in users table. I'm able to update value in appbreak by using the following:

$this->Appbreak->id = $id;
    $this->Appbreak->set('approve', 'Yes');
    $this->Appbreak->save();

However, i'm unable to pull the required data value from users table. <-- need to do so as i need to do some calculations before passing back the calculated value.

Am i able to get data from another model using the request method? Or do i need to use the find method? If i use the find method, am i able to update the the values and save it back into the database?

like image 824
user1192304 Avatar asked Dec 27 '22 06:12

user1192304


1 Answers

You can use any number of models in a controller . Declare the 'uses' array in controller

<?php
class DemoController extends AppController{
    public $uses=array('Appbreak','User');
}
?>

To get data from AppBreak model use find method of
$this->AppBreak object
Similarly to get data for User use find method of
$this->User object

like image 143
Kiran Avatar answered Jan 11 '23 22:01

Kiran