How to get a value out of a function after a query insert with CodeIgniter |
[eluser]randomdriver[/eluser]
Below is my code. After the save() function puts the data into the database I need to use the $code variable in another place via email. This first snippet is in my models folder: Code: function save() Here is my controller: Code: function send() As you can see, as soon as its saved into the DB via the model, I need to use the generated code in the email where I have $CODE. Any help appreciated.
[eluser]affix[/eluser]
Well If you want it to return true or false on success you would do this in your model Code: function save() Then in the controller Code: function send() That should return TRUE if the save function succeeds, False if otherwise Hope I have helped
[eluser]randomdriver[/eluser]
Thanks for the reply, not sure how switching to return TRUE; achieves what I need. To be more clear, I need this code in the model: Code: $code = random_string('unique'); To go to this code in the controller, AFTER it is interested into the database: Code: $this->email->message('Hello,
[eluser]jedd[/eluser]
[quote author="randomdriver" date="1254257733"]To be more clear, ... [/quote] You could probably be even clearer, if you wanted, and no one would mind. Are you suggesting that the privacy of a given message will be solely limited to the obscure nature of the $CODE here? That's a bit .. messy. If you really want to go down that path, why not just generate a UUID for each message? Easy to store in the message table, plays nice in a URL, etc.
[eluser]BrianDHall[/eluser]
To achieve what you want it seems you'll need a class property somewhere, either in the model or controller. So the model loads the $code into itself, then in the email message you access it with $this->model->code. I presume you would want to save the value of code in the model anyway, or at least in the data to be saved. Though you wouldn't have to I suppose - $code would just be available for the duration of the script that way. Was that what you were wanting? Otherwise, I'm not getting what you are after either.
[eluser]randomdriver[/eluser]
Not sure what to say at this point. Rather than critiquing my method of creating a UUID I would just like some help on how to get a variable (called $code in this example) in my model function to carry over to my controller's function so they match up without having to do extra queries to MySQL. Seems very strait forward, I just don't know how to do it. I can then worry about how I want to generate UUIDs next, looking at other services like Evite, their IDs for public pages is pretty complex: http://www.evite.com/pages/invite/viewIn...KHQHPYHQCM
[eluser]randomdriver[/eluser]
[quote author="BrianDHall" date="1254262934"]To achieve what you want it seems you'll need a class property somewhere, either in the model or controller. So the model loads the $code into itself, then in the email message you access it with $this->model->code. I presume you would want to save the value of code in the model anyway, or at least in the data to be saved. Though you wouldn't have to I suppose - $code would just be available for the duration of the script that way. Was that what you were wanting? Otherwise, I'm not getting what you are after either.[/quote] Yes, just saw this after my last reply. This is what I am getting at. The steps are: - Generate a Unique Code - Insert it and other info into the db - Email that Unique Code to a person My goal was to avoid having to insert into the database, then query the code back out of the database, seems like extra work and CPU when the code was just created in the first place. I think I follow what you are saying and will try. Alternatively, are you able to pass code to the function? Like this? Code: $code = 123;
[eluser]BrianDHall[/eluser]
[quote author="randomdriver" date="1254263644"][quote author="BrianDHall" date="1254262934"]I think I follow what you are saying and will try. Alternatively, are you able to pass code to the function? Like this? Code: $code = 123; Sure, that would work just fine too. When I'm accessing model info its always in object form, (like $user = new User() ), so there it is just easier to use a class variable as without a corresponding row in the DB it won't get saved anyway. No method is particularly better than another, so pick whichever requires less code changes
[eluser]randomdriver[/eluser]
[quote author="BrianDHall" date="1254264367"][quote author="randomdriver" date="1254263644"][quote author="BrianDHall" date="1254262934"]I think I follow what you are saying and will try. Alternatively, are you able to pass code to the function? Like this? Code: $code = 123; Sure, that would work just fine too. When I'm accessing model info its always in object form, (like $user = new User() ), so there it is just easier to use a class variable as without a corresponding row in the DB it won't get saved anyway. No method is particularly better than another, so pick whichever requires less code changes [/quote] Got it! Thanks for your help! As someone trying to self-teach themselves without formal training it is very helpful and I appreciate it.
[eluser]alboyd[/eluser]
What a confusing set of responses to your initial question! I'm not even sure what the final answer was?! I think in the end - the suggestion was that you create your $code in the controller and pass this to the model function to insert into the database - then in the controller you can continue on using the $code for other purposes? The other option with what you initially had would be to check the result of the query ($insert i think you called it) - if OK return $code otherwise return FALSE. |
Welcome Guest, Not a member yet? Register Sign In |