Welcome Guest, Not a member yet? Register   Sign In
DMZ confusion
#3

[eluser]Lyon[/eluser]
[quote author="WanWizard" date="1279884473"]You have defined a one-to-many between user and post, on the user field author.

What happens is that your 'get_by_username' gets the user record. When you then requests the post, DMZ knows from your definition that the posts table has a field called 'author' which is the foreign key to users.

It basically does a "SELECT * FROM posts WHERE author = <userid>".

You can do
Code:
$u->check_last_query();
to see what query is executed when you call a DMZ method.[/quote]

Thanks :-)
You've helped me trace down the problem of why it didn't look right, it wasn't lol. It would work by getting the posts from a user, but no the user from a post.
The solution was :
In user :
Code:
var $has_many = array(
    'posts' => array(
        'class' => 'post',
        'other_field' => 'author'
        )
    );
in post :
Code:
var $has_one = array(
    'author' => array(
        'class' => 'user',
        'other_field' => 'posts'
        )
    );

I understand it now.
The first array contains the property you use to access the data from the relationship:
e.g. $post->author or $user->posts
The class is literally the class that the other_field belongs to.
e.g. the author field can be found in class post in the has_one or has_many array
or
the posts reference can be found in the user class.

The rules for mapping from the class to the database take care of the rest.

Thanks a lot for your help WanWizard, that check last query will save me a ton of hassle, I honestly would never have thought to look in the 'Utility Methods' section of the documentation lol.

Thanks again!

Edit : If anyone comes across this thread just to let you know if you use :
Code:
echo $u->check_last_query();
The query will be output twice as check_last_query internally echo's the content.
More information can be found here.


Messages In This Thread
DMZ confusion - by El Forum - 07-22-2010, 05:55 PM
DMZ confusion - by El Forum - 07-23-2010, 12:27 AM
DMZ confusion - by El Forum - 07-23-2010, 01:37 AM



Theme © iAndrew 2016 - Forum software by © MyBB