CodeIgniter Forums
DataMapper 1.6.0 - Printable Version

+- CodeIgniter Forums (
+-- Forum: Archived Discussions (
+--- Forum: Archived Libraries & Helpers (
+--- Thread: DataMapper 1.6.0 (/showthread.php?tid=11358)

DataMapper 1.6.0 - El Forum - 02-19-2009

This is one of the things I was trying to solve. In the current, official version of DataMapper, you have to have a column for every model type when working with multiple relationships (to the same table).

In other words, you need this as your join table:
- file_id NOT NULL
- user_id
- creator_id

Note that they must allow nulls. Now the queries will work.

However, there is a big problem with this. If you look up the creator ($file->creator->get()), that creator will not work for any relationships that were set as a user. Assuming that Users have a relationship to Roles, this will not work:
$creator = $file->creator->get();
$role = $creator->role->get(); // there is no relationship here, because it will look for the column creator_id again.

$user = new User();
$role = $user->role; // this works, now

In the updated version, you can assign the same exact model in multiple ways, just using different field names:
class User extends DataMapper {
    $has_many = array(
        'created_file' => array(
            'class' => 'file',
            'other_field' => 'creator'

class File extends DataMapper {
    $has_one = array(
        'creator' => array(
            'class' => 'user',
            'other_field' => 'created_file'

// in controller
$creator = $file->creator->get(); // $creator is a User class
$user = $file->user->get(); // ditto

What's even better, you can create the file table like this:
- id,
- ...
- user_id int32, Must allow nulls
- creator_id int32 Must allow nulls

DataMapper 1.6.0 - El Forum - 02-19-2009

Thank you so much for your explanation Phil. I am still very new to CodeIgniter and ORM. But learning very fast and loving it. I will try to use your updated version of DataMapper with my application and let you know how it works out. Since I am new to CI and DataMapper your documentation file wasn't that easy to comprehend compared to DM's user guide. But I am trying my best since I really appreciate all your contributions to this great library!

DataMapper 1.6.0 - El Forum - 02-19-2009

I apologize for not having better examples prepared. My own projects have taken up a lot of time lately, so I appreciate the effort being put in by those testing it. I know my "documentation" was fairly light. I'll try to get a complete example available some time soon!

DataMapper 1.6.0 - El Forum - 02-20-2009

First, thanks for this class; I'm definitely having better productivity with it!

I'm a little puzzled over something. If I want to display all records from a table, I cannot use a select call. For example:

//This allows me to list all users
$u = new User();

//Say I want to show just their username and email
$u = new User();
//This will only list one record :-\

Is there any way to be able to select only the needed fields? I don't usually need every column when making a call. Any advice/direction would be appreciated! :-)

DataMapper 1.6.0 - El Forum - 02-20-2009

Select works. You might want to check the queries being run, and make sure you aren't getting the correct results.

DataMapper 1.6.0 - El Forum - 02-21-2009

D'oh! I just looked at one of my views again and realized I was using the same variable in my foreach as in the controller. That is:

$u = new User();
$data['users'] = $u->all;

foreach($users as $u) {
Username: $u->username<br />
Email: $u->email

I completely forgot that accessing it via $u->whatever would only show me one record. Blush Changing it to $use in the view solved it.

Thanks for the response, OverZealous!

DataMapper 1.6.0 - El Forum - 02-22-2009

Hey, I was wondering how do I repopulate a form with its previous values if the data is validated and there is an error?

I've tried using what I've found in DataMapper's Validation docs and the Form Helper & Form Validation docs for CodeIgniter. I'm using the DataMapper model's save() method to validate the data & save if valid. If the save fails, then I redisplay the form with the errors. I figured I'd use the Form Helper method set_value() to repopulate the fields as they show in the docs but that doesn't seem to work for me. Here's the code I have:

Controller Method:

function add()
    $post = new Post();
    $post->title = $this->input->post('title');
    $post->body = $this->input->post('body');
    $post->teaser = $this->input->post('teaser');
    $post->slug = $this->input->post('slug');
    $post->published = $this->input->post('published');
    // Apply Markdown on `body` text before saving.
    if ($post->save())
      $data = array(
        'title'=> $post->title,
      $this->load->view('admin/post_created', $data);
      $data = array(
        'title' => 'Create Post',
        'error' => $post->error->string,
      $this->load->view('admin/post_create', $data);

And the view:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
&lt;html &gt;

    &lt;link rel="stylesheet" href="&lt;?=base_url()?&gt;/media/css/admin.css" type="text/css" media="screen" /&gt;

    <div id="dashboard-wrapper">
    <div id="app">


          <div class="title">
            <h2>Write a new Post.</h2>
          <div id="test" class="test">
          <div class="field required">
            <label for="body" class="label">Content <span class="help"> And so the boy roared.</span></label>
            <div class="input">&lt;?=form_textarea('body', set_value('body'));?&gt;</div>    

          <div class="field required">
            <label for="title" class="label">Title <span class="help">Make it pop and fizz.</span></label>  
            <div class="input">&lt;?=form_input('title', set_value('title'));?&gt;</div>

          <div class="field">
            <label for="teaser" class="label">Teaser <span class="help">Sell the sizzle.</span></label>        
            <div class="input">&lt;?=form_input('teaser', set_value('teaser'));?&gt;</div>

          <div class="field required">
            <label for="slug" class='label'>Slug <span class="help">URL friendly title.</span></label>
            <div class="input">&lt;?=form_input('slug', set_value('slug'));?&gt;</div>

          <div class="field">
            &lt;?=form_label('Published','publish', array('class' => 'label'));?&gt;    
            &lt;?=form_checkbox('publish', 'publish', TRUE);?&gt;

          <div class="field">
            <label class="label">&nbsp;</label>
            <div class="input">&lt;?=form_submit('create', 'Save');?&gt;</div>

          &lt;!-- Does set value work? --&gt;
          <p>&lt;?=set_value('body') ?&gt;</p>


Aside from not repopulating the user's input if the form doesn't pass validation, it works Tongue But I'd really like to be able to repopulate the data the user already entered, especially if some of it was valid

DataMapper 1.6.0 - El Forum - 02-22-2009

For future reference, please be a little more selective in what you post - obviously we don't need the whole web page.

When using DataMapper, you don't have to resort to any tricks for your form. Simply pass the same object in before or after it is edited:
// controller
function edit($id = NULL) {
    $post = new Post();
    // do security checks, etc.
    if( ! is_null($id)) {
        // load in the object to be edited
        // verify input
    } else if($this->input->post('id') !== FALSE) {
        // load in the already edited object
        // save form data, validate, etc.
        // redirect on successful save
    } else {
        show_error("Invalid id.");
    $this->load->view('edit_post', array('post' => $post));

// in view
&lt;input type="text" name="title" id="title" value="&lt;?= htmlspecialchars($post-&gt;title) ?&gt;"/> &lt;?= $post->error->title ?&gt;
// etc.

You can expand this to be a little smarter, if necessary. You can even use the same form for adding new ones, simply don't throw an error if the ID is not passed. On saving, the id will be 0, so you can check for that if necessary. Calling ->save() will work for new or existing objects, and you can get the id of the saved object as $post->id on new saves as well.

DataMapper 1.6.0 - El Forum - 02-22-2009

Oh, great, thanks! That's actually alot simpler than I thought it would be. Sorry about being so verbose and thanks for the tips Smile

DataMapper 1.6.0 - El Forum - 02-22-2009

OverZealous I put some thought into why my single join table thing isnt work. (your new method with relations in the main table).

The reason why $asd->author->get(); as show below would not work is because there is no model named author... So my question to you is how are we to get author_id out of the dqip and relate it to the proper user.

This is where im at a loss... Since I know trying to get the author does not work.

Would I have to grab the author_id and manually relate the author_id to the user table?

Heres the example:

$asd = new Dqip();
$asd->data_source->get(); // Does work since there is a data_source model (not related within the dqip table)
$asd->author->get(); // Does not work since there is no author model

user model:
var $has_many = array(  "brainstorm",

                            /* DQIP User Fields */
                            "dqip_author"       => array (  'class' => 'dqip',
                                                            'other_field' => 'author'),
                            "dqip_dciu_staff_1" => array (  'class' => 'dqip',
                                                            'other_field' => 'dciu_staff_1'),
                            "dqip_dciu_staff_2" => array (  'class' => 'dqip',
                                                            'other_field' => 'dciu_staff_2')

Dqip Model:
var $has_one = array( "author"          => array(   'class' => 'user',
                                                        'other_field' => 'dqip_author' ),

                          "dciu_staff_1"    => array(   'class' => 'user',
                                                        'other_field' => 'dqip_dciu_staff_1'),

                          "dciu_staff_2"    => array(   'class' => 'user',
                                                        'other_field' => 'dqip_dciu_staff_2')