ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

DataMapper ORM v1.8.2

November 30, 2011 3:43pm

Subscribe [100]
  • #196 / Feb 29, 2012 8:56am

    WanWizard

    4475 posts

    How do I make equipments the child (relationship) of customers?

    That depends.

    If it is a many-to-many: use a relationship table with FK’s to both tables
    If it is a one-to-many: either use a relationship table, or store the FK in the ‘one’ table
    If it is a one-to-one: either use a relationship table, or store the FK in one of the the ‘one’ tables

    As for cascading deletes, the logic should be the following, assuming this:

    /*
     * Customer has a many-to-one relation with Order. One customer has many orders, an
     * order always belongs to a single customer
     */
    
    // get customer with id 10
    $customer = new Customer(10);
    
    // and delete it.
    $customer->delete();

    In this setup, the Order table contains the FK ‘customer_id’.

    When deleting the customer record, Datamapper will fetch the relation definition from the Customer model, and it will check if ‘cascading_delete’ is set to TRUE on the relation (which it is by default).

    If so, it will check if there is a relationship table. If there is, it will delete the records from that table, but will not delete any Order records.

    If there is no relationship table, it will check if Customer or Order contains the FK that defines the relation.

    If the FK is in the other table, and the other table is equal to the current objects table (only true in a self-referencing relation) and the FK is in the current table, the FK will be set to NULL, and nothing will happen to the other table.

    In all other cases, the related record will be deleted.

    After this process, the current record itself will be deleted. If you’re using a DB capable of transactions, this entire process is wrapped in a transaction to ensure integrity.

    If that doesn’t happen, then you have just found a bug.

  • #197 / Feb 29, 2012 10:08pm

    Luis Pacheco

    2 posts

    Hi, I can’t use CI libraries in my Datamapper model, neither autoloading or local load works, I get the error “Call to a member function encode() on a non-object in ...” (the encrypt library in this case).
    Is it possible to use CI libraries in Datamapper models?

    Thanks in advance!

  • #198 / Mar 01, 2012 3:20am

    Maglok

    402 posts

    Since datamapper models are effectively extending a library, you need to get the instance of the CI object I believe.

    $CI =& get_instance();

    Then reference CI calls with $CI-> like $CI->load->view(‘test’);

  • #199 / Mar 02, 2012 6:42pm

    toadies

    8 posts

    In this setup, the Order table contains the FK ‘customer_id’.

    When deleting the customer record, Datamapper will fetch the relation definition from the Customer model, and it will check if ‘cascading_delete’ is set to TRUE on the relation (which it is by default).

    My two tables is customers and equipments has one-to-one relations.
    In MySQL I have a FK in the equipment table with customer_id as the FK to id in customers table.  On Delete is set to Cascade in equipments.

    cascade_delete is set to true.

    If so, it will check if there is a relationship table. If there is, it will delete the records from that table, but will not delete any Order records.

    If there is no relationship table, it will check if Customer or Order contains the FK that defines the relation.

    I have no relationship table.

    If the FK is in the other table, and the other table is equal to the current objects table (only true in a self-referencing relation) and the FK is in the current table, the FK will be set to NULL, and nothing will happen to the other table.

    In all other cases, the related record will be deleted.

    After this process, the current record itself will be deleted. If you’re using a DB capable of transactions, this entire process is wrapped in a transaction to ensure integrity.

    If that doesn’t happen, then you have just found a bug.

    When I try to delete here is my warning message from my script.

    A Database Error Occurred
    
    Error Number: 1452
    
    Cannot add or update a child row: a foreign key constraint fails (`pools_dev`.`equipments`, CONSTRAINT `PK_Customers` FOREIGN KEY (`customer_id`) REFERENCES `customers` (`id`) ON DELETE CASCADE)
    
    UPDATE `equipments` SET `customer_id` = NULL WHERE `customer_id` = 5

    The script I don’t think is even trying to delete the equipment relationship.  Only try to update it to Null.

    Is this a bug or am I doing something wrong?

    **Edit: I resolved by setting cascade_delete to FALSE and letting mysql handling the relationship.

  • #200 / Mar 03, 2012 8:06am

    WanWizard

    4475 posts

    If you use constraints and ON CASCADE DELETE, you should set ‘cascade_delete’ to FALSE as they don’t work together.

    This is documented in the manual.

    If it tries to set it to NULL instead of deleting it, that would be a bug. I’ll have a look.

  • #201 / Mar 03, 2012 3:20pm

    Luis Pacheco

    2 posts

    Since datamapper models are effectively extending a library, you need to get the instance of the CI object I believe.

    $CI =& get_instance();

    Then reference CI calls with $CI-> like $CI->load->view(‘test’);

    Thank you very much!

  • #202 / Mar 14, 2012 5:24am

    Maglok

    402 posts

    Been finding out a lot about datamapper, the more I use it the more I like it.

    Here is one I can’t figure out, and am not sure if I understand the docs right.

    Lets assume that a Person has Hobbies.

    So Person has_many Hobby and Hobby has_many Person.

    In the join table between the two, is the amount of years the Person is doing that Hobby.

    I know how to get all the hobbies the person has, like so:

    $hobbies->include_join_fields()->where_related('person', 'id', $person)->get();

    My ‘problem’ is the following: If I want to print a list of all hobbies in the system and how many years a person is doing them (so including hobbies the person does not practice) how would I go about it?

    I would need the join field ‘years_active’, but I do not want to limit the query to just the hobbies that have a relation. Does datamapper have any way to do that? Or should I start pasting result sets together?

    Greets!

  • #203 / Mar 14, 2012 8:53am

    tarciozemel

    82 posts

    Using MATCH…AGAINST in ->where()

    Hi, folks!

    I 1.8.1 version, I was using

    ...
    $companies->where('MATCH (foo) AGAINST ("bar" IN BOOLEAN MODE)');
    ...

    and everything was running fine.

    But, when I upgraded to 1.8.2, now I get an error. When I check the query, seems like DM is put quots in MATCH word.

    ... AND `MATCH` (foo) AGAINST ("bar" IN BOOLEAN MODE) ...

    Any ideas about how avoid this?

    Regards!

  • #204 / Mar 14, 2012 2:15pm

    Ikkes

    3 posts

    Hello,

    I ran into some error that i can’t seem to solve. Not even with Google 😊

    What I want is the following:
    I have 2 databases. 1 database has user data and the other has the website data. I want to authenticate with the user database. Below you can find my database.php config file and my auth.php model file.

    database.php

    $active_group = 'default';
    $active_record = TRUE;
    
    $db['default']['hostname'] = 'localhost';
    $db['default']['username'] = 'database_user';
    $db['default']['password'] = '*****';
    $db['default']['database'] = 'website_database';
    $db['default']['dbdriver'] = 'mysql';
    $db['default']['dbprefix'] = '';
    $db['default']['pconnect'] = TRUE;
    $db['default']['db_debug'] = TRUE;
    $db['default']['cache_on'] = FALSE;
    $db['default']['cachedir'] = '';
    $db['default']['char_set'] = 'utf8';
    $db['default']['dbcollat'] = 'utf8_general_ci';
    $db['default']['swap_pre'] = '';
    $db['default']['autoinit'] = TRUE;
    $db['default']['stricton'] = FALSE;
    
    $db['users']['hostname'] = 'localhost';
    $db['users']['username'] = 'database_user';
    $db['users']['password'] = '*****';
    $db['users']['database'] = 'user_database';
    $db['users']['dbdriver'] = 'mysql';
    $db['users']['dbprefix'] = '';
    $db['users']['pconnect'] = TRUE;
    $db['users']['db_debug'] = TRUE;
    $db['users']['cache_on'] = FALSE;
    $db['users']['cachedir'] = '';
    $db['users']['char_set'] = 'utf8';
    $db['users']['dbcollat'] = 'utf8_general_ci';
    $db['users']['swap_pre'] = '';
    $db['users']['autoinit'] = TRUE;
    $db['users']['stricton'] = FALSE;

    auth.php

    class Auth extends DataMapper {
    
     var $table = 'members';
     var $db_params = 'users';
    }

    The problem I have is that I get the following database error:

    Error Number: 1146
    
    Table 'website_database.members' doesn't exist
    
    DESCRIBE `members`
    
    Filename: /bladiebladiebla/libraries/datamapper.php
    
    Line Number: 645

    It seems that CodeIgniter/DataMapper uses the default settings but in the model I’ve overwritten the db params with the users database.

    My question is: What am I doing wrong?

    With kind regards,
    Dennis

  • #205 / Mar 15, 2012 5:29pm

    North2Alaska

    58 posts

    I’m just starting to use DM.  I’ve created a simple model to query a table, but I want to add a prefix.  My tables have multiple prefixes and I will have to define the prefix for each table/class.

    class User extends DataMapper {

    var $prefix = “auth_”;

    My problem is the prefix is not honored. 

    A Database Error Occurred
    Error Number: 1146
    Table ‘publisher.users’ doesn’t exist
    DESCRIBE `users`
    Filename: /Library/WebServer/Documents/ci/libraries/Datamapper.php
    Line Number: 645

    I can get things to work for this simple test if I set the prefix in the config file, but obviously won’t work for the other prefixes.  I’m sure I’m doing something simple, but just can see it.  Any ideas?

  • #206 / Mar 15, 2012 7:40pm

    WanWizard

    4475 posts

    @Maglok,

    Datamapper is about relations, it can’t produce data in a relation that is not related. 😉

    The best option is probably to retrieve all hobbies, and retrieve the person including it’s related hobbies. Then iterate over the hobbies, and check if the id exists as a key in persons->hobbies->all. If so, that hobby is related, and if not, well…

  • #207 / Mar 15, 2012 7:43pm

    WanWizard

    4475 posts

    @tarciozemel,

    Datamapper passes where() clauses unaltered to $this->db->where(). Maybe something changed in CI’s DB drivers?

    You can try

    ...
    $companies->where('MATCH (foo) AGAINST ("bar" IN BOOLEAN MODE)', NULL, FALSE);
    ...

    which will disable escaping…

  • #208 / Mar 15, 2012 7:47pm

    WanWizard

    4475 posts

    @Ikkes,

    Which version of Datamapper? I have a comment line on Line Number: 645.

  • #209 / Mar 15, 2012 7:52pm

    WanWizard

    4475 posts

    @North2Alaska,

    That sounds like the same (kind of) issue as @Ikkes.

    Can you (both?) add a method to your model called post_model_init(), and in that method do a

    var_dump(get_object_vars($this);

    to make sure that your class properties aren’t overwritten for some reason…

  • #210 / Mar 16, 2012 6:13am

    Ikkes

    3 posts

    @WanWizard

    The version I use is 1.8.2.

    On line 645 in application/libraries/datamapper.php I have the following text:

    // Get and store the table's field names and meta data
    $fields = $this->db->field_data($this->table);

    With kind regards,
    Dennis

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases