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-10-2009

[eluser]easylancer[/eluser]
Ok I have some problems with Datamapper, maybe its me not understanding it or maybe its documentation just doesn't have this scenario. I have a table model called driver and another called car, I want to be able to save a driver to a car at the same time when i'm saving a new car and a new driver. Eg.
Code:
// Car Model
$c = new Car();
$c->model = $_POST['model'];
$c->year = $_POST['year'];

// Driver Model
$d = new Driver();
$d->name = $_POST['name'];
$d->age = $_POST['age'];
$d->insurance = $_POST['insurance'];

$d->save();
$c->save();
How can I save the driver to the car at this point?


DataMapper 1.6.0 - El Forum - 02-10-2009

[eluser]OverZealous[/eluser]
First, I don't believe you have read the docs, since this is clearly explained, several times.

Second, you HAVE to save each item at least once. So, you could call it this way:
Code:
$c->save();
$d->save($c);

DataMapper is smart enough not to re-save an object, so this would result in the exact same number of queries:
Code:
$c->save();
$d->save();
$c->save($d);

Finally, I hope that is example code, because you should not be using $_POST with CodeIgniter. Instead, use $this->input->post('field_name'), which is more secure.


DataMapper 1.6.0 - El Forum - 02-10-2009

[eluser]easylancer[/eluser]
Hi OverZealous.com, thanks for such a quick reply. I read the docs actually and this doesn't exist in there, as all the examples on the save page were querying the object before it stores the relationship. All the examples i saw would do something like this
Code:
// Get user foo
$u = new User();
$u->where('username', 'foo')->get();

// Get country object for Australia
$c = new Country();
$c->where('name', 'Australia')->get();

// Relate user foo to country Australia
$u->save($c);

It would be nice if stensi could add this as an example to the documents.


DataMapper 1.6.0 - El Forum - 02-10-2009

[eluser]tdktank59[/eluser]
I was having problems savings relations like that...

For some reason it would not make the link... but save the 2 tables...


DataMapper 1.6.0 - El Forum - 02-11-2009

[eluser]OverZealous[/eluser]
DataMapper Updates

Stensi appears to be busy right now (hopefully doing something that makes money!), so I thought I'd try something out. I've been working on an overhaul of DataMapper that allows for a few new features, and improves upon the relationship model.

These are the major changes:
1. Now supports more customizable relationships
⁃ One-to-one and one-to-many relationships can be joined without a join table. The "one" side needs to be stored as "type_id", where type can be anything.
Multiple, different relationships of the same object can occur now. For example, a User can be both an Editor and a Creator for a blog post.
⁃ Self-references no longer require additional models. Just specify the fields explicitly, within the same object.
⁃ Relationships are defined by the name of the related field, and that field can point to any class, and use any column names (that end in _id).
The old technique works exactly as it did before. No existing code should need any changes, unless it was working within DataMapper's non-public methods.
⁃ Join table names are still bars_foos for non-self-references. For self-references, the join tables are barcolumns_foocolumns, where barcolumns is the plural of the column name for bar, and foocolumns is the plural of the column name for foo.
⁃ See the attachments for the exact new formatting for the has_one and has_many arrays.
2. One-to-N related objects can be joined into a query, so that the values from multiple objects can be queried at once.
⁃ Use the method $object->join_related($field_name, $other_fields)
⁃ $field_name can either be a related field, or an object. The object will only work it $object->model is the same as it's field_name.
⁃ $other_fields should be the name or names (as an array) of fields to join in this query. It is required, and cannot be '*'.
3. Because an object can be saved to multiple fields, it may be necessary to specify which field when saving or deleting the relationship.
⁃ For example, to save the editor User to a Post, call $post->save_editor($user) or $user->save_editedpost($post).
⁃ If you have the field name in a string, you can also call $post->save($user, 'editor').
⁃ If you are saving multiple objects, you can save them as $post->save(array('editor' => $user)).
⁃ Specifying the field is ONLY necessary if the field is not the same as the name of the Model.

What I'd like is some help with this unofficial version. There's over 150 lines-of-code changed in this version, and while I tested it some, I might have missed some things, or there might be edge cases I missed. (I'm already using it on my testing server, and have reduced the number of database tables by about half just by using the in-table has_one relationships.)

So, if you are feeling lucky, please try out the DataMapper.php included in the ZIP file. For an example of how to use these new features, please read DataMapper Changes.rtf in the ZIP. I've included a lot more information about how the new relationships work.


DataMapper 1.6.0 - El Forum - 02-11-2009

[eluser]Daniel H[/eluser]
Phil this sounds really exciting - looking forward to trying it out.


DataMapper 1.6.0 - El Forum - 02-11-2009

[eluser]bEz[/eluser]
Is DMevolution on the horizon?
Sounds right up my alley, although I'd still like to wrap-up my dev trials re-organizing my db to adhere to DM.


DataMapper 1.6.0 - El Forum - 02-11-2009

[eluser]tdktank59[/eluser]
Sounds Great!!!


DataMapper 1.6.0 - El Forum - 02-13-2009

[eluser]tdktank59[/eluser]
Any idea on how we can pull columns in the join tables?


DataMapper 1.6.0 - El Forum - 02-13-2009

[eluser]macigniter[/eluser]
First of all... great job with the update. I'll make sure to give it a try asap.

Until then I noticed something today that confused me...

- Users belong to one Client, although this relation might not be set
- Client has many Products

Quote:$u = new User();
$u->get_by_id($user_id);

$u->group->get();
$u->client->get();

$p = new Product();
$p->get_by_related($u->client);

foreach ($p->all as $product) echo $product;

What I thought was strange is the fact that if a user does not belong to a client DataMapper still spits out all products for this user.

Shouldn't $p->get_by_related($u->client) only return actual relations if a client was assigned to the user?