Welcome Guest, Not a member yet? Register   Sign In
Having Trouble understanding concept of passing data from model to controller
#1

I am inserting data that has been entered in a form view to a table.  Based on the value of one of the fields, I may need to insert the same data into a different table.  I can insert the data into both tables; but I am having a difficult time retrieving the value in the field and "sending" it to the controller.  This is where my application logic is that tests the data.

Therefore, my logic is:

insert data from view to table1 (this works).

From the controller, call a model method that retrieves the value of a field from the latest record inserted and pass the value back to the controller (this is where I am having the problem).

in the controller, test the value (with if statements).  If the value test TRUE, then insert the record into table2. (this works).

Can anyone give me a clue on how to write the code in the model to pass the field data from the selected record and how the controller can read that value so that I can test it?

Thank you in advance.
Reply
#2

(09-08-2015, 05:10 PM)amwh Wrote: I am inserting data that has been entered in a form view to a table.  Based on the value of one of the fields, I may need to insert the same data into a different table.  I can insert the data into both tables; but I am having a difficult time retrieving the value in the field and "sending" it to the controller.

When you say "based on the value of one of the fields," isn't that a field that was entered in the form view or calculated in the controller? You don't say what the application is, but suppose I have a form with name and street address, and maybe it's a "friend" or maybe it's a "contact." I know this before calling the model, so I don't need to pass data from the model to the controller.

However, you can easily return data from the model to the controller if you just place it in the return statement of a method/function. For example, in my user controller, in the login() method, I have:

