0
votes

i was building an clinic managment/patient electronic records project based on codeigniter, and i decided to go for an orm since i have lots of relations between my db.

so i downloaded datamapper 1.8.2 and started a new codeigniter project first thing i noticed was loading time !

the codeigniter welcome page is now loading in 1sec ++ ! without any logic yet ! is this normal ? and is it acceptable ? usually ci loads in 0.002 sec on my machine but with datamapper orm its 1.06 at its best !

secound question is about relations.

i have 2 tables for example

table 1 users . i use to store all my office ppl, and there role

example(users)

|ID|name|role   |
|1 |Demo|admin  |
|2 |Demo|doctor |
|3 |Demo|nurse  |
|4 |Demo|patient|

2nd table is visits

|id|doctor_id|patient_id|.....
|1 |2        |4         |......

so how can i setup this so that datamapper would understand that each user in users have a role, and that table visits has_one(doctors,patient) and users(doctors,patients) has_many(visits) ? since every visit must have only one doctor and patient, yet every patient/doctor can have many visits.

current setup trial

class Visit extends DataMapper {
    var $has_one = array(
        'doctor' => array(
            'class' => 'user',
            'join_self_as' => 'doctor_id',
            'other_field' => 'visit'
        ),
        'patient' => array(
            'class' => 'user',
            'join_self_as' => 'patient_id',
            'other_field' => 'visit'
        )
    );
    var $has_many = array();

Users

class User extends DataMapper {
    var $has_one = array();
    var $has_many = array(
        'visit' => array(
            'class' => 'visit',
            'other_field' => 'doctor_id',
            'join_self_as' => 'doctor_id',
        )
    );

but when i try to access a doctor visit by

$u=new user(2);
$u->visit->get();

it give me error

DataMapper Error: 'doctor_id' is not a valid parent relationship for Visit. Are your relationships configured correctly?

help ?!

2
Regarding the slow loading time: yes, an ORM will slow your system down. It is meant to make your job as a developer much easier to work with complex databases. You sacrifice loading time for faster development. If it's unbearable, consider using Active Record or upgrading your system. - Devin Young

2 Answers

1
votes

With or without an ORM, you as a developer always have to be aware of the tools you use, and in this case, the code you write to produce the queries fired at your DB. As queries go, Datamapper is more efficient then most, using joins instead of multiple queries where possible.

To see what queries are generated by Datamapper, enable CI's profiler. You will quickly see if your code is efficient, or if you run more queries then you have to.

Did you enable production cache? If not, Datamapper will run a DESCRIBE TABLE for every model you load, every page request. While this is ok in development (when your table structures still change), this is not a good idea in producction, and will probably account for a large part of that time.

0
votes

You're relationships aren't defined properly:

class Visit extends DataMapper {
    var $has_one = array(
        'doctor' => array(
            'class' => 'user',
            'join_self_as' => 'doctor',
        ),
        'patient' => array(
            'class' => 'user',
            'join_self_as' => 'patient',
        )
    );


class User extends DataMapper {
    var $has_many = array(
        'visit' => array(
            'join_other_as' => 'doctor',
        )
    );

You only have to override non-default values, Datamapper will fill in the rest. Also, do not use the '_id' suffix, it's mandatory, and Datamapper will add it automatically.

For Visit, you have to specify which FK is used in the relation to represent it's own 'id', which will be 'doctor_id' for the 'doctor' relation, and 'patient_id' for the 'patient' relation.

For User, you have to tell it the FK in the other table isn't 'visit_id' (which would be the default), but 'doctor_id'.