CodeIgniter Forums
DataMapper ORM v1.8.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 ORM v1.8.0 (/showthread.php?tid=37531)



DataMapper ORM v1.8.0 - El Forum - 02-15-2011

[eluser]WanWizard[/eluser]
@Basketcasesoftware,

Very good effort, and very close... Smile

Reciprocal relationships can only be defined for a self-relationship (altough technically it could be between any two tables).

The best example I can think of is in the area of genealogy.

Suppose you have a table with information about people, and you want to relate them (grandparent-parent-child etc). You always have to make the relation both ways, since it can't be that person A is the child of person B, but B is not the parent of A. If you define this relation as reciprocal, when you save the relation between A and B, automatically the relation between B and A is saved as well. Two INSERT queries are generated for the relationship table.

The same is true for deleting the relation.

I have added this example in the docs, I think it clearly describes the functionality.


DataMapper ORM v1.8.0 - El Forum - 02-15-2011

[eluser]Basketcasesoftware[/eluser]
It was somewhere between 3 and 4 am here when I was working on that reply. I figured you'd give a better example when you managed to get around to the subject. Your example actually brings up a question I have. The type of relation in your example depends on the direction of the relationship. Is there a way easily distinguish that fact? I have a vague idea, but nothing definite. A is Child of B, but B is Parent of A. A is Husband of B, but B is Wife of A.


DataMapper ORM v1.8.0 - El Forum - 02-15-2011

[eluser]al3xandre[/eluser]
Hi Guys

Just a little question, I know how to retrieve the records and access to them through a loop :

Code:
$tmpPoint = new Point() ;
$tmpPoint->get() ;
                
foreach($tmpPoint as $pt) {
    ...    
}

But how can I access to the first object ? something like $tmpPoint[0] but it doesn't work.

Thank you


DataMapper ORM v1.8.0 - El Forum - 02-15-2011

[eluser]kupcza[/eluser]
Hi guys. I've got little problem. I'm using DataMapper to creating / editing users. But my 'users' table has no ID (I use user_id instead - this is because of DX_auth).

Well, here's the problem. When I'm trying to save user (just updating info), DataMapper thinks, that it's a new user.

My questions is, if there's some way how not to create extra column ID (which has same value like user_id) in users table.


DataMapper ORM v1.8.0 - El Forum - 02-15-2011

[eluser]Basketcasesoftware[/eluser]
I was thinking there was a configuration option for that. Nope. Maybe edit your copy of DX_Auth? That's a problem I've noticed with auth and access libraries here is the tight coupling to a specific database model...
Idea!

Watch this space. :coolgrin:


DataMapper ORM v1.8.0 - El Forum - 02-16-2011

[eluser]cladff[/eluser]
Hi,

i use your nested set and i would like to know if somebody has a function to show a html list (<ul> <li>) for example with a tree?

Thanks in advance


DataMapper ORM v1.8.0 - El Forum - 02-16-2011

[eluser]NeoArc[/eluser]
[quote author="al3xandre" date="1297824876"]Hi Guys

Just a little question, I know how to retrieve the records and access to them through a loop :

Code:
$tmpPoint = new Point() ;
$tmpPoint->get() ;
                
foreach($tmpPoint as $pt) {
    ...    
}

But how can I access to the first object ? something like $tmpPoint[0] but it doesn't work.

Thank you[/quote]

get_iterated() doesn't work
get() works for me.

Sorry, I didn't post a working example:
Code:
echo $tmpPoint->x;



DataMapper ORM v1.8.0 - El Forum - 02-16-2011

[eluser]WanWizard[/eluser]
@NeoArc,

get_interated() fetches records one at the time, and is meant to be iterated over, in sequence, and not to access individual records. They are not populated in the object.


DataMapper ORM v1.8.0 - El Forum - 02-16-2011

[eluser]NeoArc[/eluser]
Yeah, i noticed that : )

I think al3xandre is using get_iterated() instead of get(), that's why his script doesn't work.


DataMapper ORM v1.8.0 - El Forum - 02-16-2011

[eluser]dirkpostma[/eluser]
Hi all,

I'm trying out Datamapper and I have a question about it.

I have created two simple models: Post and Topic. A topic is a collection of posts. For performance reasons, I would like to cache some values in the topic record, e.g. the first_post_id, last_post_id and number_of_posts. I tried to do this as follows:

Code:
class Topic extends DataMapper {

    var $has_one = array(
      'first_post' => array('class' => 'post', 'other_field' => 'topic'),
      'last_post' => array('class' => 'post', 'other_field' => 'topic'),
  );

  var $has_many = array(
    'post' => array('class' => 'post', 'other_field' => 'topic')
  );
      
    var $validation = array(
        'title' => array('rules' => array('required', 'max_length' => 50, "my_update"), 'label' => 'Title'),
    'created' => array(),
    'updated' => array(),
    'posts' => array('label' => 'Posts', 'rules' => array('my_update')),
    'first_post' => array('label' => "First post"),
    'last_post' => array('label' => "Last post"),
    );


  function __construct($id = NULL)
    {
        parent::__construct($id);
  }

  function _my_update($field)
  {
    // Determine & save last_post and first_post based on their time
    echo "IN Topic:: _my_update<br>\n";
    
    // get_clone() is needed, otherwise $first_post is the same as $last_post
    $first_post = $this->posts->order_by('created')->limit(1)->get()->get_clone();
    $last_post = $this->posts->order_by('created', 'desc')->limit(1)->get()->get_clone();

    // TODO: find out why this doesn't work
    $result = $this->save(array('first_post' => $first_post,'last_post' => $last_post));
    
    if ($result) {
      echo "first- and last_post saved<br>\n";
    } else {
      echo "Error: [" . $this->error->string . "]<br>\n";
    }
  }
}

As the comments in the code make clear: this doesn't work. However, when I do it in the controller, it DOES work:


Code:
function test_datamapper()
  {
    $t = new Topic(1);
    $t->title = "Testtopic " . rand();

    if ($t->save()) {
      echo "Topic saved " .$t->id. "<br>\n";
    } else {
      echo "Error: " . $t->error->string . "<br>\n";
    }
    
    $p = new Post();
    $p->body = "This is a test " . rand();

    if ($p->save($t)) {
      echo "Post saved with related topic<br>\n";
    } else {
      echo $p->error->string;
    }

    $this->_update_topic($t);

  }
  
  function _update_topic($t)
  {
    // I would like to put this in model, but that doesn't work
    echo "IN Test_controller:: _update_topic<br>\n";
    
    $first_post = $t->posts->order_by('created')->limit(1)->get();
    $last_post = $t->posts->order_by('created', 'desc')->limit(1)->get();
    if ($t->save(array(
        'first_post' => $first_post,
        'last_post' => $last_post)
    )) {
      echo "topic updated!";
    } else {
      echo "Error:" . $t->error->string . "<br>\n";
    }
  }


I think this should not be done in the controller, but in the model, but, I can't get it working. Can anyone explain this? Thank you very much!