EllisLab text mark
Advanced Search
7 of 62
7
   
DMZ 1.7.1 (DataMapper OverZealous Edition)
Posted: 24 March 2010 01:39 PM   [ # 91 ]   [ Rating: 0 ]
Avatar
Joined: 2008-10-08
1039 posts

@Atas

No.  DMZ requires certain concessions in the database design.

Also, this is clearly stated in the manual.

 Signature 

Phil DeJarnett
  OverZealous Creations, LLC

 
Posted: 24 March 2010 01:50 PM   [ # 92 ]   [ Rating: 0 ]
Avatar
Joined: 2008-03-04
59 posts

Ok,

I am going to rename my Primary Keys…

Thanks!

 Signature 

?

 
Posted: 24 March 2010 05:57 PM   [ # 93 ]   [ Rating: 0 ]
Joined: 2009-06-18
279 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?

 
Posted: 24 March 2010 06:03 PM   [ # 94 ]   [ Rating: 0 ]
Avatar
Joined: 2008-10-08
1039 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.

 Signature 

Phil DeJarnett
  OverZealous Creations, LLC

 
Posted: 25 March 2010 12:38 AM   [ # 95 ]   [ Rating: 0 ]
Avatar
Joined: 2008-03-04
59 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…

 Signature 

?

 
Posted: 25 March 2010 12:40 AM   [ # 96 ]   [ Rating: 0 ]
Avatar
Joined: 2008-10-08
1039 posts

See item #4 in the Troubleshooting guide.

 Signature 

Phil DeJarnett
  OverZealous Creations, LLC

 
Posted: 25 March 2010 01:24 AM   [ # 97 ]   [ Rating: 0 ]
Avatar
Joined: 2008-03-04
59 posts

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

I had to update mysql_driver.php too.

Thanks.

 Signature 

?

 
Posted: 25 March 2010 09:43 AM   [ # 98 ]   [ Rating: 0 ]
Joined: 2009-04-02
200 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

 
Posted: 26 March 2010 09:28 AM   [ # 99 ]   [ Rating: 0 ]
Avatar
Joined: 2008-07-16
664 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.

 
Posted: 26 March 2010 02:49 PM   [ # 100 ]   [ Rating: 0 ]
Avatar
Joined: 2009-06-13
94 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.

 
Posted: 26 March 2010 04:49 PM   [ # 101 ]   [ Rating: 0 ]
Joined: 2009-06-12
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.

 
Posted: 28 March 2010 02:01 AM   [ # 102 ]   [ Rating: 0 ]
Joined: 2010-03-28
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

 
Posted: 28 March 2010 10:24 AM   [ # 103 ]   [ Rating: 0 ]
Joined: 2009-10-05
8 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

 
Posted: 28 March 2010 10:52 AM   [ # 104 ]   [ Rating: 0 ]
Joined: 2009-10-05
8 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

 
Posted: 29 March 2010 12:14 AM   [ # 105 ]   [ Rating: 0 ]
Avatar
Joined: 2008-10-08
1039 posts
NachoF - 25 March 2010 01:43 PM

I want to save it all at once cause its all coming from the same form.

You have to save each new object one-by-one.  You also have to save relationships on the object as they exist.

However, if you wrap them all in a transaction, you can rollback the saves if something goes wrong.  (Even when DMZ saves multiple relationships, it doesn’t automatically wrap them in a transaction — if something failed, it would not automatically roll back the first items saved.)

$project->trans_begin();
// if new: save $region, $function, or $organization, too
$success $stage->save(array($region$function$organization));
if(
$success{
    $success 
$project->save($stage);
}
if($success{
    $project
->trans_commit();
    
$this->session->set_flashdata('success''Project Created');
    
redirect("Welcome");
else {
    $project
->trans_rollback();
    
// show error, etc.

* Note: you need a transactionable table type if you are using MySQL.

 Signature 

Phil DeJarnett
  OverZealous Creations, LLC

 
7 of 62
7