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 - 02-22-2009

[eluser]OverZealous[/eluser]
Are you using the second updated one I posted? That might be the problem. The earlier version required that 'join_other_as' be set, while the second one requires that 'other_field' is set (like you have).

You don't need to have a model named 'author' for it to work. The code inside DM looks up the "model" based on 'join_self_as', 'other_field', or $this->model in that order.

Basically, when you call $asd->author->get(), this is what happens:
* The property author triggers a look up to determine which class to instantiate. In this case, it looks at $has_one['author'], and sees the class is User (lowercase user is OK here).
* A blank User is created. This User is told that its parent's id is $this, and it is related through other_field (in this case, dqip_author).
* When you call get(), DM notices the parent information, and runs the query based on that. It looks up $has_many['dqip_author'] to determine how to query.
* dqip_author says the field to relate on is author (because that's the other_field name)
* The table is determined to be dqips, because the field author_id is available on that table.
* Finally, the tables are joined and the query proceeds.

Assuming you are using the updated version, the only place you would see an error about join_dqips_users is if author_id is not on the dqips table.

Let me know if that helps any. Otherwise, I'll happily take a look at your server again ;-), because I want to make sure the bug isn't in my code!


DataMapper 1.6.0 - El Forum - 02-22-2009

[eluser]Yman[/eluser]
can anyone see what is wrong with my code here:

Code:
foreach($latest_entry->all as $e){
    
    echo "Reference No:" . $e->entry_no;
    $e->product->get();   //I'm accessing relationship to
                          // "product", and Yes, Product model exist
    echo "Product:" . $e->product->name;
    echo "Knowledge Fragment:" . $e->know_fragment;
    echo "Severity Level:" . $e->severity;
    }

It get error saying "Call to a member function get() on a non-object"

and also, am i doing the saving relationships correctly?

Code:
$n = new Entry();
        $n->entry_no = $this->input->post('entry_no');
        $n->know_fragment = $this->input->post('know_fragment');
        $n->severity = $this->input->post('severity');
        $n->remark = $this->input->post('remark');
        $n->solution = $this->input->post('solution');
        $n->onoff = $this->input->post('onoff');
        
        //saving the relationships
        $c1 = new Horizontal();
        $c1->get_by_name($this->input->post('horizontal'));
        $c2 = new Product();
        $c2->get_by_name($this->input->post('product'));
        $c3 = new Descriptor();
        $c3->get_by_name($this->input->post('descriptor'));
        $c4 = new Dataresource();
        $c4->get_by_name($this->input->post('dataresource'));
        $c5 = new Ppnbg();
        $c5->get_by_name($this->input->post('ppnbg'));
        $c6 = new Persona();
        $c6->get_by_name($this->input->post('persona'));
        $c7 = new Maintask();
        $c7->get_by_name($this->input->post('maintask'));
        $c8 = new Rawdatatype();
        $c8->get_by_name($this->input->post('rawdatatype'));
        
        $n->save(array($c1, $c2, $c3, $c4, $c5, $c6, $c7, $c8));



DataMapper 1.6.0 - El Forum - 02-23-2009

[eluser]tdktank59[/eluser]
@overzealous

I had no clue you released a new version lol You should put some version numbers on yours so we know whats what lol.

Other than that ill let you know if it fixes it.

Update

Fixed the problem however id like to point out something you may want to look at.
Notice all the user table joining looks like 3 times for each user get i want to grab.

Quote:0.0002 SELECT * FROM `dqips` LIMIT 1

0.0001 SELECT *
FROM (`dqips`)
WHERE `id` = '1'

0.0001 SELECT * FROM `data_sources` LIMIT 1

0.0001 SELECT `data_sources`.*
FROM (`data_sources`)
LEFT JOIN `join_data_sources_dqips` as join_data_sources_dqips ON `data_sources`.`id` = `join_data_sources_dqips`.`data_source_id`
LEFT JOIN `dqips` as dqips ON `dqips`.`id` = `join_data_sources_dqips`.`dqip_id`
WHERE `dqips`.`id` = '1'

0.0001 SELECT `data_sources`.*
FROM (`data_sources`)
LEFT JOIN `join_data_sources_dqips` as join_data_sources_dqips ON `data_sources`.`id` = `join_data_sources_dqips`.`data_source_id`
LEFT JOIN `dqips` as dqips ON `dqips`.`id` = `join_data_sources_dqips`.`dqip_id`
WHERE `dqips`.`id` = '1'

0.0001 SELECT * FROM `users` LIMIT 1

0.0001 SELECT `users`.*
FROM (`users`)
LEFT JOIN `dqips` as dqips ON `users`.`id` = `dqips`.`author_id`
WHERE `dqips`.`id` = '1'

0.0002 SELECT `users`.*
FROM (`users`)
LEFT JOIN `dqips` as dqips ON `users`.`id` = `dqips`.`author_id`
WHERE `dqips`.`id` = '1'

0.0001 SELECT `users`.*
FROM (`users`)
LEFT JOIN `dqips` as dqips ON `users`.`id` = `dqips`.`dciu_staff_1_id`
WHERE `dqips`.`id` = '1'

0.0001 SELECT `users`.*
FROM (`users`)
LEFT JOIN `dqips` as dqips ON `users`.`id` = `dqips`.`dciu_staff_1_id`
WHERE `dqips`.`id` = '1'

0.0001 SELECT `users`.*
FROM (`users`)
LEFT JOIN `dqips` as dqips ON `users`.`id` = `dqips`.`dciu_staff_2_id`
WHERE `dqips`.`id` = '1'

0.0001 SELECT `users`.*
FROM (`users`)
LEFT JOIN `dqips` as dqips ON `users`.`id` = `dqips`.`dciu_staff_2_id`
WHERE `dqips`.`id` = '1'

0.0001 SELECT `users`.*
FROM (`users`)
LEFT JOIN `dqips` as dqips ON `users`.`id` = `dqips`.`author_id`
WHERE `dqips`.`id` = '1'

0.0001 SELECT `users`.*
FROM (`users`)
LEFT JOIN `dqips` as dqips ON `users`.`id` = `dqips`.`dciu_staff_1_id`
WHERE `dqips`.`id` = '1'

0.0001 SELECT `users`.*
FROM (`users`)
LEFT JOIN `dqips` as dqips ON `users`.`id` = `dqips`.`dciu_staff_2_id`
WHERE `dqips`.`id` = '1'

0.0001 SELECT `data_sources`.*
FROM (`data_sources`)
LEFT JOIN `join_data_sources_dqips` as join_data_sources_dqips ON `data_sources`.`id` = `join_data_sources_dqips`.`data_source_id`
LEFT JOIN `dqips` as dqips ON `dqips`.`id` = `join_data_sources_dqips`.`dqip_id`
WHERE `dqips`.`id` = '1'



DataMapper 1.6.0 - El Forum - 02-23-2009

[eluser]wolffc[/eluser]
[quote author="commandercool" date="1232688587"][quote author="wolffc" date="1232685529"]Is it possible to give an order column to the joining table? Lets say a user can pick 3 of 10 options and set a specific order. Would they be inserted and retrived in the same order?


Thanks[/quote]

I've been toying with this idea of building in methods that access additional fields in the relationship tables..

essentially though, you can add whatever fields you like, in your linking table as long as you have the id, model1_id, model2_id fields..

so say you want to get a users groups, which would have a sort order, you could add in an order_number column on the groups_users table, and then (with a join specified), you can try
Code:
$user = new User();
$user->where('id',$userID)->get();
$user->group->order_by('groups_users.order_number', 'asc');
$user->group->get();

does that make sense?.. you just have to literally specify the relationship table name for now.

CC[/quote]

So, getting the data is not a problem. My problem is actually setting the order. I can write a sql query to do it but was wondering if there was a way to actaully set the order using the save method. Is it possible to actually save an order field in my joining table?


DataMapper 1.6.0 - El Forum - 02-23-2009

[eluser]DominixZ[/eluser]
Today I do my project with DataMapper and I have these models.
- Bookmark
- User
- Tag
Bookmark has_many Tag
Tag has_many Bookmark
User has_many Bookmark
Bookmark has_one User

and my question. Is it possible to delete relationship with one step? Now, I must use code like this to complete delete relationship
$bookmark->user->get();
$bookmark->tag->get();

$bookmark->delete($bookmark->tag);
$bookmark->delete($bookmark->user);
$bookmark->delete();

Please lead me the right way to complete this.


DataMapper 1.6.0 - El Forum - 02-23-2009

[eluser]OverZealous[/eluser]
@tdtank

Nothing in my code should cause there to be more queries than before. The piece you are using simply uses a different "join" table (no table, in this case).

DataMapper has always run a query every time you call ->get(). Since each type of user is related differently, it is going to be a different join.

There really isn't a way in SQL to query multiple objects in different relationships into the same result set, especially without losing the "meaning" of the relationship. So, if the same user is related as "editor" and "creator", that user will be queried twice.

I can look into seeing if there is a practical way of looking through the relationships and seeing if the same object is already loaded, but that probably won't be terribly efficient, especially since these primary key queries.

@DominixZ
As it says in the manual, you can delete multiple relationships using one call. However, that is completely unnecessary in your case, because calling $bookmark->delete() automatically deletes all relationships (also mentioned above, in red). You don't even have to ->get() the related items:
Code:
$bookmark = ...
// delete bookmark and all relationships (but not the related items themselves)
$bookmark->delete();



DataMapper 1.6.0 - El Forum - 02-23-2009

[eluser]tdktank59[/eluser]
Sounds good now I at least understand what its doing lol!


DataMapper 1.6.0 - El Forum - 02-25-2009

[eluser]quasiperfect[/eluser]
i'm new to datamapper, actually is my first use and i don't understand something why the code
Code:
$u = new User();$u->get_by_email('a');if ($u->exists()){echo 'k'}
generates 2 queries
Code:
SELECT * FROM `users` LIMIT 1
SELECT * FROM (`users`) WHERE `email` = 'a'



DataMapper 1.6.0 - El Forum - 02-25-2009

[eluser]tdktank59[/eluser]
Ill try and awnser this but I may be wrong...

It does 2 queries because the first one gets the user object setup/populated then the second query limits the results based on what you want to get.


DataMapper 1.6.0 - El Forum - 02-25-2009

[eluser]quasiperfect[/eluser]
why use 2 queries ? when it could simply do
Code:
SELECT * FROM (`users`) WHERE `email` = 'a' LIMIT 1
i'm traing to understand if is a advantage using datamapper but if it uses more queries then normal code i don't understand where's the advantage