How to get related fields in another model from current model, using datamapper in CI? - codeigniter

I am a newbie user to CodeIgniter and DataMapper. I have a User model and User Group model. Both of this model has an one-to-many relationships. Is there any way I could get user_group_name in User Group using the User model?

Start by reading the Datamapper user guide, on http://datamapper.exitecms.org.
You can include columns from a related model into the result by using include_related().

You should familiarize yourself with codeigniter first.
As for your question, I think it would be better for you to get help from the datamapper forums or the codeigniter forums.

Wanwizard and Thorpe,
Thanx for both of you. I have got the solution. I only call Group model from User model. The following code would explain it.
<?php
foreach($users->all as $user)
{
$user->group->get();
}
?>

Related

get laravel eloquent model relationships

is there any way to get the defined relationships in eloquent model. I have a situation where I need to get the model relationships so I can update all other eloquent models that relies on a specific id before delete it
There's no unified method to iterate over all registered relationships of a class. You can, however, access all the currently loaded relationships of a model instance (via the ->relations attribute or the getRelations() method), but that's not what you're up to. I'd suggest you take a look at laravel's documentation on inserting and updating relationships. So far that's the best laravel provides out of the box, the rest is developing approaches.
Try this function:
public function getRelations()
You can use
$model->getRelations()
function to get all relations
Also refer below link for details https://laravel.com/api/5.7/Illuminate/Database/Eloquent/Concerns/HasRelationships.html#method_getRelations

How can I retrieve this in laravel?

Flowing query is working fine on mysql database but how can I retrieve this in laravel? code of mysql is given bellow:
select count('brand_id') from products where brand_id=3
You could do
$number = 3;
DB::table('products')->where('brand_id',$number)->count();
But I recommend using Models
Product::where('brand_id',$number)->count();
Following what laravel has on https://laravel.com/docs/5.6/queries at the aggregates topic:
$products = DB::table('products')->where('brand_id', 3)->count();
Welcome to Stack Overflow!
As #PlayMa256 said, you can do exactly this query using
DB::table('products')->where('brand_id', 3)->count();
BUT, if you have defined your models correctly, you can do this directly using you model class. If your model is called Product, then:
Product::where('brand_id', 3)->count();
Another awesome way to do this is by using the magic methods for where clause:
Product::whereBrandId(3)->count();

Laravel 5.2 eloquent model order by their relation model attribute

I have a question. I have an user model and a post model. A user can have many posts. Now I need to get posts with pagination. And I need to sort posts by their related user's id desc. Per page number is 10. I do not know how to write this code, someone can help me? Thanks.
Since Post belongs to User the posts table has user_id key. Use it:
Post::orderBy('user_id', 'desc')->paginate(10);

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.

Run a function before a model is saved to the database in DataMapper ORM and 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.

Resources