$u = D('user'); $u->name = 'xiak'; $u->password = 'qqqqqq'; $u->shop = array( 'gg' => '1', ); $u->relation(true)->add($data);
user(id, name)shop(id, name)user_shop(user_id,shop_id)
class UserModel extends RelationModel { protected $_link = array( 'shop' => array( 'mapping_type' => self::MANY_TO_MANY, 'relation_table' => '__USER_SHOP__', ) }
怎麼沒有效果啊?
sql調試是這樣的:
[user] SHOW COLUMNS FROM `tb_user` (executeTime: 0.009001s ) [user] INSERT INTO `tb_user` (`name`,`password`) VALUES ('xiak','qqqqqq') (executeTime: 0.117007s ) [shop] SHOW COLUMNS FROM `tb_shop` (executeTime: 0.013001s ) [shop] INSERT INTO tb_user_shop (user_id,shop_id) SELECT a.id,b.id FROM tb_user AS a ,tb_shop AS b where a.id =45 AND b.id IN (1) (executeTime: 0.001000s )
手冊上說 當MANY_TO_MANY時,不建議使用關聯插入。是什麼意思啊?
回複內容:
$u = D('user'); $u->name = 'xiak'; $u->password = 'qqqqqq'; $u->shop = array( 'gg' => '1', ); $u->relation(true)->add($data);
user(id, name)shop(id, name)user_shop(user_id,shop_id)
class UserModel extends RelationModel { protected $_link = array( 'shop' => array( 'mapping_type' => self::MANY_TO_MANY, 'relation_table' => '__USER_SHOP__', ) }
怎麼沒有效果啊?
sql調試是這樣的:
[user] SHOW COLUMNS FROM `tb_user` (executeTime: 0.009001s ) [user] INSERT INTO `tb_user` (`name`,`password`) VALUES ('xiak','qqqqqq') (executeTime: 0.117007s ) [shop] SHOW COLUMNS FROM `tb_shop` (executeTime: 0.013001s ) [shop] INSERT INTO tb_user_shop (user_id,shop_id) SELECT a.id,b.id FROM tb_user AS a ,tb_shop AS b where a.id =45 AND b.id IN (1) (executeTime: 0.001000s )
手冊上說 當MANY_TO_MANY時,不建議使用關聯插入。是什麼意思啊?
難道就沒有人跟我一樣,覺得分拆SQL再加事務才是最好的方案嗎,關聯的SQL其它人看起來是不是很受罪呢