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