Welcome Guest, Not a member yet? Register   Sign In
Calculate value with data from another model
#1

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;

Reply
#2

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

Reply
#3

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

https://en.wikipedia.org/wiki/Model%E2%8...controller

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.
Reply
#5

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/overv...pflow.html

Attached Files Thumbnail(s)
   
Reply
#6

(This post was last modified: 05-23-2021, 04:15 AM by InsiteFX. Edit Reason: Added note )

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.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#7

(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.
Reply
#8

(This post was last modified: 11-17-2021, 08:36 AM by skaleon. Edit Reason: Spelling mistake )

(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.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB