thinkphp comes with the associated model HAS_MANY.
Use, create ProductModel.class.php in the Model folder
The code is as follows:
<?php namespace Test\Model; <span style="color:#FF0000;">use Think\Model\RelationModel;</span> class ProductModel extends <span style="color:#FF0000;">RelationModel</span>{ protected $_link = array( 'attr' =>array( 'mapping_type' => self::HAS_MANY, 'class_name' => 'attr', 'foreign_key' => 'product_id', 'mapping_name' => 'a', //用来取数据 'mapping_fields' => 'id,name,value', // 'as_fields' => 'id,name,value', ) );}
How to use it in the controller:
public function testRelation(){ $postData = I('post.'); // dump($postData);die; $productModel = D("Test/Product"); $data['name'] = $postData['phone_name']; // $data['thumb'] = $photo[0]; $data['thumb'] = 'kk'; $data['create_time'] = time(); <span style="color:#FF0000;"> $data['a']= array( //这里为二维数组,因为是HAS_MANY模型 array( 'name' => 'color', 'value' => serialize($postData['phone_color'])), array( 'name' => 'size', 'value' => $postData['phone_size']),</span> ); $result = $productModel->relation(true)->add($data); dump($result);exit; }
Recommended tutorial: thinkphp tutorial
The above is the detailed content of How to use thinkphp association model. For more information, please follow other related articles on the PHP Chinese website!