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 - 11-04-2008

[eluser]OverZealous[/eluser]
@ Paul
Can you give an example, please?


DataMapper 1.6.0 - El Forum - 11-04-2008

[eluser]Paul Apostol[/eluser]
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


DataMapper 1.6.0 - El Forum - 11-04-2008

[eluser]OverZealous[/eluser]
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 <i>need</i> 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.


DataMapper 1.6.0 - El Forum - 11-04-2008

[eluser]Paul Apostol[/eluser]
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.


DataMapper 1.6.0 - El Forum - 11-04-2008

[eluser]OverZealous[/eluser]
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
Code:
$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.


DataMapper 1.6.0 - El Forum - 11-04-2008

[eluser]Paul Apostol[/eluser]
thanks again. I'll give it a try


DataMapper 1.6.0 - El Forum - 11-05-2008

[eluser]bojack[/eluser]
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:

Code:
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).


DataMapper 1.6.0 - El Forum - 11-05-2008

[eluser]OverZealous[/eluser]
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.

Code:
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;
}



DataMapper 1.6.0 - El Forum - 11-05-2008

[eluser]___seb[/eluser]
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.

Code:
$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 '<p>test tag : '.$tag->bookmark->tag->tag.'</p>';

    foreach($tag->bookmark->all as $b1)
    {
    
        echo '<h3>'.$b1->url.'</h3>';
        
        echo '<p>'.$b1->description.'</p><p>Tags :';
        echo 'count : '.count($b1->tag->select('tag')->get()->all);
        foreach($b1->tag->select('tag')->get()->all as $t) // <== No tags found :/
        {

            echo '&nbsp;'.$t->tag;
        }
        echo '</p>';
            
    }

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)


DataMapper 1.6.0 - El Forum - 11-05-2008

[eluser]OverZealous[/eluser]
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:
Code:
$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>';
        
        echo '<p>'.$b1->description.'</p><p>Tags :';
        // only need to call this once.
        $b1->tag->get();
        echo 'count : '.count($b1->tag->all);
        foreach($b1->tag->all as $t)
        {

            echo '&nbsp;'.$t->tag;
        }
        echo '</p>';
            
    }

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