CodeIgniter Forums
DataMapper 1.6.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 1.6.0 (/showthread.php?tid=11358)



DataMapper 1.6.0 - El Forum - 04-23-2009

[eluser]OverZealous[/eluser]
@qureshi
If you want people to reply to your thread, don't cross-post.

Second, you couldn't possibly have been reading the documentation for DM, because your sample code doesn't include a get() for each object. DataMapper doesn't magically look everything up unless you turn that feature on (and I definitely recommend you leave it off for performance reasons).

If you want to get what you mentioned, assuming your database is set up correctly, then your code would need to look like this:
Code:
$data->get_by_id($id);
$verses = $data->verse->get();
foreach($verses->all as $verse) {
    $translations = $verse->translation->get();
    foreach($translations->all as $trans) {
        $translator = $trans->translator->get();
    }
}

But that's an assumption based on the code you wrote.


DataMapper 1.6.0 - El Forum - 04-23-2009

[eluser]qureshi[/eluser]
Hi,

@bEz: Forgive me, I forgot to thank you for your welcome and help. Thanks a thousand! :-)

@OverZealous.com:

I shouldn't have cross-posted. That is bad forum manners. I apologize.

Thank you though for helping me out. Your code works! I did read the documentation, and the code I wrote was just to demonstrate what I meant: To get everything on one line of code. But your code is short and effective enough, and I'll definitely use it :-) Once again, thanks!

Nadeem Qureshi


DataMapper 1.6.0 - El Forum - 04-23-2009

[eluser]Peet86[/eluser]
Sorry! My Full query is:

Categories are in Nested data structure:
-root [lft:1 rgt: 8]
-sub1 [lft:2 rgt:5]
-sub2 [lft:3, rgt:4]
-sub3 [lft:6, rgt:7]

Code:
SELECT node.name, (count(parent.id) - 1) as depth
        FROM categories AS node,
             categories AS parent
        WHERE node.lft BETWEEN parent.lft AND parent.rgt
        GROUP BY node.id
        ORDER BY node.lft;

The query returns with the categories name and depth in the category tree.

Quote:In general, it is difficult to use DataMapper to query hierarchical relationships, though. You may try looking at using my updated version, DMZ, which supports complex relationships better.

Query will be good, but the depth (AS) column vanished..
So, I see I couldnt solve this probleme with the normal DataMapper, but DMZ seems to be good!
Thanks!

EDIT:

I can't solve it with DMZ Sad


DataMapper 1.6.0 - El Forum - 04-23-2009

[eluser]warrennz[/eluser]
[EDIT] Wooo. +1 posts.


DataMapper 1.6.0 - El Forum - 04-23-2009

[eluser]nmac[/eluser]
DMZ or DM - How to count related records and return in result?

Hi Folks.
Hopefully you can help with a ci/datamapper/sql issue I am having.
I'm using DMZ.
I have two tables/models, "articles" and "tags"
these have a related table articles_tags

In general, these are working with no problem. Great work Stensi / OverZealous just awesome.
However, I have the requirement to generate a result set that shows how many times a tag is used across articles. E.g. I need three columns in the result set...
id(of the tag) tagname usecount(how many times the tag is used).

Now, I have managed to do this by just iterating through the tag list and getting a count.
BUT, I know there must be a better way using GROUP BY and COUNT, and a sub query? But its a bit out of my depth. I think DMZ can do this, but am not sure how, despite searching for a similar issue here in the forums.

There are two reasons I want to be able to do it via a DMZ 'get' query to get a single resultset.. 1. Performance, and 2. I want to be able to sort by EITHER tagname or use-count.

Any help or suggestions would really be appreciated.
Cheers!
Neil.

DMZ or DM - How to count related records and return in result?


DataMapper 1.6.0 - El Forum - 04-23-2009

[eluser]OverZealous[/eluser]
@Peet86
Sorry, I just don't know how to help you here. Most likely the only way you are going to get DataMapper (or DMZ) to work with what you want is to change your structure, or rethink your design.


@nmac
You can perform normal ActiveRecord-like queries with DM and DMZ. DMZ lets you include fields that are not on the table (such as "COUNT(column) as column_total"), and you can use the normal group_by to group your query rows.

I'm not sure you will benefit much speed by trying to do this, however, unless your DataBase is on another server and not connected to the same LAN. Remember that the counting is done on the server either way.

I really think just calling $tag->article->count() will be pretty fast, easier to read, and save you the headache of maintaining a custom command if things get changed later.


DataMapper 1.6.0 - El Forum - 04-23-2009

[eluser]nmac[/eluser]
Phil,
thanks for the reply, really appreciated.
I think I have a standard query that will give me what I want, but I think I have lost my way in terms of using it with DMZ?
Here is what I have...
Code:
// test function
        function test()
        {
            // Trying to get all tags with a count of their use in articles.
            $t = new Tag();
            $sql = "SELECT tags.tag, count(articles_tags.id) as thecount FROM tags
            LEFT join articles_tags
            ON articles_tags.tag_id = tags.id
            GROUP BY tags.id
            ORDER BY thecount";
            $t->query($sql);
            
            foreach ($t->all as $tag)
                {
                    echo $tag->tag . '::'.$tag->thecount .            '<br />';
                    // hmm not quite?
                }
        }

The query itself seems to be right, in that when I run it in phpmyadmin I get what looks about right. But the code above is flawed? Do you have an example of using a custom count field in a result?
Cheers
N.
[EDIT] ok, some more fiddling, and the addition of the id for tags and I seem to be on to it.
Code:
$sql = "SELECT tags.id, tags.tag, count(articles_tags.id) as thecount FROM tags
            LEFT join articles_tags
            ON articles_tags.tag_id = tags.id
            GROUP BY tags.id
            ORDER BY thecount";

If there is an easier way using DM/DMZ notation, any ideas would be great!!
Thanks again!
N.


DataMapper 1.6.0 - El Forum - 04-23-2009

[eluser]warrennz[/eluser]
Forcing DM(Z) to save instead of deciding of save or update is required? I need to keep old relationships for historys sake. Eg user is on subscription x, then upgrades to subscription y, then later downgrades to subscription x again. Typically DM(Z) will UPDATE the original relationship with subscription x. Where as I want to keep 3 seperate records of those relationships.

I was thinking I could add a secondary method force_save(). This would set $this->force_save = true and then simply call $this->save() as per normal. In the _save_related() method just check for the flag and decide depending on that flag status?

Can anyone foresee any major problems with doing that?


DataMapper 1.6.0 - El Forum - 04-23-2009

[eluser]camporter1[/eluser]
If I have a DataMapper class named 'Profile', and I also have a controller named 'Profile', will this cause problems?


DataMapper 1.6.0 - El Forum - 04-23-2009

[eluser]camporter1[/eluser]
Actually, it does cause problems. How might I specifically ask for one or the other? Or maybe I should rename my controller to make things easier?