This article mainly introduces ThinkPHP3.2's related information on the associated query solution. if you need some friends, you can refer to it and paste the code directly. the code is very simple, these are common SQL statements. For more information, see the following.
Create table 'test _ avatar '('uid' int (11) unsigned not null default '0', 'Avatar' varchar (255) not null default '', primary key ('uid') ENGINE = MyISAM default charset = utf8; insert into 'test _ avatar 'VALUES (1 ,'. /Uploads/admin.jpg '); create table 'test _ PRO' ('id' int (11) unsigned not null AUTO_INCREMENT, 'uid' int (11) unsigned not null default '0', 'name' varchar (255) not null default '', primary key ('id') ENGINE = MyISAM default charset = utf8; insert into 'test _ Pro' VALUES (, 'product 1'), (, 'product 2 '); create table 'test _ user' ('id' int (11) unsigned not null AUTO_INCREMENT, 'Username' varchar (50) not null default '', 'Tel 'int (11) unsigned not null default '0', primary key ('id') ENGINE = MyISAM default charset = utf8; insert into 'test _ user' VALUES (1, 'admin ', 110 );
For example, there are three tables: User profile table, product table, and user table.
How to establish association in the model?
class ProModel extends RelationModel{ protected $_link=array( 'Avatar'=>array( 'mapping_type'=>self::HAS_ONE, 'class_name'=>'Avatar', 'foreign_key'=>'uid', 'mapping_fields'=>'avatar', 'as_fields'=>'avatar', ), )}
I tried HAS_ONE, BELONGS_TO, has_detail, and many_to_detail.
------ Solution ----------------------
Create ProModelModel. class. php under Model
class ProModelModel extends ViewModel { public $viewFields = array( 'test_avatar'=>array( 'id',... ), 'test_pror'=>array( 'id',... '_on'=>'' ), 'test_pror'=>array( 'id',... '_on'=>'' ), ); }
Problems related to SQL statements in Thinkphp and solutions
In THINKPHP, the data table contains a field in the form of {1, 2, 3, 4, 5}
So how can I compare the obtained ID value with the content in the field in the SQL statement?
------ Solution ----------------------
Use fuzzy search
$ Where = array ('field '=> array ('like',' % '. $ ID.' % '));