I just started to use the PHP ActiveRecord class and get along with it quiet well but now I'm stuck by trying to associate a user with a group, and a group with many users.
This is what I've got:
The SQL-Part:
CREATE TABLE `users` (
`id` INT(255) NOT NULL AUTO_INCREMENT,
`group_id` INT(255) NOT NULL ,
`name` VARCHAR(150) NOT NULL ,
`email` VARCHAR (150) NOT NULL ,
`passwd` VARCHAR (512) NOT NULL ,
`salt` VARCHAR (16) NOT NULL ,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
CREATE TABLE `groups` (
`id` INT(255) NOT NULL AUTO_INCREMENT,
`name` VARCHAR(150) NOT NULL ,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
The PHP-Class:
#Class User.php
class User extends ActiveRecord\Model{
static $belongs_to = array(
array('group')
);
static $validates_presence_of = array(
array('name'),
array('email'),
array('passwd'),
array('salt')
);
static $validates_uniqueness_of = array(
array('name'),
array('email')
);
}
#Class Group.php
class Group extends ActiveRecord\Model{
static $has_many = array(
array('users')
);
static $validates_presence_of = array(
array('name')
);
static $validates_uniqueness_of = array(
array('name')
);
}
VAR DUMP User:
<?php
var_dump(User::find(1));
?>
Now if I search for an user no group is being attached... Does someone know how to solve this or can tell me what I'm doin wrong? I just can't find my mistake... :)
var_dump output:
object(User)[32]
public 'errors' => null
private 'attributes' (ActiveRecord\Model) =>
array (size=6)
'id' => int 1
'group_id' => int 1
'name' => string 'admin' (length=5)
'email' => string 'user@email.com' (length=17)
'passwd' => string 'f22b07f3b0f2d93a696336f040a6d08b8c36fe53fc5f080b5a3ad1db3387b4553f16ed2c7c4196900f3ff9b8aa516b115e8250be6c0a60f6e22cf768fe43c291' (length=128)
'salt' => string '6976e5b5410415bd' (length=16)
private '__dirty' (ActiveRecord\Model) =>
array (size=0)
empty
private '__readonly' (ActiveRecord\Model) => boolean false
private '__relationships' (ActiveRecord\Model) =>
array (size=0)
empty
private '__new_record' (ActiveRecord\Model) => boolean false
Thanks anyway for having a look at it :)