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

[eluser]Benedikt[/eluser]
Just to be clear on that, I really appreciate your enhancements and sharing. The more I understand the manual the more I see that your extensions are absolutely useful for me.

I wanted to express that I didnt want to annoy you on purpose or take ur time from running ur own company, good luck with that btw. I understand DMZ as a very powerful extension to CI and am happy that I found it. Just its sometimes a little annoying if I want to get deeper into it to understand it and use its full power, then being stuck on a few questions which seem not to be covered by the manual (and yes I have read it). But if I ask for explanation I get forwarded to the manual (and it were not the very stupid ones in my eyes at least).

Maybe you can understand my thinking.

I will continue to use DMZ and the functionality I unterstood so far.
#32

[eluser]Benedikt[/eluser]
Maybe someone can help me with this:

I understood that I have to set up relationships between models in two ways:
1) In model "Car" that it "has_one" "Color".
2) In "Color" that is "has_many" "Car".

Is that correct?

What is the difference between setting "Car" as the parent model in "Color" and using "DataMapper" as parent? Can I not access "Color" then as the root object?

Thanks you could point me to the manual here or explain in a few words.
Thanks!!
#33

[eluser]OverZealous[/eluser]
[quote author="Benedikt" date="1256208589"]
I understood that I have to set up relationships between models in two ways:
1) In model "Car" that it "has_one" "Color".
2) In "Color" that is "has_many" "Car".

Is that correct?[/quote]

Yup, that's correct; all relationships require two descriptions. The reason is that DMZ has to be able to look at the relationship from both sides, ie: Car->Color as well as Color->Car. The number of items doesn't matter (has_one or has_many), or how they are joined (via join table or In-Table Foreign Key [ITFK]).

Quote:What is the difference between setting "Car" as the parent model in "Color" and using "DataMapper" as parent? Can I not access "Color" then as the root object?

In DMZ there is no true "parent" or "child" models. All relationships just are. (Sometimes it is convenient in Many-to-One relationships to refer to one side as the parent and the other as the child, but that's just semantics.)

What this boils down to is you can easily find all Cars with a certain Color, like so:
Code:
$color = new Color();
$color->get_by_name('Red');

$cars = $color->car->get();
// $cars contains all red cars

You can even query deeper, like so:
Code:
$cars2 = $color->car->where('brand', 'Ferarri')->get();
// $cars2 contains all red Ferraris

DMZ also supports flipping the relationships around, so you can do things like this:
Code:
$cars3 = new Car();
$cars3->where_related($color)->where('brand', 'Ferarri')->get();
// $cars3 contains all red Ferraris

Or say you keep manufacturers on their own table, you could do this:
Code:
// Assume you already know the ID you want to look up
$ferarri = new Manufacturer($ferrari_id);
$cars4 = new Car();
$cars4->where_related($color)->where_related($ferarri)->get();

Finally, you can do some really crazy stuff with deep relationships. For example, finding all Manufacturers that have Red cars (of any type):
Code:
// just to blow your mind
$m = new Manufacturer();
$m->distinct()->where_related('car/color', 'name', 'Red')->get();

Have fun. :-P
#34

[eluser]Benedikt[/eluser]
Sounds great.

Thanks for your fast and detailed reply.
#35

[eluser]ennis[/eluser]
Hi guys back again done loads of relationships now working great loving it,

But I just cant get my head around this one its silly but I think I have confused my self somehow Smile

I have a users table, and a clients table I already have users have many clients and clients has many users and it works perfectly.

Then I have a new realtions table to tell me witch users are admins for witch clients, called clients_admins how would I relate the client to this table? and user the other way? as they both have many

is it possible etc?

Cheers.
#36

[eluser]OverZealous[/eluser]
[quote author="ennis" date="1256223999"]I have a users table, and a clients table I already have users have many clients and clients has many users and it works perfectly.

Then I have a new realtions table to tell me witch users are admins for witch clients, called clients_admins how would I relate the client to this table? and user the other way? as they both have many[/quote]

OK, I'm gonna try to wrap my head around this. I think you mean that you have Many Users to Many Clients already. You now want to add the ability to have Many Admins to Many Clients, where Admins are just a special case for Users.

If this is correct, then I recommend looking at join fields. They are the easiest solution, because you can just make note of which Users are Admins by adding a boolean to the join table. You won't need the extra admins_clients table at all.

Say you add a column called "isadmin" to your "clients_users" table. You use it like this:
Code:
$client = ...;
// get all admins
$client->user->where_join_field('client', 'isadmin', TRUE)->get();

// make a user an admin
$user = ...
$user->save($client);
$user->set_join_field($client, 'isadmin', TRUE);

// revoke admin rights:
$user->set_join_field($client, 'isadmin', FALSE);

// get all users, along with admin flag
$client->user->include_join_fields()->get();
foreach($client->user as $user) {
    echo $user->name . ' is ';
    if( ! $user->join_isadmin) {
       echo 'not ';
    }
    echo 'an Admin.';
}

If you only need one admin per client, then you can either use the join field, or you can add an additional relationship (but it may be more difficult to manage).
#37

[eluser]ennis[/eluser]
exactly mate, thanks alot.

Sorry for the crappy formulation of my problem.
#38

[eluser]Benedikt[/eluser]
I have a question about return values.

Let's say I search for a user:

Code:
$u = new User();
$u->get_by_id( $id );

Now I want to verify if a user was found or not. Currently I always do sth like that:

Code:
if ( $u->id > 0 ) {
   ...
}

Is this the recommended way? Does "id" have a default value (e.g. 0 or NULL) or is it just emtpy so I would rather do a
Code:
if ( ! empty($u->id) ) {
   ...
}

I could find sth similar in the examples but not sure if this is applicable to id's as well.
Thanks for a comment on that!
#39

[eluser]OverZealous[/eluser]
The method is called exists.

You can see it in use in the login example.
#40

[eluser]Benedikt[/eluser]
Ok, thanks. I thought that it would be better to use a native PHP function due to performance reason.

Thanks for your recommendation.




Theme © iAndrew 2016 - Forum software by © MyBB