CodeIgniter Forums
Calculate value with data from another model - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: Model-View-Controller (https://forum.codeigniter.com/forumdisplay.php?fid=10)
+--- Thread: Calculate value with data from another model (/showthread.php?tid=75919)



Calculate value with data from another model - groovebird - 03-29-2020

Hi, this is code from my seminar model:

PHP Code:
// Seminar Model

public function find_by_id($id) {
 
$this->load->model('Seminar_date_model''seminar_date');
 
$query $this->db->where('seminar_id'$id)->get('seminar_dates');
 
$seminar_dates$query->custom_result_object('Seminar_date_model');
 
 
$query $this->db->where('id'$id)->get('seminars');
 
$result $query->custom_row_object(0'Seminar_model');
 
$result->seminar_dates $seminar_dates;
 return 
$result;
}

public function 
get_seminar_dates() {
    // if i call $this->seminar_dates->price() there are no seminar data
    return $this->seminar_dates;


In the seminar_dates model i want to calculate a price and need data from the seminar model. How can i solve this?
Getting the seminar_date model with an seminar is easy, because then i have access to the seminar property in the seminar_date model by doing this:

PHP Code:
// Seminar_date Model

public function find_by_id($id) {
    $query $this->db->where('id'$id)->get('seminar_dates');
    $result $query->custom_row_object(0'Seminar_date_model');
 
    $this->load->model('Seminar_model''seminar');
    $query $this->db->where('id'$result->seminar_id)->get('seminars');
    $seminar $query->custom_row_object('Seminar_model');
    $result->seminar $seminar;

    return $result;
}

public function 
price() {
    // seminar data available 
    return $this->seminar->price $this->coupon;




RE: Calculate value with data from another model - ZoeF - 03-29-2020

Calculations should happen in your controller.

PHP Code:
public function myCalculation()
{
  $this->load->model('seminar');
  
$this->load->model('seminar_date');
  
$dataSeminar $this->seminar->find_by_id($id);
  
$dataSeminarDate $this->seminar_date->find_by_id($id);

  
// Do your calculations




RE: Calculate value with data from another model - groovebird - 03-29-2020

I think controllers are only for retrieving the data from the model. What if i need the calculation in many other controllers?


RE: Calculate value with data from another model - ZoeF - 03-29-2020

https://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller

The controller is meand to handle the data you get from the model and the view. Recieve input (view), get data (model), handle data (controller). If you require the handling of this calculation in many controllers you could use a library of sorts. Input your data get the other data out of it.


RE: Calculate value with data from another model - wdeda - 05-05-2021

In the CI3 version manual, there is a flowchart explaining how data flows in the framework. It is more than evident, even by its name, that the 'Controller' is the heart of the system.

https://codeigniter.com/userguide3/overview/appflow.html


RE: Calculate value with data from another model - InsiteFX - 05-06-2021

All your business logic should be done in the Model. The Controller acts as a switch board get data
from Model then send data to View.

I would create a calculateDates() Method in your Model then send the data back to the Controller.

Updated: 05-23-2021

Just a note here you can also use libraries for business logic if you do not want to use the model.


RE: Calculate value with data from another model - marciano.dili - 05-22-2021

(05-06-2021, 02:57 AM)InsiteFX Wrote: All your business logic should be done in the Model. The Controller acts as a switch board get data
from Model then send data to View.

I would create a calculateDates() Method in your Model then send the data back to the Controller.

Thank you for reminding.


RE: Calculate value with data from another model - skaleon - 11-16-2021

(03-29-2020, 05:08 AM)groovebird Wrote: Hi, this is code from my seminar model:

PHP Code:
// Seminar Model

public function find_by_id($id) {
 
$this->load->model('Seminar_date_model''seminar_date');
 
$query $this->db->where('seminar_id'$id)->get('seminar_dates');
 
$seminar_dates$query->custom_result_object('Seminar_date_model');
 
 
$query $this->db->where('id'$id)->get('seminars');
 
$result $query->custom_row_object(0'Seminar_model');
 
$result->seminar_dates $seminar_dates;
 return 
$result;
}

public function 
get_seminar_dates() {
    // if i call $this->seminar_dates->price() there are no seminar data
    return $this->seminar_dates;


In the seminar_dates model i want to calculate a price and need data from the seminar model. How can i solve this?
Getting the seminar_date model with an seminar is easy, because then i have access to the seminar property in the seminar_date cinema hd model by doing this:

PHP Code:
// Seminar_date Model

public function find_by_id($id) {
    $query $this->db->where('id'$id)->get('seminar_dates');
    $result $query->custom_row_object(0'Seminar_date_model');
 
    $this->load->model('Seminar_model''seminar');
    $query $this->db->where('id'$result->seminar_id)->get('seminars');
    $seminar $query->custom_row_object('Seminar_model');
    $result->seminar $seminar;

    return $result;
}

public function 
price() {
    // seminar data available 
    return $this->seminar->price $this->coupon;

Same question here. I  am also looking for the solution.