thinkphp关联模型 多对多 插入 怎么使用?????

WBOY
Lepaskan: 2016-06-06 20:24:23
asal
1411 orang telah melayarinya

<code>$u = D('user');
        $u->name = 'xiak';
        $u->password = 'qqqqqq';
        $u->shop = array(
                'gg' => '1',
            );
        $u->relation(true)->add($data);</code>
Salin selepas log masuk
Salin selepas log masuk
<code>user(id, name)
shop(id, name)
user_shop(user_id,shop_id)</code>
Salin selepas log masuk
Salin selepas log masuk
<code>class UserModel extends RelationModel {
    protected $_link = array(
        'shop' => array(
            'mapping_type' => self::MANY_TO_MANY,
            'relation_table' => '__USER_SHOP__',
                            )
                       }</code>
Salin selepas log masuk
Salin selepas log masuk

怎么没有效果啊?

sql调试是这样的:

<code>[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 )</code>
Salin selepas log masuk
Salin selepas log masuk

手册上说 当MANY_TO_MANY时,不建议使用关联插入。是什么意思啊?

回复内容:

<code>$u = D('user');
        $u->name = 'xiak';
        $u->password = 'qqqqqq';
        $u->shop = array(
                'gg' => '1',
            );
        $u->relation(true)->add($data);</code>
Salin selepas log masuk
Salin selepas log masuk
<code>user(id, name)
shop(id, name)
user_shop(user_id,shop_id)</code>
Salin selepas log masuk
Salin selepas log masuk
<code>class UserModel extends RelationModel {
    protected $_link = array(
        'shop' => array(
            'mapping_type' => self::MANY_TO_MANY,
            'relation_table' => '__USER_SHOP__',
                            )
                       }</code>
Salin selepas log masuk
Salin selepas log masuk

怎么没有效果啊?

sql调试是这样的:

<code>[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 )</code>
Salin selepas log masuk
Salin selepas log masuk

手册上说 当MANY_TO_MANY时,不建议使用关联插入。是什么意思啊?

难道就没有人跟我一样,觉得分拆SQL再加事务才是最好的方案吗,关联的SQL其它人看起来是不是很受罪呢

Label berkaitan:
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan