CodeIgniter Forums

Full Version: Saving relationships in Datamapper
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]Wazzu[/eluser]
Hi, all. This is a newbie question about Datamapper.
My code is working but I'm not sure if this is the right way.
Imagine I have a users table (id, name), a phones table (id, number) an a relation table called users_phones (user_id, phone_id)

I want to use the same form to add and edit, so I'm doing this way:

From the controller
Code:
function form() {
    $u = new User();
    $t = new Phone();
    if($this->input->post('add')) {
        $u->name   = $this->input->post('name');
        $t->number = $this->input->post('number');
        $t->save();
        $u->save($t);
    }
    $this->load->view('form');
}

And from the view

Code:
<input name="name" value="<?php echo $u->name ?>">
<input name="number" value="<?php echo $t->number ?>">

Q1 Up to here, is this the correct procedure to save it? or I should use only one model?

Q2 How can I reuse this form to edit an existing record?

Thanks in advance

El Forum

[eluser]jbreitweiser[/eluser]
Why do you need a joining table. Shouldnt you just have a phone table with the users ID as part of the key? Unless you plan on using the phone number over and over with different people, but then I contend that its a small amount of information that dos not need a joining table. Also, unless you plan on having multiple phone numbers(ie cell, office, house, etc.) you should just save it as part of the user record. No need for a second table at all.

Using the multiple phone number schema, I would save the user first, then save the phone number using the user primary key as the primary key for the phone number record if the user save was successfull. The number would use the same key_id at the user record as its primary key. I hope this helps.

El Forum

[eluser]Wazzu[/eluser]
[quote author="jbreitweiser" date="1266437098"]Why do you need a joining table. Shouldnt you just have a phone table with the users ID as part of the key? Unless you plan on using the phone number over and over with different people, but then I contend that its a small amount of information that dos not need a joining table. Also, unless you plan on having multiple phone numbers(ie cell, office, house, etc.) you should just save it as part of the user record. No need for a second table at all.[/quote]
This is just a sample, no matter what fields are, the question is about saving a parent object and a child one

[quote author="jbreitweiser" date="1266437098"]Using the multiple phone number schema, I would save the user first, then save the phone number using the user primary key as the primary key for the phone number record if the user save was successfull. The number would use the same key_id at the user record as its primary key. I hope this helps.[/quote]
Forgot to tell, I'm working with Datamapper ORM

Thanks for answering ;-)

El Forum

[eluser]Wazzu[/eluser]
Listen, Wazzu, you can have just one object, like this:

Code:
function form() {
    $f = new Friend();
    $f->phone->get();
    if($this->input->post('submit')) {
        $f->name          = $this->input->post('name');
        $f->phone->number = $this->input->post('number');
        $f->phone->save();
        $f->save();
    }
    $this->load->view('form');
}

So that you save 'phone' object and then save 'friend' object

Then, in your view file:

Code:
<form method="post">
    &lt;input type="text"   name="name"   value="&lt;?=$friend-&gt;name ?&gt;"><br />
    &lt;input type="text"   name="number" value="&lt;?=$friend-&gt;phone->number ?&gt;"><br />
    &lt;input type="submit" name="submit" value="save"&gt;
&lt;/form&gt;

Good luck!