EllisLab text mark
Advanced Search
54 of 63
54
   
DataMapper ORM v1.8.2
Posted: 05 November 2012 03:22 PM   [ Ignore ]   [ # 801 ]   [ Rating: 0 ]
Avatar
Joined: 2008-11-04
4404 posts

No, that’s not what I meant.

You currently have: Person -> has_many -> (via join_person_car) <- has_many <- Car (a many-many between Car and Person).

If Wheel is something specific about the combination of a car and a person, from a normalisation point of view it should be related to the join table, and not to either Person or Car.

This means you have to make a model for the join table (let’s call that PersonCar), otherwise you can’t relate something to it.

You now get:
- Person -> has_many - <- has_one <- PersonCar
- Car -> has_many - <- has_one <- PersonCar

This will still be a many to many between Person and Car. If you define a custom tablename of ‘join_person_car’ for the PersonCar model, you don’t even have to change the many to many for direct access via Person or Car.

Now that you have this, you can relate other models of PersonCar:
- PersonCar -> has_one -> Wheel (which has wheel_id in PersonCar)

 Signature 

Me: WanWizard.eu | My company: Exite | Datamapper: DataMapper ORM

 
Posted: 06 November 2012 12:09 AM   [ Ignore ]   [ # 802 ]   [ Rating: 0 ]
Joined: 2011-05-18
15 posts
WanWizard - 04 November 2012 06:58 PM

No guarantees, I only don’t make any promisses towards maintaining it.

I don’t write (new) CI applications anymore, and I haven’t used the extension in years. But the DM code is very stable, and as CI itself hardly changes, there isn’t much to maintain, and I don’t see it breaking any time soon. Unless CI 3 is going to “pull a kohana”, but I don’t think so…

:( But i´m not very clear… you will keep DM ORM mantained?

Abot what you said… you dont prefer CI anymore, what do you recommend us then?

 
Posted: 06 November 2012 03:46 AM   [ Ignore ]   [ # 803 ]   [ Rating: 0 ]
Avatar
Joined: 2008-11-04
4404 posts

Yes, I will.

I can’t recommend anything. You choose the right tool for the job. For me, that wasn’t CI anymore.

 Signature 

Me: WanWizard.eu | My company: Exite | Datamapper: DataMapper ORM

 
Posted: 09 November 2012 03:07 AM   [ Ignore ]   [ # 804 ]   [ Rating: 0 ]
Joined: 2012-08-13
26 posts

Hello, I’m using the login example listed on the datamapper website. What I’m trying to build now is a function to allow users to change their password. I want the user to input their current password before allowing a new password to be saved. Simply saving a new password for the user works fine:

$u->password $this->input->post('newpassword')
$u->save(); 

how would I go about doing this:

if ($u->password == $this->input->post('currentpassword')
{
$u
->password $this->input->post('newpassword')
$u->save();

obviously the above will not work because it’s comparing a plaintext password against something already encrypted… thoughts?

 
Posted: 09 November 2012 03:30 AM   [ Ignore ]   [ # 805 ]   [ Rating: 0 ]
Avatar
Joined: 2009-06-12
420 posts

Encrypt it before comparing it. I can’t recall the encryption the example uses, but something like this:

if($u->password == encrypt($this->input->post('currentpassword')) 
 Signature 

- Simon

 
Posted: 09 November 2012 08:21 AM   [ Ignore ]   [ # 806 ]   [ Rating: 0 ]
Avatar
Joined: 2008-11-04
4404 posts

The model in the example is using a validation rule to encrypt the password, using the function _encrypt() in the model.

The login() method in the model shows you how a salt is determined and how a password entered by the user is encrypted so it can be compared to a stored password.

 Signature 

Me: WanWizard.eu | My company: Exite | Datamapper: DataMapper ORM

 
Posted: 10 November 2012 01:19 PM   [ Ignore ]   [ # 807 ]   [ Rating: 0 ]
Joined: 2011-05-18
15 posts

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.

 
Posted: 10 November 2012 02:03 PM   [ Ignore ]   [ # 808 ]   [ Rating: 0 ]
Avatar
Joined: 2008-11-04
4404 posts

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.

 Signature 

Me: WanWizard.eu | My company: Exite | Datamapper: DataMapper ORM

 
Posted: 14 November 2012 01:38 PM   [ Ignore ]   [ # 809 ]   [ Rating: 0 ]
Joined: 2011-01-27
6 posts

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.

$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?

 
Posted: 15 November 2012 05:44 AM   [ Ignore ]   [ # 810 ]   [ Rating: 0 ]
Avatar
Joined: 2008-11-04
4404 posts

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.

 Signature 

Me: WanWizard.eu | My company: Exite | Datamapper: DataMapper ORM

 
Posted: 15 November 2012 05:51 AM   [ Ignore ]   [ # 811 ]   [ Rating: 0 ]
Joined: 2011-01-27
6 posts

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?

 
Posted: 15 November 2012 06:40 AM   [ Ignore ]   [ # 812 ]   [ Rating: 0 ]
Joined: 2012-10-31
5 posts

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:

class MY_DataMapper extends DataMapper
{
   
public function my_shared_method()
   
{
      
// ...method stuff...
   
}

Then:

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.

 
Posted: 15 November 2012 07:44 AM   [ Ignore ]   [ # 813 ]   [ Rating: 0 ]
Avatar
Joined: 2008-11-04
4404 posts

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:

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

and

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

 

 Signature 

Me: WanWizard.eu | My company: Exite | Datamapper: DataMapper ORM

 
Posted: 15 November 2012 08:26 AM   [ Ignore ]   [ # 814 ]   [ Rating: 0 ]
Joined: 2012-10-31
5 posts

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.

 
Posted: 22 November 2012 07:59 PM   [ Ignore ]   [ # 815 ]   [ Rating: 0 ]
Avatar
Joined: 2009-03-19
103 posts

Hello,

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

like:

$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

 
54 of 63
54