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]
  • #346 / Dec 08, 2008 11:25pm

    hugle

    289 posts

    OverZealous , thans for your thoughts.
    Well, i went a bit further now.

    So I started checking the libraries and the models directories. What I came up with,
    is that problem lays somethere in MY_Form_validation.php.

    [update]
    after looking into it, I’ve spoted some whitespace. thanks for your time, I really appreaciate it, OverZealous.

    Now i understend CI and DM even more.
    cheers,
    Jaroslav

  • #347 / Dec 08, 2008 11:31pm

    OverZealous

    1030 posts

    Good Stuff!  😊

    I can’t stand those time-consuming, little tiny bugs.  I think I spend twice as long fixing bugs that are bizarre and tiny than I do the severe ones!

  • #348 / Dec 08, 2008 11:36pm

    hugle

    289 posts

    Strange this bug didn’t came out earlier - bastard:) but thanks again, really!

    I must agree with you OverZealous, those little bugs consume too much time sometimes…
    sometimes you need to write a script which takes <10 mins, instead of this it eats you 2 hours or so 😊))

  • #349 / Dec 10, 2008 1:45am

    hugle

    289 posts

    Hello 😊
    It is me again, hope you did miss me 😉)))

    I’m playing with Relationships,
    my structure is as follows:

    forum has many topic
    topic has many post

    here is an example I’m working on

    $f = new Forum();
            $f->get();
    
            foreach ($f->all as $forum)
            {
                //part1
                $f2 = new Forum();
                $f2->get_by_id($forum->id); 
                $topic_count = $f2->topic->count(); // save topic count for later use
    
                //part2
                $t2 = new Topic();
                //...
                
                $data['forum'][] = array(id => $forum->id, title => $forum->title, topic_count => $topic_count, post_count => $post_count);
            }

    Part1 works well,
    but I’m stuck at part2. Can’t figure out how I can make relations between
    forum<>topic<>post and count it…

    Maybe some of you have some examples or thoughts on this approach?

    Thank you 😊

  • #350 / Dec 10, 2008 1:52am

    stensi

    109 posts

    Version 1.6.0 has been released!

    View the Change Log to see what’s changed.

    In short, there are now advanced queries. Let’s go through an example to see the benefits of these.

    Let’s say we have a User model and a Group model. A group can have many users but a user can only have one group. Here’s how you would look up all users belonging to the Moderator group without the advanced query:

    // Create user object
    $u = new User();
    
    // Get all users
    $u->get();
    
    // Loop through all users
    foreach ($u->all as $user)
    {
        // Get the current user's group
        $user->group->get();
        
        // Check if user is related to the Moderator group
        if ($user->group->name == 'Moderator')
        {
            // ...
        }
    }

    Here’s how you would do the above, but using an advanced query:

    // Create user object
    $u = new User();
    
    // Get users that are related to the Moderator group
    $u->where_related_group('name', 'Moderator')->get();
    
    // ...

    As you can see, it’s a big time saver but not just in the amount of code you write, but also in the number of database queries and overall processing time.  There are alternate usage formats as well.  Here’s one of the variants of the above:

    // Create and get Moderator group
    $g = new Group();
    $g->get_by_name('Moderator');
    
    // Create user object
    $u = new User();
    
    // Get users that are related to the Moderator group
    $u->where_related($g)->get();

    Or you could use the advanced Get By:

    $u->get_by_related($g);
    
    // or
    
    $u->get_by_related_group('name', 'Moderator');
    
    // etc.

    Enjoy 😊

  • #351 / Dec 10, 2008 2:02am

    hugle

    289 posts

    Thanks Stensi for sharing it with us 😊
    going to upgrade now.

    You’re fast 😊

    Life is easier with yoU:))))

  • #352 / Dec 10, 2008 2:12am

    stensi

    109 posts

    @hugle: There’s several things you’re doing that you don’t need to, such as creating $f2 when you already have $forum there to use.

    I’d rewrite part 1 as just:

    // save topic count for later use
    $topic_count = $forum->topic->count();

    I’m assuming you’re trying to list the number of topics each forum has, and the number of posts each of those topics have.  Is that correct?  If so, there are many ways of doing it.  I’ll write up a quick sample when I have time.  I can’t right now since you’ve just caught me on my way out the door, sorry!

  • #353 / Dec 10, 2008 2:33am

    hugle

    289 posts

    @stensi: Thanks a lot for such contribution.
    I had quick view of all posts in this thread 😊

    I had changed part1 as you sugested:

    $f->get_by_id($forum->id); // i need this, since there are 4 different subforums, so I need to count topics in that forum
    $topic_count = $f->topic->count();

    somehow I thought it won’t work, since I’m not PHP user too long, only 4 months. But I try to be better each day 😊

    Yes stensi, you are 100% correct.
    I now have 4 subforums, each subforum has N topics, and each of topics in this subforum has N posts.
    I need to count how many posts have topics in each of subforum.

    Thanks for your time.
    Good luck and take care, wherever you would be 😊

  • #354 / Dec 10, 2008 1:08pm

    onnyjay

    4 posts

    I think i have found a bug tho i’m not sure.

    I’m using
    Codeigniter 1.7
    Datamapper 1.5.4


    I have created a table called products and to avoid conflicts trying to load a datamapper model (product) and a web page controller(product) [conflict = they have the same name] i have renamed my datamapper model to Db_product. I also have a table called agents and the two are related. An agent can have many products and a product can have many agents.

    Thusly, I have a table called rel_agents_products (rel_ is my join prefix).

    Next i have added the table variable in the db_product obviously so it knows which table to reference.

    Now the problem…

    I used the get() method on db_agent to call my agent data. then i attempted to load all related products for that agent.

    $agent = new Db_agent();
    $agent->get_by_id(1); // << My agent ID.
    $agent->db_product->get(); << get the related products using my db_product dm model

    when stepping through the code I found that the relationship was trying to use the db_product as the field name in the relationship ie. products.db_product_id.

    obviously this isnt correct as the relationship table consists of id, agent_id and product_id.

    I was forced to modify the Datmapper library to check if the table var has been set and so use a singular version of the table var, else use the model name.

    Is this correct or have I solved a problem that there is another way around?

    This is what i have changed in the Datamapper library.

    Line 2104
    was..
    
    $this->db->join($relationship_table, $this->table . '.id = ' . $relationship_table . '.' . $this->model . '_id', 'left');
    
    ..which i changed to..
    
    $this->db->join($relationship_table, $this->table . '.id = ' . $relationship_table . '.' .(isset($this->table)?singular($this->table):$object->model) . '_id', 'left');
    
    and
    
    line 2105
    was..
    
    $this->db->join($object->table, $object->table . '.id = ' . $relationship_table . '.' . $object->model . '_id', 'left');
    
    ..which i changed to..
    
    $this->db->join($object->table, $object->table . '.id = ' . $relationship_table . '.' . (isset($object->table)?singular($object->table):$object->model) . '_id', 'left');

    I hope i just made sense!?
    Sorry about the long post.
    😊

  • #355 / Dec 10, 2008 3:00pm

    OverZealous

    1030 posts

    Actually, what DM was doing IS correct.  The name of the MODEL is the name of the relationship key.  This is done on purpose, to allow for inheritance.  Check out the Self Referencing Relationships section of the manual.

    I recommend that, instead of renaming your models, think about renaming your controllers to be plural: /products/<whatever>.

    If you do this, you won’t have any conflicts, and it often reads a little better.  That is, of course, your choice.  If you decide to rename the models, you might want to be consistent, then, and rename the whole model, and all of the relationship fields.

  • #356 / Dec 10, 2008 5:34pm

    onnyjay

    4 posts

    Thanks for your response.

    What you say makes sense and I have taken onboard your advice about the naming conventions I use and your right. It does make for easier reading.

    Cheers

  • #357 / Dec 11, 2008 7:15am

    hugle

    289 posts

    @stensi, I somehow figured out the question about counting the posts.. maybe it is not as professional as it could be but It will be good for start 😊

    now I have a question about delete_all function.

    It somehow does not work as I expected. Maybe my approach is not possible at all, I will explain:

    I have assoc array: Array ( [msg_id] => Array ( [0] => 25 [1] => 37 )

    I thought, I could delete entries, like this:
    #example 1

    $m = new Privatemsg();
    $m->where_in('id', array($this->input->post('msg_id')))->get();
    $m->delete_all();

    it produces query:
    “SELECT * FROM (`privatemsg`) WHERE `id` IN (Array)”

    #example 2
    So i tried a different approach:

    $m = new Privatemsg();
                $m->get();
    
                foreach ($m->all as $msg)
                {
                    if (in_array($msg->id, $this->input->post('msg_id'))) {
                        $msg->delete();
                    }
                }

    it throws error, and the QUERY WAS:
    DELETE FROM `privatemsg_users` WHERE `privatemsg_id` = ‘25’.
    it tries to access: `privatemsg_users` table, it is normal behaviour of delete function?
    because my msg table is `privatemsg`

    But after I looked in the database, I saw that that msg with ID = 25, was removed.
    only msg ID 37 left.

    Anybody have thought on this ?
    Or I should somehow extend this functionality?

    Thank you!

    [UPDATE]
    SOrry. example 2 works. I had a line in privatemsg model:
    var $has_one = array(“user”);
    which I removed now.

  • #358 / Dec 11, 2008 7:49am

    stensi

    109 posts

    Sorry but you’re doing a lot of non-standard things so I’m finding it difficult to answer your questions in a straight forward way.  Same with your other issue, as I found it hard to understand the way you were approaching it code-wise.

    Have you read the DataMapper User Guide?  It explains how the delete_all() method works.

    Delete All is used to delete all objects in an objects all list.

    For example, let’s say we have a users table with 100 users and we grab the first 10 users like this:

    // Create user object and get the first 10 users
    $u = new User();
    $u->limit(10)->get();

    The $u object’s all list ($u->all) now contains an array of user objects, which is the 10 users returned.  Doing this:

    // Delete all users
    $u->delete_all();

    ...will delete the users in the $u object’s all list.  That is, it’s the same as doing this:

    foreach ($u->all as $user)
    {
        $user->delete();
    }

    Whenever you do a get() on an object, the all list is populated.  If the get() query resulted in more than one record, they go in the all list as well.

  • #359 / Dec 11, 2008 3:39pm

    meteor

    31 posts

    hi stensi ...
    Just want to say that everything what you have done which is related to DataMapper is AWESOME.
    Haven’t explored all the availeable library , but everything which I have seen so far is great ...

    Using CI I wanted to choose a clean version of active record for this framework ... I’ve choosen DataMapper instaed of ActiveRecord because of the documentation you provided ...it is very clean ... as clean as CI documentation ...

    All in all ... great job man

    best regards

  • #360 / Dec 12, 2008 8:44am

    ntheorist

    84 posts

    hi stensi, great work integrating all the new features with DM. I’ve been ultra busy and haven’t had a chance to pipe in or work much with the new versions. So i installed DM 1.6 and tried running it but i keep getting the error:

    Message: array_keys() [function.array-keys]: The first argument should be an array
    Filename: libraries/datamapper.php
    Line Number: 79

    Message: Invalid argument supplied for foreach()
    Filename: libraries/datamapper.php
    Line Number: 79

    this only happens when i’m extending Datamapper (which is every model plus a common extend i use).. it’s like the static $config var vanishes when extended. I think i’m just missing something simple. I even created a totally blank model too, that just called parent::DataMapper() in the constructor and same error. Any hints here?..

    thanks!

    CC

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

ExpressionEngine News!

#eecms, #events, #releases