Provides various official and user-released code examples. For code reference, you are welcome to learn to reload or override the getRelation function to obtain multi-layer associations and associated parameters: array ('ing ing _ name' => 'Next _ mappingname_or_array ', 'normal _ mapping_name ')
/**
* Obtain the association record of the returned data
* @ Access protected
* @ Param mixed $ result returns data
* @ Param string | array $ name join name
* @ Param boolean $ return whether to return the associated data itself
* @ Return array
*/
Protected function getRelation (& $ result, $ name = '', $ return = false ){
If (! Empty ($ this-> _ link )){
Foreach ($ this-> _ link as $ key => $ val ){
$ MappingName =! Empty ($ val ['ing ing _ name'])? $ Val ['ing ing _ name']: $ key; // ing name
If (empty ($ name) | true ===$ name | $ mappingName ==$ name | (is_array ($ name) & (in_array ($ mappingName, $ name) | array_key_exists ($ mappingName, $ name )))){
$ MappingType =! Empty ($ val ['ing ing _ type'])? $ Val ['ing ing _ type']: $ val; // join type
$ MappingClass =! Empty ($ val ['class _ name'])? $ Val ['class _ name']: $ key; // associate class name
$ MappingFields =! Empty ($ val ['ing ing _ fields'])? $ Val ['ing ing _ fields ']:' * '; // ing field
$ MappingCondition =! Empty ($ val ['condition'])? $ Val ['condition ']: '1 = 1'; // join condition
$ MappingKey =! Empty ($ val ['ing ing _ key'])? $ Val ['ing ing _ key']: $ this-> getPk (); // associate key name
If (strtoupper ($ mappingClass) = strtoupper ($ this-> name )){
// Obtain the parent key name from the auto-reference Association
$ MappingFk =! Empty ($ val ['parent _ key'])? $ Val ['parent _ key']: 'parent _ id ';
} Else {
$ MappingFk =! Empty ($ val ['forn N _ key'])? $ Val ['forn N _ key']: strtolower ($ this-> name). '_ id'; // associate a foreign key
}
// Obtain the associated model object
$ Model = D ($ mappingClass );
Switch ($ mappingType ){
Case HAS_ONE:
$ Pk = $ result [$ mappingKey];
$ MappingCondition. = "AND {$ mappingFk} = '{$ pk }'";
If (is_array ($ name) & array_key_exists ($ mappingName, $ name )){
$ RelationData = $ model-> relation ($ name [$ mappingName])-> where ($ mappingCondition)-> field ($ mappingFields)-> find ();
} Else {
$ RelationData = $ model-> where ($ mappingCondition)-> field ($ mappingFields)-> find ();
}
Break;
Case BELONGS_TO:
If (strtoupper ($ mappingClass) = strtoupper ($ this-> name )){
// Obtain the parent key name from the auto-reference Association
$ MappingFk =! Empty ($ val ['parent _ key'])? $ Val ['parent _ key']: 'parent _ id ';
} Else {
$ MappingFk =! Empty ($ val ['forn N _ key'])? $ Val ['forn N _ key']: strtolower ($ model-> getModelName (). '_ id'; // associate a foreign key
}
$ Fk = $ result [$ mappingFk];
$ MappingCondition. = "AND {$ model-> getPk ()} = '{$ fk }'";
If (is_array ($ name) & array_key_exists ($ mappingName, $ name )){
$ RelationData = $ model-> relation ($ name [$ mappingName])-> where ($ mappingCondition)-> field ($ mappingFields)-> find ();
} Else
$ RelationData = $ model-> where ($ mappingCondition)-> field ($ mappingFields)-> find ();
Break;
Case HAS_MANY:
$ Pk = $ result [$ mappingKey];
$ MappingCondition. = "AND {$ mappingFk} = '{$ pk }'";
$ MappingOrder =! Empty ($ val ['ing ing _ order'])? $ Val ['ing ing _ order']: '';
$ MappingLimit =! Empty ($ val ['ing ing _ limit '])? $ Val ['ing ing _ limit ']: '';
// Obtain association records with latency
If (is_array ($ name) & array_key_exists ($ mappingName, $ name )){
$ RelationData = $ model-> relation ($ name [$ mappingName])-> where ($ mappingCondition)-> field ($ mappingFields)-> order ($ mappingOrder) -> limit ($ mappingLimit)-> select ();
} Else
$ RelationData = $ model-> where ($ mappingCondition)-> field ($ mappingFields)-> order ($ mappingOrder)-> limit ($ mappingLimit)-> select ();
Break;
Case many_to_case:
$ Pk = $ result [$ mappingKey];
$ MappingCondition = "{$ mappingFk} = '{$ pk }'";
$ MappingOrder = $ val ['ing ing _ order'];
$ MappingLimit = $ val ['ing ing _ limit '];
$ MappingRelationFk = $ val ['relation _ foreign_key ']? $ Val ['relation _ foreign_key ']: $ model-> getModelName ().' _ id ';
$ MappingRelationTable = $ val ['relation _ table']? $ Val ['relation _ table']: $ this-> getRelationTableName ($ model );
$ SQL = "SELECT B. {$ mappingFields} FROM {$ mappingRelationTable} AS ,". $ model-> getTableName (). "AS B WHERE. {$ mappingRelationFk} = B. {$ model-> getPk ()} AND. {$ mappingCondition }";
If (! Empty ($ val ['condition ']) {
$ SQL. = 'AND'. $ val ['condition '];
}
If (! Empty ($ mappingOrder )){
$ SQL. = 'ORDER BY'. $ mappingOrder;
}
If (! Empty ($ mappingLimit )){
$ SQL. = 'limit'. $ mappingLimit;
}
If (is_array ($ name) & array_key_exists ($ mappingName, $ name )){
$ MappingOrder =! Empty ($ val ['ing ing _ order'])? $ Val ['ing ing _ order']: '';
$ MappingLimit =! Empty ($ val ['ing ing _ limit '])? $ Val ['ing ing _ limit ']: '';
$ RelationData = $ model-> relation ($ name [$ mappingName])
-> Field ($ model-> getTableName (). '.'. $ mappingFields)
-> Join ($ mappingRelationTable. "on $ mappingRelationTable. $ mappingRelationFk =". $ model-> getTableName (). ".". $ model-> getPk ())
-> Where ($ mappingRelationTable. ". {$ mappingFk} = '{$ pk }'")
-> Order ($ mappingOrder)
-> Limit ($ mappingLimit)
-> Select ();
} Else
$ RelationData = $ this-> query ($ SQL );
Break;
}
If (! $ Return ){
If (isset ($ val ['as _ fields ']) & in_array ($ mappingType, array (HAS_ONE, BELONGS_TO ))){
// You can directly map the associated field values to a field in the data object.
// Only HAS_ONE BELONGS_TO is supported
$ Fields = explode (',', $ val ['as _ fields ']);
Foreach ($ fields as $ field ){
If (strpos ($ field ,':')){
List ($ relationName, $ nick) = explode (':', $ field );
$ Result [$ nick] = $ relationData [$ relationName];
} Else {
$ Result [$ field] = $ relationData [$ field];
}
}
} Else {
$ Result [$ mappingName] = $ relationData;
}
Unset ($ relationData );
} Else {
Return $ relationData;
}
}
}
}
Return $ result;
}
AD: truly free, domain name + VM + enterprise mailbox = 0 RMB