Home>Article>PHP Framework> How to use thinkphp association model

How to use thinkphp association model

尚
forward
2020-04-01 09:19:35 3085browse

How to use thinkphp association model

thinkphp comes with the associated model HAS_MANY.

Use, create ProductModel.class.php in the Model folder

The code is as follows:

use Think\Model\RelationModel; class ProductModel extends RelationModel{ 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();  $data['a']= array( //这里为二维数组,因为是HAS_MANY模型 array( 'name' => 'color', 'value' => serialize($postData['phone_color'])), array( 'name' => 'size', 'value' => $postData['phone_size']), ); $result = $productModel->relation(true)->add($data); dump($result);exit; }

How to use thinkphp association model

How to use thinkphp association model

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!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete