Fixes in belongsTo relation definition syntax

This commit is contained in:
Anatoliy Chakkaev 2013-03-27 04:48:26 +04:00
parent d3752eab4c
commit 2749a1f7e0
1 changed files with 15 additions and 5 deletions

View File

@ -512,6 +512,12 @@ AbstractClass.include = function (objects, include, cb) {
} }
var relation = relations[relationName]; var relation = relations[relationName];
if (!relation) {
return function() {
cb(new Error('Relation "' + relationName + '" is not defined for ' + self.modelName + ' model'));
}
}
var req = {'where': {}}; var req = {'where': {}};
if (!keyVals[relation.keyFrom]) { if (!keyVals[relation.keyFrom]) {
@ -957,6 +963,9 @@ AbstractClass.belongsTo = function (anotherClass, params) {
params = params || {}; params = params || {};
if ('string' === typeof anotherClass) { if ('string' === typeof anotherClass) {
params.as = anotherClass; params.as = anotherClass;
if (params.model) {
anotherClass = params.model;
} else {
var anotherClassName = anotherClass.toLowerCase(); var anotherClassName = anotherClass.toLowerCase();
for(var name in this.schema.models) { for(var name in this.schema.models) {
if (name.toLowerCase() === anotherClassName) { if (name.toLowerCase() === anotherClassName) {
@ -964,12 +973,13 @@ AbstractClass.belongsTo = function (anotherClass, params) {
} }
} }
} }
}
var methodName = params.as || i8n.camelize(anotherClass.modelName, true); var methodName = params.as || i8n.camelize(anotherClass.modelName, true);
var fk = params.foreignKey || methodName + 'Id'; var fk = params.foreignKey || methodName + 'Id';
this.relations[params['as']] = { this.relations[params['as']] = {
type: 'belongsTo', type: 'belongsTo',
keyFrom: params['foreignKey'], keyFrom: fk,
keyTo: 'id', keyTo: 'id',
modelTo: anotherClass, modelTo: anotherClass,
multiple: false multiple: false