Thinkphp association model and thinkphp Model
Thinkphp comes with the associated model has_plugin.
Use: Create ProductModel. class. php In the Model folder.
The Code is as follows:
<? 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 ('ing ing _ type' => self: HAS_MANY, 'class _ name' => 'attr', 'foreign _ key' => 'product _ id', 'ing ing _ name' => 'A ', // used to obtain the data 'ing ing _ fields '=> 'id, name, value', // 'as _ fields' => 'id, name, value ',));}
Use 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 (// here is a two-dimensional array, because it is a has_struct model 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 ;}