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

[eluser]Lyon[/eluser]
Hi all,

I have just started using DMZ for ORM and am having a little bit of trouble understanding the relationships.

I have a table users :
Code:
CREATE TABLE IF NOT EXISTS `ci_dmz`.`users`
(
    `id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
    `username` VARCHAR(20),
    `password` VARCHAR(128),
    `salt` VARCHAR(32),
    `name` VARCHAR(40),
    `email` VARCHAR(100),
    
    PRIMARY KEY(`id`),
    UNIQUE KEY `username` (`username`),
    UNIQUE KEY `email` (`email`)
) DEFAULT CHARSET=utf8;

and a table posts :
Code:
CREATE TABLE IF NOT EXISTS `ci_dmz`.`posts`
(
    `id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
    `author_id` INT UNSIGNED,
    `title` VARCHAR(100),
    `content` TEXT,
    `created_at` DATETIME,
    `updated_at` DATETIME,
    `is_published` INT(1),
    
    PRIMARY KEY(`id`),
    UNIQUE KEY `title` (`title`)
) DEFAULT CHARSET=utf8;

I have the relationship on the author id matching the user id using :
In the User model :
Code:
var $has_many = array(
    'posts' => array(
        'class' => 'post',
        'other_field' => 'author'
        )
    );
In the Post model :
Code:
var $has_one = array(
    'author' => array(
        'class' => 'user',
        'other_field' => 'user'
        )
    );

I retrieve the posts as :
Code:
$u = new User();
$u->get_by_username($username);
$u->posts->get();
foreach($u->posts as $post)
{
    echo $post->title;
    echo '<br>';
    echo $post->content;
    echo '<br>';
    echo '<br>';
}
exit();

Now this works, but I am unsure as to why it works.
I am hesitant to continue without understanding fully what part(s) of the $has_many and $has_one arrays is relating to the class (model) and what part(s) are relating to the database fields.

If someone who understands this would be able to take a couple of minutes to break this down for me I would greatly appreciate it.

Thanks,
Lyon.
#2

[eluser]WanWizard[/eluser]
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.
#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.




Theme © iAndrew 2016 - Forum software by © MyBB