ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

DataMapper 1.6.0

September 05, 2008 12:32pm

Subscribe [115]
  • #751 / Apr 23, 2009 1:42pm

    qureshi

    14 posts

    Hi!

    I’m using DataMapper, and it’s a great tool, but I need some help with it. I’ve created a thread here:
    http://ellislab.com/forums/viewthread/112411/ Perhaps someone familiar with DataMapper could help me. That would be very much appreciated. Thanks.

    Nadeem Qureshi

  • #752 / Apr 23, 2009 3:43pm

    bEz

    110 posts

    @Qureshi
    First off, Welcome to CI!

    I would suggest that if you truly want to use DataMapper Library, that you check the USER GUIDE for it.
    Then, read-up on the adaption to Datamapper (DMZ edition)

    It will unlock the answers on how to take advantage of the Relationships you have from Category to Verses (etc.).
    What I’m getting at is that it will depend on whether you want to build the tables to match DataMapper’s guidelines or need customization.  😉

  • #753 / Apr 23, 2009 4:16pm

    OverZealous

    1030 posts

    SELECT node.name, (COUNT(parent.name) - 1) AS depth
    FROM nested_category AS node,
    nested_category AS parent
    WHERE ...

    I found the count function in Datamapper documentation, but how can I define the “AS” ?

    I’m not 100% sure what you are trying to do with your query, here, since you clipped it at the WHERE, so I can’t help too much.  That being said, you can usually solve a complex query like this in two ways.

    1) If possible, just use the normal DM method for selecting.  the count() method just counts the number of results in a query, so it may or may not work the way you expect.  There’s no reason to have an AS, because it just returns a number.

    Instead, you might try using the select method, which allows you to write in anything into the SELECT statement.  However, the original DM doesn’t allow you to return any fields that are not directly on the table (DMZ does) in a get(), so the alternate is to use…

    2) The DataMapper query() method.  This allows you to pass any query into DataMapper, and return the result.  Make sure you read the docs, and make sure you include a unique id for each row.

    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.

  • #754 / Apr 23, 2009 4:24pm

    OverZealous

    1030 posts

    @naren_nag

    I’ve tested my application with hundreds of rows, and never had that problem.

    Please make sure that you don’t have something wrong in your saving code.  Is it possible that a save is *failing*, but you aren’t checking the result?  For example:

    $obj1 = new Object1();
    $obj1->value = 'value';
    if( ! $obj1->save()) {
        // show error
    }
    
    $obj2 = new Object2();
    $obj2->get_by_id(1);
    
    $obj1->save($obj2); // <<< Don't assume this will not error!

    I’ve made that kind of mistake before, assuming a save was succeeding when it wasn’t.

  • #755 / Apr 23, 2009 5:07pm

    warrennz

    56 posts

    @naren_nag

    Debug the queries being ran. It may be updating instead of inserting.

    And yes, never assume it will save every time.

  • #756 / Apr 23, 2009 5:19pm

    naren_nag

    81 posts

    @phil @warrennz

    So I checked if the objects were saving and individually they are.

    so here’s what i’m doing.

    $person->get_by_id($id);  // This works fine. checked.

    $work->designation = $designation etc. // All values reaching controller fine, and being assigned fine.

    $work->save($person); // A new record is being created in the workexperiences table, but no corresponding record is being created in the people_workexperiences table. This was working ABSOLUTELY fine till the 199th record was created in the join table.

  • #757 / Apr 23, 2009 5:24pm

    OverZealous

    1030 posts

    Is it possible you have a $has_many relationship set up as a $has_one?  That would cause UPDATEs instead of INSERTs.

  • #758 / Apr 23, 2009 5:24pm

    naren_nag

    81 posts

    Double checked all relationships ... this one is a doozy :!

  • #759 / Apr 23, 2009 5:29pm

    OverZealous

    1030 posts

    Is it possible to reset your data, or set up a temporary database, and try inserting 200 items (via code, of course).  Just to see if it is repeatable?

    Do you have any related validaters?

    Can you manually insert another row into the table?

    As warrennz mentioned, turn on CodeIgniter’s profiling, and look at the exact queries being run.

  • #760 / Apr 23, 2009 5:29pm

    qureshi

    14 posts

    @Qureshi
    First off, Welcome to CI!

    I would suggest that if you truly want to use DataMapper Library, that you check the USER GUIDE for it.
    Then, read-up on the adaption to Datamapper (DMZ edition)

    It will unlock the answers on how to take advantage of the Relationships you have from Category to Verses (etc.).
    What I’m getting at is that it will depend on whether you want to build the tables to match DataMapper’s guidelines or need customization.  😉

    Hi,

    I have read the documentation but it doesn’t cover such complex relations, and I don’t even understand what DMZ documentation says, or what DMZ even does that DM can’t. It would be best if you could post your replies to me in the thread that I have created and linked to in my previous post, since I have notifications turned on and I don’t wish to receive notifications merely when this thread is updated.

    Basically, I’m hoping that I can do something like this to get all my data:
    $data = new Category();
    $data->get_where(‘id’, $id)->verse->translation->translator;

    But that doesn’t work. If it did, it would’ve given me (1) all the verses in a category,(2) the translations for each verse, (3) the translator name of each translation. An array like this:

    Verse1
    ——-Translation1
    ————————Translator

    ——-Translation2
    ————————Translator

    ——-Translation3
    ————————Translator

    ——-Translation4
    ————————Translator

    Verse2
    ——-Translation1
    ————————Translator

    ——-Translation2
    ————————Translator

    ——-Translation3
    ————————Translator

    ——-Translation4
    ————————Translator


    Verse3
    ——-Translation1
    ————————Translator

    ——-Translation2
    ————————Translator

    ——-Translation3
    ————————Translator

    ——-Translation4
    ————————Translator

    Thread: http://ellislab.com/forums/viewthread/112411

    Thanks in advance,

    Nadeem Qureshi

  • #761 / Apr 23, 2009 5:36pm

    OverZealous

    1030 posts

    @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:

    $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.

  • #762 / Apr 23, 2009 6:01pm

    qureshi

    14 posts

    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

  • #763 / Apr 23, 2009 7:42pm

    Peet86

    14 posts

    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]

    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.

    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 :(

  • #764 / Apr 23, 2009 7:54pm

    warrennz

    56 posts

    [EDIT] Wooo. +1 posts.

  • #765 / Apr 23, 2009 8:52pm

    nmac

    8 posts

    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?

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases