[sql]
I have 3 tables in the database tbl_player, tbl_player_sports and tbl_sports
CREATE TABLE IF NOT EXISTS tbl_player_sports (
player_id int(11) NOT NULL DEFAULT ‘0’,
sport_id int(11) NOT NULL DEFAULT ‘0’,
PRIMARY KEY (player_id,sport_id),
KEY FK_player_id (player_id),
KEY FK_sport_id (sport_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
INSERT INTO tbl_player_sports (player_id, sport_id) VALUES
(15, 1),
(15, 2),
(17, 2),
(17, 3);
ALTER TABLE tbl_player_sports
ADD CONSTRAINT FK_player_id FOREIGN KEY (player_id) REFERENCES tbl_player (id) ON DELETE CASCADE,
ADD CONSTRAINT FK_sport_id FOREIGN KEY (sport_id) REFERENCES tbl_sports (id) ON DELETE CASCADE;
CREATE TABLE IF NOT EXISTS tbl_sports (
id int(11) NOT NULL AUTO_INCREMENT,
name varchar(20) COLLATE utf8_unicode_ci NOT NULL,
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=4 ;
–
– Дамп данных таблицы tbl_sports
–
INSERT INTO tbl_sports (id, name) VALUES
(1, ‘tennis’),
(2, ‘football’),
(3, ‘swimming’);
CREATE TABLE IF NOT EXISTS tbl_player (
id int(11) NOT NULL AUTO_INCREMENT,
username varchar(128) COLLATE utf8_unicode_ci NOT NULL,
password varchar(128) COLLATE utf8_unicode_ci NOT NULL,
salt varchar(128) COLLATE utf8_unicode_ci NOT NULL,
email varchar(128) COLLATE utf8_unicode_ci NOT NULL,
name varchar(64) COLLATE utf8_unicode_ci NOT NULL,
sname varchar(128) COLLATE utf8_unicode_ci NOT NULL,
fname varchar(128) COLLATE utf8_unicode_ci NOT NULL,
aim int(1) NOT NULL,
atp_rate int(11) DEFAULT NULL,
photo varchar(90) COLLATE utf8_unicode_ci NOT NULL,
profile text COLLATE utf8_unicode_ci,
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=19 ;
INSERT INTO tbl_player (id, username, password, salt, email, name, sname, fname, aim, atp_rate, photo, profile) VALUES
(15, ‘root’, ‘8fabcc92999b55cc343646120f8f547c’, ‘28b206548469ce62182048fd9cf91760’, ‘we@g.net’, ‘as’, ‘we’, ‘as’, 0, 45, ‘48413086.jpg’, NULL),
(17, ‘zxc’, ‘1f9099ad686311ab046a6510508bff93’, ‘28b206548469ce62182048fd9cf91760’, ‘t@bigmir.net’, ‘zxc’, ‘zxc’, ‘zxc’, 0, 345, ‘1187134.jpg’, NULL),
[/sql]
And I have a model for a User with relation
'sports'=>array(self::MANY_MANY, 'Sport','tbl player sports(player id, sport id)'),