CodeIgniter Forums
DataMapper ORM v1.8.2 - 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.2 (/showthread.php?tid=47243)



DataMapper ORM v1.8.2 - El Forum - 11-10-2012

[eluser]Crackz0r[/eluser]
WanWizard, i have a doubt, i know any ORM make slow any aplication... But i want to know if there is a big difference between using CI ActiveRecord and using DM? and if there is possible to mix CI AR in DM Models? i.e. to make simple queries.


DataMapper ORM v1.8.2 - El Forum - 11-10-2012

[eluser]WanWizard[/eluser]
Any extra layer costs performance, and an ORM is no exception.

Whether or not that is an issue, depends on your situation. My company creates (large) business applications, the hourly rate of the developers by far outweighs the extra cost of a more beefy server. If you work for free, then ymmv.

Datamapper is quite fast, the biggest overhead is instantiating all objects. So using an ORM for batch operations is a bad idea. Also, use PHP 5.4, since it's a lot faster when using objects.

Mixing AR and DM isn't a problem, DM uses AR underneath. You can even convert an AR resultset into DM objects if you want.


DataMapper ORM v1.8.2 - El Forum - 11-14-2012

[eluser]bigspring[/eluser]
Hi Wanwizard,

I've been playing with datamapper for a little while now and I'm really loving how easy it makes things. I had my own core model that handled some of this before, but DM ORM is much more powerful. All credit to you!

I have a question - I have some fairly complex relationships in my system. Part of the system is based around a decision tree. So a tree has many questions, and question has many answers, plus others I won't bother explaining.

I'm adding a concept of revisions into the system, so now each tree / question / answer also has one revision. When I create a new revision, I need to duplicate the data for each of the entities and update the revision to the latest revision id so we have a complete history of all entities.

What I'm not sure about is how to handle the relationships. The code below uses get_copy on each entity, updates the revision id and save it again but obviously this won't account for any relationships.

Code:
$trees = new tree();
   $trees = $trees->where('revision_id = ' . $revision_id)->get();
  
   foreach($trees AS $tree)
   {
    $new_tree = $tree->get_copy();
    $new_tree->revision_id =  $revision->id;
    $new_tree->save();
   }
  
   $questions = new question();
   $questions = $questions->where('revision_id = ' . $revision_id)->get();

   foreach($questions AS $question)
   {
    $new_question = $question->get_copy();
    $new_question->revision_id =  $revision->id;
    $new_question->save();
   }

Do you have any suggestions on how best to handle this?


DataMapper ORM v1.8.2 - El Forum - 11-15-2012

[eluser]WanWizard[/eluser]
Following normal normalisation rules, I would probably split the record into question and revision, using a one-to-many.

It would keep the rest of the system, up to question, the same, and you can easily get the latest revision of the question using either the id or a timestamp by running a query with a DESC order, LIMIT 1.

For easy access you could store the last revision id in the question record, and use two relations: one for the one-to-many to get all revisions of a question, and a one-to-one using that last revision id as FK to directly fetch the last revision of a question.

I don't think there should be a revision in the tree, as it's a property of the question.


DataMapper ORM v1.8.2 - El Forum - 11-15-2012

[eluser]bigspring[/eluser]
Thanks for the advice.

The reason I've done it the way that I have is that all trees / questions / answers (and also products, and product attribute values that I didn't explain before) should have a full revision history so the admins can press a button and restore all of the values for those entities back to their previous state. To my mind that means we need to store a revision ID for all of entities - what do you think?

What I was really looking for is a way to handle the copying of the entities and their relationships. Is there a good way of doing that with DM ORM?


DataMapper ORM v1.8.2 - El Forum - 11-15-2012

[eluser]phobeous[/eluser]
Hello WanWizard and congrats for DataMapper (it has speeded up my work). I have a question that maybe is answered in v2.0 input request, but want to clear any doubt. Let me explain: all my model objects extend DataMapper class and all of them share a set of methods. The question is obvious: I want to write those methods just once, is there any way to do this?

I've tried:
Code:
class MY_DataMapper extends DataMapper
{
   public function my_shared_method()
   {
      // ...method stuff...
   }
}

Then:
Code:
class Model_1 extends MY_DataMapper
{
   // ...model stuff...
}

But error: "PHP Fatal error: Class 'My_DataMapper' not found" appears.

Thanks in advance for any response.


DataMapper ORM v1.8.2 - El Forum - 11-15-2012

[eluser]WanWizard[/eluser]
Where did you create MY_Datamapper? It should be library, not a model. If so, it should work fine, as CI deals with loading your extension.

But it's better not to extend the Datamapper library at all, but instead work with a base model:

Code:
// ../application/models/model_base.php
class Model_Base extends Datamapper
{
    // ...generic model stuff...
}

and

Code:
// ../application/models/model_i.php
class Model_I extends Model_Base
{
    // ...local model stuff...
}



DataMapper ORM v1.8.2 - El Forum - 11-15-2012

[eluser]phobeous[/eluser]
It's fine Harro, I had done exactly as you wrote, but name convetion was responsible for non-functioning. Actuallly my Model_Base was LB_Entity, stored in application/models/lb_entity.php

Renaming to Lb_Entity was enough.

Sorry for disturbing, as it would have worked from the begining if I had considered class naming convention.

Thanks for your time.


DataMapper ORM v1.8.2 - El Forum - 11-22-2012

[eluser]PoetaWD[/eluser]
Hello,

Is there a way to order by a related table ???

like:

Code:
$obj = new Product
$obj->include_related('brand','name');
$obj->order_by('brand_name','desc');
$obj->get();

If not, is it possible using MYSQL query ???

Thanks


DataMapper ORM v1.8.2 - El Forum - 11-23-2012

[eluser]smi23[/eluser]
[quote author="PoetaWD" date="1353628771"]Hello,

Is there a way to order by a related table ???

like:

Code:
$obj = new Product
$obj->include_related('brand','name');
$obj->order_by('brand_name','desc');
$obj->get();

If not, is it possible using MYSQL query ???

Thanks[/quote]
Code:
$obj->order_by_related('brand','name','desc');

See http://datamapper.wanwizard.eu/pages/getadvanced.html#_related_model