make codeigniter automatically insert/update created field and modified field like cake |
[eluser]runrun[/eluser]
Cakephp has very useful feature that, if a table has created and modified field, when you insert something, cakePhp will automatically insert the datetime to those field. And it will update the modified field whenever you update that row. So I wonder how can I archive the same thing with codeigniter ?
[eluser]louisl[/eluser]
Assuming you're using mySQL, Set the modified date field to type = timestamp, attribute = on update CURRENT_TIMESTAMP, default = CURRENT_TIMESTAMP
[eluser]CroNiX[/eluser]
Just let the database take care of it as louisl explained. No need for this to be handled by a framework as it would require every table in the database to have a date field.
[eluser]PhilTem[/eluser]
Either put it to your MySQL-server (make a field for update and create timestamp and set the options accordingly) or create a custom MY_Model which takes care of this. If you're interested in the second part, you might wanna take a look at my model I currently develop. It's pretty massive, but for your part only method 'create()' and 'update()' will probably be important https://svn.ignitedco.de/icf/trunk/appli..._Model.php
[eluser]runrun[/eluser]
OK I think I go with both way. Because the mysql solution only insert and update the modified field. I need to insert the datetime to created field as well. So I make a model to check for created field (if exist), then insert the date. And I want to run this model automatically. But the problem is, technically, the model doesn't know what table to perform with.
[eluser]vbsaltydog[/eluser]
[quote author="runrun" date="1327978678"] So I make a model to check for created field (if exist), then insert the date. And I want to run this model automatically. But the problem is, technically, the model doesn't know what table to perform with. [/quote] Pass the table name to the model constructor at load time (or define it at the start of the model class as a class variable) , then have the model extend MY_Model, then put your update logic in the MY_Model constructor. Code: class MY_Model extends CI_Model |
Welcome Guest, Not a member yet? Register Sign In |