Sharing the related query solution in ThinkPHP3.2 _ php instance-php Tutorial

Source: Internet
Author: User
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.' % '));
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.