CodeIgniter Forums
DataMapper ORM v1.8.0 - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Libraries & Helpers (https://forum.codeigniter.com/forumdisplay.php?fid=22)
+--- Thread: DataMapper ORM v1.8.0 (/showthread.php?tid=37531)



DataMapper ORM v1.8.0 - El Forum - 07-05-2012

[eluser]WanWizard[/eluser]
That is not necessarily true.

I did some tests here, and the explain for both scenario's where exactly the same for every query I tested.

It can't be done because the join condition is used to create the relation between related objects. There is no functionality to filter related records to a subset other then where_related().

Please remember that Datamapper is an ORM, and not a query builder that allows you to construct any SQL you want. Use CI's query builder for that. I don't know any ORM that allows you to define custom join conditions on a relation.


DataMapper ORM v1.8.0 - El Forum - 07-05-2012

[eluser]jez123[/eluser]
It's well known in SQL/Databases amongst anyone that knows how to optimise them, really you have to have a high number of rows to see how much difference it can make

The fact that it can't be done because of the ORM paradigm is fair enough.

I just added a function that does

$db->join('b','a.id = b.a_id AND b.b_id = 23','LEFT OUTER');

to the underlying ci active record as a workaround


DataMapper ORM v1.8.0 - El Forum - 08-08-2012

[eluser]JamieBarton[/eluser]
if I have a list of events made by me and my friends, how do i also include the number of attendees of each event within the foreach loop? Not sure where to begin.

Normally, with something like Rails you could easily just have user.friends.events.count

Regards,
Jamie


DataMapper ORM v1.8.0 - El Forum - 08-08-2012

[eluser]WanWizard[/eluser]
That would be using a subquery. There's an example in the docs that uses a related count.


DataMapper ORM v1.8.0 - El Forum - 08-14-2012

[eluser]Unknown[/eluser]
lass Games{
var $has_many = array('gameSetting','team','log');
}

class GameSetting {}

class Team{
var $has_many = array('user','log');
}
class User {
var $has_many = array('log');
}

class Log {
}
According to the official document, With out joining tables I must create the suffix _id act as the foreign key. So in the logs table There are 3 foreign keys game_id,team_id,user_id.

How should I name the FK column and How Datamapper know which FK is reference to which table with the same suffix _id?



DataMapper ORM v1.8.0 - El Forum - 08-15-2012

[eluser]WanWizard[/eluser]
Datamapper uses the model name to construct the foreign key, if you haven't defined one. It doesn't use the table name.

In this example those will be games_id, gamesetting_id, team_id and user_id.


DataMapper ORM v1.8.0 - El Forum - 08-24-2012

[eluser]rei[/eluser]
Hi, How can i use datamapper orm and preserve the MVC logic in codeigniter. for example:

I want to get all the users, then get the posts related to each user, then get the comments for each posts of the user.

then pass the final result to the view.

Can someone please guide me how will I do that using MVC?


DataMapper ORM v1.8.0 - El Forum - 08-25-2012

[eluser]WanWizard[/eluser]
Datamapper models are the M of MVC, so there's nothing to preserve.

There are two groups of Datamapper users. Some stick to the provided Datamapper API, and run the queries from the controller, some use the strict model approach, and create a model method called (for example) get_all_user_posts() which will run all these queries, and return the final result to the controller.

I personally take the second route, unless it's a simple single query, then the overhead of a model method is a bit pointless.


DataMapper ORM v1.8.0 - El Forum - 08-25-2012

[eluser]rei[/eluser]
I like the second one that follows strict model approach.

So if I wanted to get all the users, then get all the posts related to each user, then get the comments related to each post. I will do something like:

Code:
$u = new User();

$u->include_related('post', 'post');

$u->include_related('post/comment', 'comment');

$u->get();

so I can return the $u to the controller. Am I correct sir?

And by the way..

I tried this one:

Code:
$users = new User();
      
        $users->include_related('post', 'name');

        $users->get();

       // print_r($users);

        foreach ($users as $user)
        {

            echo $user->name;

            echo $user->post_name;
            echo '<br>';
            echo '<br>';

        }

It works perfect, but the name of the user keeps repeating. How can I only show the name of the user once? and then show all the posts that belongs to that user. Thanks in advance sir Smile

And last question.

is it bad practice to make datamapper calls in the view like this one:
Code:
&lt;?php foreach($student_list as $student): ?&gt;
&lt;?php $student->course->get()->order_by('name'); ?&gt;
&lt;?php endforeach; ?&gt;






DataMapper ORM v1.8.0 - El Forum - 08-25-2012

[eluser]WanWizard[/eluser]
Something like this:
Code:
class User extends Datamapper
{

    // ... your config here

    public method get_all_posts()
    {
        // run the query
        $this->include_related('post', 'post')->include_related('post/comment', 'comment')->get();

        // always return the current object so you can chain
        return $this;
    }

}

Then in your controller you can use

Code:
$u = new User();

$u->get_all_posts();

include_related() runs a JOIN, so in an 1:n relation the parent will be repeated, that is the nature of the join. Datamapper doesn't distribute the result back to related objects (that's funcctionality on the roadmap for 2.0).