[eluser]Stolz[/eluser]
I'm using the latest tip version from Bitbucket and I'm having a problem with an advanced relationship. I can save/delete in one direction but in the other one I can delete but not save.
The project I'm working on is about TV Shows. One user can follow many shows and one show can be followed by many users. A show can be followed only once by the same user.
My intention is to be able to do something like this:
Code:
$show = new Show(111);
$user = new User(222);
//Get all users that follow Show 111
$show->follower->get();
//Get all shows that User 222 follows
$user->follow->get();
Here are the models:
Code:
class User extends DataMapper {
public $has_many = array('
'follow' => array('
class' => 'show',
'other_field' => 'follower',
'join_self_as' => 'user',
'join_other_as' => 'show',
'join_table' => 'shows_followers'
));
}
class Show extends DataMapper {
public $has_many = array(
'follower' => array(
'class' => 'user',
'other_field' => 'follow',
'join_self_as' => 'show',
'join_other_as' => 'user',
'join_table' => 'shows_followers'
));
}
The join table looks like:
Code:
CREATE shows_followers (
id INT(10) unsigned NOT NULL AUTO_INCREMENT PRIMARY KEY,
show_id SMALLINT(5) unsigned /*I-T-F-k*/, FOREIGN KEY (show_id) REFERENCES shows(id),
user_id SMALLINT(5) UNSIGNED /*I-T-F-k*/, FOREIGN KEY (user_id) REFERENCES users(id),
UNIQUE(show_id, user_id)
) ENGINE=InnoDB;
If I try user 222 to follow show 111 like this, everything works
Code:
$show = new Show(111);
$user = new User(222);
$show->delete_follower($user); //Unfollow
$show->save_follower($user); //Follow
but if I try the same action just in the opposite direction it doesn't work
Code:
$show = new Show(111);
$user = new User(222);
$user->delete_follow($show); //Unfollow (this code works! it generates the proper query)
$user->save_follow($show); //Follow (this is where I get the error)
The error I get when I run
$user->save_follow($show) is
Quote:Fatal error: Class name must be a valid object or a string in application/libraries/datamapper.php on line 2461
The problem is DM is trying to create an instance of NULL class, which obviously doesn't exist. The class name comes from the DM function
get_related_properties($related_field) which is returning a NULL value. That means DM thinks there is no relation between the objects so I guess there is something wrong with my advanced relationship definition.
What did I do wrong?
Thanks.