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.

DMZ 1.7.1 (DataMapper OverZealous Edition)

March 14, 2010 11:43pm

Subscribe [104]
  • #91 / Mar 24, 2010 1:37pm

    Atas

    45 posts

    Hello, DataMapper OverZealous Edition is great for me. But i have a question.
    Can i set the primary key field name?. I have a lot of tables that primary key field name is “xxx_id”. And i don’t want to change it to “id”.

    I want to do something like this:

    class Test extends DataMapper {
        
        var $table = 'testing';
        var $pk_filedname = 'test_id'; //<----
    
        function __construct($id = NULL)
        {
            parent::__construct($id);
        }
    }

    is this possible ?

    Thanks.

  • #92 / Mar 24, 2010 1:39pm

    OverZealous

    1030 posts

    @Atas

    No.  DMZ requires certain concessions in the database design.

    Also, this is clearly stated in the manual.

  • #93 / Mar 24, 2010 1:50pm

    Atas

    45 posts

    Ok,

    I am going to rename my Primary Keys…

    Thanks!

  • #94 / Mar 24, 2010 5:57pm

    Mareshal

    230 posts

    I have one question, and please don’t send me to read the manual.

    I have one parent table, and 4 other child tables. One field from my parent is present in the other 4 child tables. Is there any way I can do this: update only the main parent table and all child table will get updated automatically?

  • #95 / Mar 24, 2010 6:03pm

    OverZealous

    1030 posts

    @Mareshal
    No.  Write a custom method on the parent table’s model, or override the save function on the parent table:

    class Parent extends DataMapper {
        function save($object = '', $related_field = '') {
            $needs_update = ($this->shared_field === $this->stored->shared_field);
            $ret = parent::$save();
            if($need_update && $ret) {
                if($this->other_object->get()->exists()) {
                    $this->other_object->shared_field = $this->shared_field;
                    $this->other_object->save();
                }
                // repeat for each object
            }
            return $ret;
        }
    }

    You may need to add in special code to check for new objects.  This is just an example, so don’t take it as gospel.

  • #96 / Mar 25, 2010 12:38am

    Atas

    45 posts

    Hello, i have the following models and tables:

    CREATE TABLE `provincia` (
      `id` int(11) NOT NULL AUTO_INCREMENT,
      `nombre` varchar(255) DEFAULT NULL,
      PRIMARY KEY (`id`)
    ) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=latin1;

    class Provincia extends DataMapper {
        
        var $table = 'provincia';
        var $has_many = array("localidad");
        
        function __construct($id = NULL)
        {
            parent::__construct($id);
        }
        
    }

    CREATE TABLE `localidad` (
      `id` int(11) NOT NULL AUTO_INCREMENT,
      `nombre` varchar(255) DEFAULT NULL,
      `provincia_id` int(11) DEFAULT NULL,
      PRIMARY KEY (`id`)
    ) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=latin1;

    class Localidad extends DataMapper {
        
        var $table = 'localidad';
    
        function __construct($id = NULL)
        {
            parent::__construct($id);
        }
        
    }

    I am triying to get all “localidades” by “provincia” but an error message appears saying “DataMapper Error: ‘provincia’ is not a valid parent relationship for Localidad. Are your relationships configured correctly?”

    I don’t know what i am doing wrong.

    Provincia is State and Localidad is like City.

    Sorry my english, i am learning…

    Thanks…

  • #97 / Mar 25, 2010 12:40am

    OverZealous

    1030 posts

  • #98 / Mar 25, 2010 1:24am

    Atas

    45 posts

    Oh, sorry, it was in the troubleshooting. :red:

    I had to update mysql_driver.php too.

    Thanks.

  • #99 / Mar 25, 2010 9:43am

    NachoF

    171 posts

    Im a little confused on how should I get this done

    I have Projects which have many Stages and each stage has one Region, Function, and Organization.

    I want to save it all at once cause its all coming from the same form. When you create the project you also create the first stage for it

    if($project->save(array($stage, $region, $function, $organization)))
                        {
                                $this->session->set_flashdata('success', 'Project Created');
                                redirect("Welcome");
                        }

    I know this code wont work cause regions and functions arent related to projects but I just wanted to show it so that maybe someone could have an idea on how could i get this done

  • #100 / Mar 26, 2010 9:28am

    cahva

    662 posts

    I upgraded DMZ from 1.6.2 to 1.7.1.

    Just wanted to inform that if someone else has done the same.

    Before DMZ 1.7.x there was no built-in way to localize labels so I did it this way:

    class Customer Extends DataMapper {
        
        var $validation;
        var $has_one = array('customer','country');
    
        function __construct()
        {
            parent::__construct();
            $this->_set_validation();
        }
    
        function _set_validation()
        {
            $this->validation = array(
                'name' => array(
                    'label' => lang('account.name'),
                    'rules' => array('required','trim','min_lenght' => 3,'max_lenght' => 255)
                )
                
                ...
                
                'confirm_password' => array(
                    'label' => lang('account.confirm_password'),
                    'rules' => array('required', 'encrypt', 'matches' => 'password', 'min_length' => 3, 'max_length' => 40),
                    'type' => 'password'
                )
            );
        }
    }

    This way of doing it messes up the validation in the new version so be sure to update your models to the new localization system(which is great btw!).

    The problem with my previous approach was that if updating existing customer, the validation would fail because not defining confirm_password. For example:

    $customer = new Customer();
    $customer->get_by_name('cahva');
    $customer->name = 'Cahvaaaa';
    $customer->save();

    That failed with 1.7.1 but not the earlier 1.6.2 version..

    So if you are upgrading from earlier version and done something similar to get validation localized before, be sure to update your models.

    EDIT: Hmm.. Could it be that using parent::__construct() before the _set_validation could have caused this behaviour?? Maybe that parent::__construct() was not needed anyway.. I have probably left it there by accident.

  • #101 / Mar 26, 2010 2:49pm

    tomdelonge

    60 posts

    Quick question:

    In the past, when I needed nested categories, I would just have a categories table and a column with parent_id. If the parent id was 0, then it mean it didn’t have a parent.

    What’s a good way to achieve nested categories in DMZ? I imagine it has to do with self relationships. I’ve never needed a self relationship before, so can someone tell me: is it the right way to go for this particular problem?

    Just point me in a direction.

    Thanks.

  • #102 / Mar 26, 2010 4:49pm

    santiagofs

    2 posts

    I’m having a problem with the include_related function.
    It works ok on my local server but its not getting the related objects on the online server.
    Only difference I can see is the php version. I’m running v.5.2.5 locally and v.5.2.13 online.
    Does anyone having a similar issue?

    Thanks.

  • #103 / Mar 28, 2010 2:01am

    Henry Merriam

    1 posts

    Hello,

    I just upgraded to 1.7.1 and I’m glad to see the new localization functions. I have a feature suggestion to make this library even more powerful, but I’m not sure if my situation is too specific.

    For several of my models, some of the fields are enum types, and those correspond to lines in my language files. With the new localization features, I moved those lines into the auto-loaded model language files and created a proper naming convention for them.

    <?php
    // Language File for Transaction Model
    $lang['transaction_amount']     = 'Amount';
    $lang['transaction_account']     = 'Account';
    $lang['transaction_date']         = 'Date';
    $lang['transaction_type']         = 'Type';
    
    $lang['transaction_type_deposit'] = 'Deposit';
    $lang['transaction_type_transfer'] = 'Transfer';
    
    /* End of file model_transaction_lang.php */
    /* Location: ./application/language/english/model_transaction_lang.php */

    Then, in the model file, I created a function to retrieve those lines.

    public function localize_type() {
        return $this->localize_by_model('${model}_type_${field}', $this->type);
    }

    In my User model, I even gave the function some static functionality (although I haven’t written it to work as a truly static function yet).

    public function localize_status($status = null) {
        return $this->localize_by_model('${model}_status_${field}', ($status ? $status : $this->status));
    }

    In the case of that function, if no parameter is given, it defaults to the object instance’s field value.

    Could something like this be implemented as a magic function? For example, a function localize_{field}() which would return the line {model}_{field}_{value}, where {value} is the value of the specified field. This would make producing localized strings of enumerations incredibly easy.

    I’m having a great time with DMZ, so thank you for the excellent work. 😊

    Henry Merriam

  • #104 / Mar 28, 2010 10:24am

    Cord

    6 posts

    Hi guys,

    I’m very new to DMZ but as a developer, I am really loving working with it. The classes are well thought out and superbly implemented. Good job Phil!

    I’m trying to doing something a little unusual and am not sure if I am going to be able to achieve my goal within the limitations of the framework. I have a database (call it db_system) with two tables: users and databases. There is a many-to-many relationship between them, databases_users, which defines which databases a user has access to.

    By default, CI is configured to automatically connect to the db_system database. When a user logs in to the main website and is authenticated against db_system, I want to fetch the list of other databases that the user can connect to, and then dynamically connect to each one and extract the information that is relevant to that user. Obviously, I could put all the information currently in the separate databases into a single database and reference each user’s data by user_id—but my client wants to keep them all separate because the parts of his business are discrete and he wants to be able to move any database onto a new database server with a minimum amount of fuss.

    Let me explain by way of an example and hope that it’s not too confusing. Assume that we have a db_system database, and two other databases: db_cars and db_houses. The structures look something like this:

    db_system:
      databases
        database_id int unsigned auto_increment
        host varchar(60)
        schema varchar(40)
        username varchar(40)
        password varchar(30)
        dbdriver varchar(10)
        dbprefix varchar(20)
        pconnect bit(1)
        db_debug bit(1)
        cache_on bit(1)
        cachedir varchar(255)
        char_set varchar(10)
        dbcollat varchar(20)

      users
        user_id bigint unsigned auto_increment
        username varchar(64)
        password varchar(40)

      databases_users
        database_id int unsigned
        user_id bigint unsigned

    db_cars:
      sales
        sale_id bigint unsigned auto_increment
        user_id bigint unsigned
        item_id int unsigned
        ...

      purchases
        purchase_id bigint unsigned auto_increment
        user_id bigint unsigned
        item_id int unsigned
        ...

    db_houses:
      sales
        sale_id bigint unsigned auto_increment
        user_id bigint unsigned
        item_id int unsigned
        ...

      purchases
        purchase_id bigint unsigned auto_increment
        user_id bigint unsigned
        item_id int unsigned
        ...

    The example above is a little contrived but I think you should be able to see what I’m trying to do. A user can belong to one or more databases, and can have different information in each. The databases can reside on different servers and I want to consolidate each user’s information by dynamically connecting to each database and displaying that on a single page.

    I know I can use $db_params in each of the data-mapped classes but I assume that DMZ automatically connects to the database when the data-mapped object is created. I want to be able to do something like this:

    $u = new User();
    $u->where('username', 'is_some_user')->get();
    
    $u->database->get();
    
    foreach ($u->database->all as $d)
    {
      // Connect to the selected database
      $user_d = some_dmz_db_connect(
                  $d->host,
                  $d->schema,
                  $d->username,
                  $d->password,
                  $d->dbdriver,
                  $d->dbprefix,
                  $d->pconnect,
                  $d->db_debug,
                  $d->cache_on,
                  $d->cachedir,
                  $d->char_set,
                  $d->dbcollat
                );
    
      // Now do some stuff with a user's connection
      $s = new $user_d->Sales()
      $s->where('user_id', $u->user_id)->get();
      ...
    }

    Does the above make sense? Is it possible to achieve what I’m trying to do or am I just barking up the wrong tree?

    Thanks,
    Cord

  • #105 / Mar 28, 2010 10:52am

    Cord

    6 posts

    I have another question relating to what I think would be a deep relationship in DMZ. In my previous question I mentioned a database, db_system. I would like to change the structure of the database slightly such that each databases_users row can below to a group. This would mean that, for example, user X can have access to database Y and Z but with different permission levels. The database structure would look like this:

    db_system:
      databases
        database_id int unsigned auto_increment
        host varchar(60)
        schema varchar(40)
        username varchar(40)
        password varchar(30)
        dbdriver varchar(10)
        dbprefix varchar(20)
        pconnect bit(1)
        db_debug bit(1)
        cache_on bit(1)
        cachedir varchar(255)
        char_set varchar(10)
        dbcollat varchar(20)

      users
        user_id bigint unsigned auto_increment
        username varchar(64)
        password varchar(40)

      groups
        group_id smallint unsigned auto_increment
        description varchar(64)
        access_level int

      databases_users
        database_id int unsigned
        user_id bigint unsigned
        group_id smallint unsigned

    How would I represent this relationship in the DMZ models? Clearly I would need a Databases and a Users model, and each would have a $has_many relationship with the other. But how do I add the relationship with groups, and then reference and use the `databases_users.group_id` relationship in a meaningful way?

    Thanks again,
    Cord

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

ExpressionEngine News!

#eecms, #events, #releases