The field associated with the foreign key is not the ID field in the belonsto relationship of CakePHP

Source: Internet
Author: User
Tags types of tables sql using

In use, it is found that activerecord of CakePHP is not very useful sometimes.

For example:

MySQL database. The SQL statement is as follows:

 
Drop table if exists 'works'; Create Table 'works' ('id' int (11) not null auto_increment, 'adate' date not null, 'btime' varchar (4) not null, 'ctime' varchar (4) not null, 'content' text, 'IP' varchar (15) not null, primary key ('id ')) engine = InnoDB default charset = utf8; drop table if exists 'users'; Create Table 'users' ('id' int (11) not null auto_increment, 'name' varchar (15) not null, 'IP' varchar (15) not null, primary key ('id') engine = InnoDB default charset = utf8;

 

Two model classes

 

ClassWorkExtendsAppmodel {Public $ Name= 'Work';Public $ Belonsto=Array('User');}ClassUserExtendsAppmodel {Public $ Name= 'User';}

 

According to the default regulations of CakePHP, there should be a user_id field in the works table that corresponds to the ID in the users table.

However, the two types of tables are defined using the IP field. (It is actually a design error. I should have replaced the IP field in the works table with the user_id field)

ProgramAn error occurred during running:

...... From 'Works 'as 'work' left join 'users' as 'user' on ('work '. 'User _ id' = 'user '. 'id ')......

The modification is as follows:

 Class Work Extends  Appmodel {  Public   $ Name = 'Work' ;  Public   $ Belonsto = Array  ( 'User' => Array ( 'Classname' => 'user', 'foreignkey' => 'IP' ));}  Class User Extends  Appmodel {  Public   $ Name = 'User' ;  Public   $ Primarykey = 'IP' ;} 

 

This operation runs correctly, but it violates the intention because the primary key of the users table should be Id rather than IP. (In theory, the IP address and ID should also be unique)

 

I checked the CakePHP document and I don't know how to set it.

If all the designs comply with the CakePHP rules, it is indeed very convenient, but once the regulations are violated, sometimes it is quite difficult to cope.

Found a web page: http://q.hatena.ne.jp/1278656235

 

Bytes ------------------------------------------------------------------------------------------------------------------------------

If you cancel the association between two models and call SQL using the query method, some problems can be solved in some cases.

For example:
$ Works = $ this-> Work-> query ('select * from works work, users user where work. IP = user. ip ');
$ This-> set (COMPACT ('works '));

Bytes ------------------------------------------------------------------------------------------------------------------------------

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.