Welcome Guest, Not a member yet? Register   Sign In
[Deprecated] DMZ 1.5.3 (DataMapper OverZealous Edition)
#81

[eluser]OverZealous[/eluser]
Hi cube1893, welcome to CodeIgniter and DMZ!

How much information is unique between those models? What I'm thinking is that you might decide to merge the three different vehicle types into one vehicles table, and then have a column called type, which you can store, for example, 'c', 'b', 'm' to distinguish between types.

Then it would be simple to query, say, bikes like this:
Code:
$v = new Vehicle();
$v->where('type', 'b')->get();

If you wanted all users who have cars, you could try this:
Code:
$u = new User();
$u->where_related_vehicle('type', 'c')->get();

Another benefit of this is that you could get a merged query of multiple vehicle types at once.

However, if those three tables are very unique, meaning they don't share much information, this might be inefficient. In that case, you might want to simply include a column (car_id, bicycle_id, motorbike_id) for each vehicle type, and then each type will be represented by a unique class (Car, Bicycle, Motorbike), and have a unique relationship to User.

The draw back here is that you now have 2 NULL columns per user (although those usually don't take up much room), and you have to manually ensure that each User only has one vehicle, by clearing all three vehicle ids before you save a new vehicle.

Finally, a third option, which isn't much better than the last one, would be to have a unique relationship table for all three types (cars_users, bicycles_users, motorbikes_users). This usually makes your database messy, and is a little harder to manage than the in-table relationships. However, no NULL columns.

That's about all of the options I can see easily.
#82

[eluser]cube1893[/eluser]
Hi Phil,

thanks for your reply. The Bicycle and the Motorbike are quite similar, whereas the Car has a completely different structure. That's why your suggestion with 3 columns (car_id, bicycle_id and motorbike_id) suits me best.
Thanks a lot!
I have a nother quick one: I have 2 tables continents (id, abbreviation) and countries (id, continent_id, name), where a continent has many countries.
With a given continent abbreviation, what's the fastest way to get the corresponding countries for that continent?

Cheers,
cube1893
#83

[eluser]NachoF[/eluser]
[quote author="cube1893" date="1242151563"]
With a given continent abbreviation, what's the fastest way to get the corresponding countries for that continent?

Cheers,
cube1893[/quote]

Code:
$continent=new Continent();
$continent->get_by_abbreviation('your_abbreviation_here'); //assuming there is just one continent with that abbreviation
$c=$continent->country->get();
foreach($c->all as $country)
{
    echo $country->name; //or whatever you want to do with each related country.
}

Is that right, Phil? Smile
#84

[eluser]cube1893[/eluser]
Awesome, that worked, thx a lot! I have to read the manuel, I really love that module!
Cya!
cube
#85

[eluser]OverZealous[/eluser]
@NachoF
Exactly correct!
#86

[eluser]Daniel H[/eluser]
I'm experimenting with the multiple relationships on the same object feature, and I'm having difficulty with something.

An account can have a creator (user) an editor (user) but also many users (i.e. normal users). I have my model set up such that an account can have one editor and creator - which works fine - but then I also have it set up such that it can have many users.

I take it that the user object appearing in the has_one and has_many arrays is throwing DMZ, since I can't seem to create 'normal' relationships between account and user, using - for example -

Code:
$account->save(array('creator'=>$user1, 'editor'=>$user2, $user3, $user4));

The creator and editor links are made in account_user table okay, but the other two users don't appear.

Any ideas?
#87

[eluser]OverZealous[/eluser]
What do your $has_many and $has_one arrays look like on both User and Account?
#88

[eluser]Daniel H[/eluser]
Wow - way too quick Phil - was just coming back to say I screwed up my user model, so works perfectly! Sorry!
#89

[eluser]warrennz[/eluser]
Any chance you could post your code anyway? I tried getting similar idea working a while ago but failed Tongue
#90

[eluser]Daniel H[/eluser]
Sure... Account model:

Code:
var $has_one = array('country', 'theme', 'style',
    
                            'creator' => array(
                                'class' => 'user',
                                'other_field' => 'created_account'),
                            
                            'editor' => array(
                                'class' => 'user',
                                'other_field' => 'edited_account'),
    
                        );
                        
    var $has_many = array('user', 'image', 'client', 'page');


User model:

Code:
var $has_many = array('account',
    
        'edited_account' => array(
            'class' => 'account',
            'other_field' => 'editor'
        ),
        'created_account' => array(
            'class' => 'account',
            'other_field' => 'creator'
        )
        
    );
    
    var $has_one = array();


And then my db table:

Code:
CREATE TABLE `accounts_users`
(
`id` INTEGER unsigned  AUTO_INCREMENT,
`user_id` INTEGER unsigned,
`account_id` INTEGER unsigned,
`creator_id` INTEGER unsigned,
`created_account_id` INTEGER unsigned,
`editor_id` INTEGER unsigned,
`edited_account_id` INTEGER unsigned,
PRIMARY KEY (`id`)
) ENGINE=MyISAM;




Theme © iAndrew 2016 - Forum software by © MyBB