thinkphp 關聯模型,thinkphp模型

來源:互聯網
上載者:User

thinkphp 關聯模型,thinkphp模型


thinkphp 內建關聯模型HAS_MANY。

使用,在Model檔案夾下建立ProductModel.class.php

代碼如下:

<?phpnamespace 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',) );}

在控制器裡的使用方法:

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;      }



相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.