Run a function before a model is saved to the database in DataMapper ORM and CodeIgniter - codeigniter

Is there a way to run a function/callback before a model is saved using Datamapper ORM in CodeIgniter?
I know this is possible in most ORMs where you can hook into certain points such as before_update, before_create and so on. Basically I want to hook into before_update to save a copy of the table row in another logging table before it is modified. I am doing this in the controller currently but want to move it into the model as a better MVC practice.
Please provide code or link to documentation. Thanks in advance.

Looks like Datamapper has a decent documentation on cloning/copying: http://datamapper.wanwizard.eu/pages/clonecopy.html
I would probably set up a separate Log_model for those tasks. You can load one model from another by instantiating CI ($this->CI =& get_instance();) and then loading the Log_model from your data model.

Related

Laravel Modeling System

I have been using Laravel for a couple weeks now and I love the framework. However, with models is there any actual robust system? Creating models, like the user model, just seems to be string arrays of what you do and do not want modified in queries. The place I see validation of the models is in the controller (through $this->validate()) and/or migrations creating the tables.
I just wanted to know if there is a certain place I am missing or not implementing. My brain is referencing something like ASP.NET's decorators that add validation to data models. Thank you!

Models responsability

I have a doub about Laravel. The models are used to define the relationships between the models like hasMany, belongsTo, etc. Also the models are used to define the fillable fields. But he models are only for that? Because I already check some examples that it seems that some queries are executed in the models instead of the controller so Im not understanding if the models should also have the querying of the relationships or not. Can you give a help to understand better what is the correct use of models (what should be placed in the models)?
Its same way to execute queries on model or controller. Written queries in model make your controller more clean. We can write mutator, accessor or query scope in eloquent model. Correct me if I'm wrong.
Visit https://laravel.com/api/5.5/Illuminate/Database/Eloquent/Model.html
You can refer this documentation, it's quite helpful if you want to know deep basics and responsibilities about particular part in Laravel.
Models are mostly used to make an outlook of the data i.e what fields are going to be saved in the database and we also use it to associate the relationships with other related data as you already know but we also use it to alter the values that are either going in/out of the data base which you can check in the documentation in link bellow https://laravel.com/docs/5.6/eloquent-mutators
the main purpose is to divide the code between controller and model (were it best fit to be)

Laravel: What are models?

I'm confused as to what models are and do in Laravel. I've tried to find some explanations but couldn't find any.
Can someone briefly explain what models are, when I would use them, and why I should use them?
More so, what are fillable and guarded attributes? I don't find they're very well explained in the docs.
For example, I have a table in my database, called login_log, that contains all login attempts. Would I create a model for this? Why?
Model is represented by M when you talk about MVC which stands for Model, View and Controller.
In Laravel Model is simply your database table object. This allows you to interact with your database tables as if they are PHP objects or classes.
Fillable property is used to tell laravel to allow mass assignment for the listed fields
while Guard property is the opposite of fillable
Laravel documentation is the best documentation so far.
See this Links to Understand well : Mass Assignment in Eloquent ORM for Laravel 4.2
Suggestions:
If you are newbie in Laravel, as I am Android Application Developer
i've find solution and understood too.
You have to learn documentation before putting question.
As MVC stands for Model View Controller, Model Deals with Database for example controller ask to Model to give me the first names of Students from the student table an d then controller pass it to the view.

CodeIgniter MVC Model Logic

I have programmed in Rails, Django, Zend, and CakePHP. Also, Wordpress and Drupal.
Now, I am "catching up to speed" in as fairly large application in CodeIgniter.
Typically, my experience with MVC frameworks, has led me to believe that Models represent business logic in reference to actual database tables. In the CodeIgniter docs and in the code base I'm dissecting I see models created that represent something as vague as a page. A lot of the business logic is written directly into those models and they incorporate other actual data models. I'm not sure this is ideal and following MVC.
Should models be created beyond data models?
Also, lets say I have a data model representing a user (user table in DB). In that user table there is a column called gender of the type enum('male', 'female'). Now I want to populate a dropdown with the gender options from the enum column.
Where is it most appropriate to put this logic?
The user model is an object used to represent a single user or row in the db... right? So it doesn't seem fitting to include a function in the user model/class called "get_gender_options", because, while the function is related to the user table, it is NOT relevant to a single user object. In Zend this kind of logic could be built into the form object itself.
There is not "right" answer, just one we can consider the most appropriate...
I would probably just put the "get_gender_options" in the model, rather than sticking it in the view for the form. To keep it DRY but not put it in the model, I would create a helper to hold this.

ASP.Net MVC How to separate view models from DB models?

I can't quite decide how to go about separating my view models from my DB models.
I'm using an ActiveRecord pattern for my DB access. Meaning I get a User class instance for each User row in the database.
In WebForms I'm used to use these as my model objects, implementing most of the business logic directly on my ActiveRecords.
I realize this isn't exactly 3-tiered design, and I'd really like to improve upon it, especially in MVC, where separation of concerns is empathized.
So I'd think the Controller shouldn't have access to my DB models, but how do I then go about storing/loading data from the DB ?
It's not my impression you should place a huge amount of business logic in your view models either, so somehow I think I'm missing a central piece of the puzzle.
What I'm looking for is some best-practice advice I guess :-)
I hope all this made sense, otherwise please ask.
I strongly suggest creating one view model per view and using AutoMapper to map properties from your active records to your view models. I don't believe that there's a problem with your controller having access to your DB models; the controller should be responsible for translating them into view models.
As for translating view models (really post data models) back into active records, you can use AutoMapper for this as well in simple cases and custom code for the rest.

Resources