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]
  • #226 / Nov 01, 2008 3:39pm

    Aukie

    5 posts

    Thanks Phil,

    You are right I’m more used to functional programmer and did not grasped the full idea about OOP
    Maybe diving into datamapper will make my understanding of OOP come to a new level 😉

    Thanks again for answering and your time.

    Cheers Auke.

  • #227 / Nov 01, 2008 5:52pm

    m4rw3r

    647 posts

    @overZealous, about set_default():

    Horrible performance… All I can say about your code: use SQL instead

  • #228 / Nov 01, 2008 10:28pm

    OverZealous

    1030 posts

    @m4rw3r

    I appreciate your concern.  I’m certain - and never said otherwise - that a traditional pure SQL variation would have better performance.  It would replace the 3 or 4 queries with 2 (depending on how if the Contact is already loaded or not).  If you are updating this field constantly, I’m sure you will see a tiny improvement.

    However, I don’t see anyone changing the field constantly on my end.  In fact, most of my code is about reading data back from the DB.

    You are allowed your opinion, but if you have nothing to offer beside “horrible performance”, then it is merely that - an opinion.  There is more to application development than performance, including development time, maintenance and updatability.

    If you prefer raw SQL designs, then that is your choice.  But please don’t bash my choices - it’s unproductive for everyone.

  • #229 / Nov 01, 2008 10:43pm

    m4rw3r

    647 posts

    I was just criticizing that particular piece of code, not anything else.
    The performance loss is probably not really *that* important in the large perspective, because there are a lot of other things going on.

    As you say, there is maintainability to take in consideration. Normally it would have been about the same. But with the db style of DataMapper, there are the join tables to take in consideration in every relation which makes your method more easily maintained.

  • #230 / Nov 04, 2008 11:42am

    Paul Apostol

    43 posts

    Hello,
    Anybody used a joining table with 3 tables with DM? I really need such a thing. Or do you have any idea how to split them somehow to work with DM?

  • #231 / Nov 04, 2008 11:58am

    OverZealous

    1030 posts

    @ Paul
    Can you give an example, please?

  • #232 / Nov 04, 2008 12:03pm

    Paul Apostol

    43 posts

    It’s about Star schema. I have 3 tables which must be connected like there. I don’t know how to make this work in DM style

  • #233 / Nov 04, 2008 12:12pm

    OverZealous

    1030 posts

    Well, I don’t know anything about star schema.  If you gave a more specific example, I might be able to help better.

    You have a few choices.

    1: You might not be able to use DM in this topology.  It probably isn’t the right tool for data warehousing.

    2: Use a model as your “join” table.  Then you have to edit DM (or create a data mapper extension - please look back through this forum) to allow multiple joins.

    3: Skip around DM for your multi-way joins.

    4: Re-think your design.  Does it actually need multiple joins?  Many times the database design is overly complicated when multiple joins are being used regularly.  Again, without specific examples, I’m shooting from the hip.

  • #234 / Nov 04, 2008 12:24pm

    Paul Apostol

    43 posts

    Let’s see why I need this joining table (just another example):
    1. I have users (user1, user2)
    2. I have books (book1, book2)
    3. I have actions to the books (preview, download)
    The relations are many to many.

    Every user have a set of actions on every book.
    So, “user1” can have only the right to do “preview” and only for “book2”

    On the page I’ll display the list of the books (in our case only “book2”) and the user can do only “preview”.

    The easy part is that in DM style I can easily retrieve the list of books. But how can I retrieve the list of actions related to that book for that user?
    Thanks a lot for your fast answers.

    PS: yes, it’s an idea to skip DM for this particular case.

  • #235 / Nov 04, 2008 12:32pm

    OverZealous

    1030 posts

    Well, option 2 would work in that example.  It’s not the prettiest, but it solves the problem:

    Users have many Bookrules
    Books have many Bookrules
    Actions have many Bookrules

    Bookrules has one Book
    Bookrules has one User
    Bookrules has one Action

    Your model tables would be:
    users, books, actions, bookrules

    Your relationship tables would be
    actions_bookrules
    bookrules_books
    bookrules_users

    You could then get the books through

    $user->bookrules->get();
    foreach($user->bookrules->all as $bookrule) {
        $book = $bookrule->book->get();
        $action = $bookrule->action->get();
    }

    Update:  I forgot to mention that this will generate a lot of queries on the server.  You might be able to reduce them using some of the above mentioned additions to DM.

  • #236 / Nov 04, 2008 12:36pm

    Paul Apostol

    43 posts

    thanks again. I’ll give it a try

  • #237 / Nov 05, 2008 3:58pm

    bojack

    6 posts

    I am looking to implement an automatic relationship for any new record from one model to another. IE: any new ‘permission’ is automatically related to the ‘root’ role. I have tried something like this:

    public function save($object = '') {
        // check if this is a new record
        if (empty($this->id))
        {
            // get role that we want added to all new permissions
            $role = new Role();
            $role->where('title', 'root')->get();
            // add role to $object
            if (empty($object)) $object = $role;
            elseif (is_array($object)) $object[] = $role;
            else $object = array($object, $role);
        }
        return parent::save($object);
    }

    Using this function not only is the new relationship not saved, the new record itself is not created. Any tips about what I am missing (probably something obvious).

  • #238 / Nov 05, 2008 4:03pm

    OverZealous

    1030 posts

    First, if you pass an object into save() it will only save a relationship.  So, you didn’t actually save the object at all.  Second, you need to save $this first.

    public function save($object = '') {
        // check if this is a new record
        $was_new = empty($this->id);
        parent::save($object);
        if ($was_new)
        {
            // get role that we want added to all new permissions
            $role = new Role();
            $role->where('title', 'root')->get();
            $this->save($role);
        }
        return $this;
    }
  • #239 / Nov 05, 2008 10:34pm

    ___seb

    4 posts

    hi,

    I’m new to dattamapper and orm.

    I have prob trying to get the tags of the links that have one specified tag.
    I mean : Tag xxx -> links with that tag -> all tags of each link.

    $tag = new tag();
        $tag->where('id','19')->get();
    
        echo 'tag : '.$tag->tag.'<br>';
        $tag->bookmark->select('url','description')->get(); //all
    
        //$tag->bookmark->select('id','url')->get()->tag->select('tag')->order_by('rnd()')->get();
        //echo 'test tag : '.$tag->bookmark->tag->tag.'';
    
        foreach($tag->bookmark->all as $b1)
        {
        
            echo '<h3>'.$b1->url.'</h3><p>';<br />
            <br />
            echo ''.$b1->description.'<br />
    Tags :';<br />
            echo 'count : '.count($b1->tag->select('tag')->get()->all);<br />
            foreach($b1->tag->select('tag')->get()->all as $t) // <== No tags found :/<br />
            {</p>
    
    <p>            echo ' '.$t->tag;<br />
            }<br />
            echo '';<br />
                <br />
        }

    No tags are found in the second foreach.

    ( The table tags has a field ‘tag’ (the name/string) )

    any idea ? (i tried some things without success)

  • #240 / Nov 05, 2008 10:44pm

    OverZealous

    1030 posts

    What you are doing wrong is not selecting the ID field.  In fact, you probably don’t need the ->select() at all.

    By not selecting the id field, DM cannot look up the relationships, since it doesn’t know the ID of the bookmark.

    Try this:

    $tag = new tag();
        $tag->where('id','19')->get();
    
        echo 'tag : '.$tag->tag.'<br>';
        $tag->bookmark->get(); //all
    
        foreach($tag->bookmark->all as $b1)
        {
        
            echo '<h3>'.$b1->url.'</h3><p>';<br />
            <br />
            echo ''.$b1->description.'<br />
    Tags :';<br />
            // only need to call this once.<br />
            $b1->tag->get();<br />
            echo 'count : '.count($b1->tag->all);<br />
            foreach($b1->tag->all as $t)<br />
            {</p>
    
    <p>            echo ' '.$t->tag;<br />
            }<br />
            echo '';<br />
                <br />
        }

    If you need to select, make sure you select the ‘id’ field as well.

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

ExpressionEngine News!

#eecms, #events, #releases