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.


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