PHP Code:
$valid_login $this->user_model->validate_login($data);
if (
$valid_login)
{
    
// do whatever

So validate_login() receives $data, but happens to return a true or false, and that is stored in $valid_login.

If I didn't understand your question, sorry, maybe give more details. Smile
Hey, don't work without a PHP debugger. Several free IDEs have this features built in. Two are NetBeans and CodeLobster. Without a debugger, it's like you're driving with a blindfold on -- you are going to crash!
Reply
#3

(09-08-2015, 05:48 PM)RobertSF Wrote:
(09-08-2015, 05:10 PM)amwh Wrote: I am inserting data that has been entered in a form view to a table.  Based on the value of one of the fields, I may need to insert the same data into a different table.  I can insert the data into both tables; but I am having a difficult time retrieving the value in the field and "sending" it to the controller.

When you say "based on the value of one of the fields," isn't that a field that was entered in the form view or calculated in the controller? You don't say what the application is, but suppose I have a form with name and street address, and maybe it's a "friend" or maybe it's a "contact." I know this before calling the model, so I don't need to pass data from the model to the controller.

However, you can easily return data from the model to the controller if you just place it in the return statement of a method/function. For example, in my user controller, in the login() method, I have:


PHP Code:
$valid_login $this->user_model->validate_login($data);
if (
$valid_login)
{
 
   // do whatever

So validate_login() receives $data, but happens to return a true or false, and that is stored in $valid_login.

If I didn't understand your question, sorry, maybe give more details. Smile

Thank you for your quick response. Sorry for not being more plain.  You answered my question generally.  

In the model, I am trying to "extract" the value of one field from the last inserted record in a table and pass the value to the controller.  Once the controller has the value, I will then test it to see if I should insert this record into another table.  

Specifically, one of the choices the user has to make on a form is how an order is shipped.  This information is collected from a drop-down box on the form.  We maintain a table of all orders so when the order is saved, it is inserted into our order table.  However, one of the selections of the drop-down box is an "in-house shipper"; so we need to analyze the drop-down selection and if the in-house selection is chosen, we need to save the record to another table, an in-house order table. 

I am asking specifically, how can I pull the value of the one field from the last inserted record from the order table, and pass that value to the controller.  In the controller I can perform an if statement, and if it tests TRUE (that the in-house shipper was selected), then I can initiate the model method that inserts a copy of the record to the second or in-house order table.

Thank you so much for any guidance that you can give
Reply
#4

There are a couple of ways that you can do this, but the logic of the way you are asking your question doesn't really mesh well with the way most people would approach the problem. To answer the question directly, you can setup whatever method you are using in your model to insert the record to return the data you're looking for:

PHP Code:
public function insert($data)
{
 
   if ($this->db->insert($this->tableName$data)) {
 
       return $data['desired_field']; 
 
       // or return $this->db->insert_id(); if you need the ID of the inserted record
 
   }
 
   return false;


Then the controller would check the return value when inserting the record:

PHP Code:
$result $this->some_model->insert($data);
// If $result is false, there was an error performing the insert
if ($result !== false) {
 
   // $result == $data['desired_field'], so do something with it
 
   if ($result === $inHouseShipperSelected) {
 
       $this->other_model->insert($otherData);
 
   }


But, I think the point most people immediately come to is that the controller can check whether it needs to do the second insert without the result from the model, because the user made the selection in the view. The only thing the insert can really tell you that you don't already know is:
  • whether the insert was performed successfully, and
  • the ID of the inserted row (assuming the ID is an auto_increment field)
I reinforced this in the insert() method above by returning the value from the $data variable which was passed to the model by the controller, rather than doing something like querying the database for the value.
Reply
#5

(This post was last modified: 09-09-2015, 10:29 AM by RobertSF.)

Ah, yes, that's what I meant, and that's what mwhitney said just before this -- you already know what you need to know, and the model doesn't tell you anything you don't know, except the two things mwhitney documented.


For what it's worth, if you'd like the database to tell you the number in the autoincrement field of the last insert, the function is LAST_INSERT_ID(). For example, if you add a user and want to know the ID to store in the session as "logged in," you can do this.
PHP Code:
public function add_user($data)
{
 
   $this->db->insert('users'$data);
 
   return $this->db->query('SELECT LAST_INSERT_ID() AS REC_ID;')->row()->REC_ID;


But that's not the situation you have here, which is instead
Quote:Specifically, one of the choices the user has to make on a form is how an order is shipped. This information is collected from a drop-down box on the form. We maintain a table of all orders so when the order is saved, it is inserted into our order table. However, one of the selections of the drop-down box is an "in-house shipper"; so we need to analyze the drop-down selection and if the in-house selection is chosen, we need to save the record to another table, an in-house order table.


I would do something like this:
PHP Code:
// after validating the form and creating the $data array to insert
$this->db->insert('orders'$data);
if (
$data['ship'] == 'inhouse')
{
 
   $this->db->insert('inhouse');


You could even get the user selection from the $_POST array. CI doesn't erase any of the variables, so whatever was in $_POST when the user pressed the submit button and whatever you store in $data should still be there no matter how many calls to a model you make. 
Hey, don't work without a PHP debugger. Several free IDEs have this features built in. Two are NetBeans and CodeLobster. Without a debugger, it's like you're driving with a blindfold on -- you are going to crash!
Reply
#6

(09-09-2015, 10:29 AM)RobertSF Wrote: For what it's worth, if you'd like the database to tell you the number in the autoincrement field of the last insert, the function is LAST_INSERT_ID(). For example, if you add a user and want to know the ID to store in the session as "logged in," you can do this.

PHP Code:
public function add_user($data)
{
 
   $this->db->insert('users'$data);
 
   return $this->db->query('SELECT LAST_INSERT_ID() AS REC_ID;')->row()->REC_ID

This is one of the things that will usually be different based on which database software you're using, so, with the exception of a couple of unsupported drivers, you're better off using $this->db->insert_id() to retrieve the value (and you don't have to go through the query()->row()->fieldname dance).
Reply
#7

Oh, yes, definitely! I hadn't noticed that helper function, but I'll start using it. It's more convenient and makes for more portable code. Thanks!
Hey, don't work without a PHP debugger. Several free IDEs have this features built in. Two are NetBeans and CodeLobster. Without a debugger, it's like you're driving with a blindfold on -- you are going to crash!
Reply




Theme © iAndrew 2016 - Forum software by © MyBB