Skip to content
Toggle navigation
Projects
Groups
Snippets
Help
public
/
sequelize
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
不要怂,就是干,撸起袖子干!
Commit c1ac2e71
authored
Sep 10, 2010
by
Sascha Depold
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
a lot of crazy hacking to get belongsTo work
1 parent
87f85fdb
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
18 additions
and
21 deletions
lib/sequelize/SequelizeTable.js
lib/sequelize/SequelizeTable.js
View file @
c1ac2e7
...
...
@@ -69,7 +69,7 @@ exports.SequelizeTable = function(Sequelize, sequelize, tableName, attributes, o
case
'belongsTo'
:
// adds the foreign key to me
// e.g. table.dayId = Sequelize.INTEGER
table
.
attributes
[
assocNameAsTableIdentifier
]
=
{
type
:
Sequelize
.
INTEGER
}
table
.
attributes
[
assocNameAsTableIdentifier
]
=
{
type
:
Sequelize
.
INTEGER
}
break
}
})
...
...
@@ -135,31 +135,24 @@ exports.SequelizeTable = function(Sequelize, sequelize, tableName, attributes, o
},
hasMany
:
function
(
assocName
,
_table
,
backAssocName
)
{
var
Factory
=
new
require
(
"./Factory"
).
Factory
(
Sequelize
,
sequelize
)
var
association
=
{
name
:
assocName
,
backAssociationName
:
backAssocName
,
table
:
_table
,
type
:
'hasMany'
}
var
Factory
=
new
require
(
"./Factory"
).
Factory
(
Sequelize
,
sequelize
),
association
=
{
name
:
assocName
,
backAssociationName
:
backAssocName
,
table
:
_table
,
type
:
'hasMany'
}
table
.
associations
.
push
(
association
)
// don't check inside of method to increase performance
if
(
backAssocName
)
{
table
.
prototype
[
Sequelize
.
Helper
.
SQL
.
addPrefix
(
'get'
,
assocName
)]
=
Factory
.
createManyToManyGetter
(
table
,
_table
,
assocName
,
backAssocName
)
table
.
prototype
[
Sequelize
.
Helper
.
SQL
.
addPrefix
(
'set'
,
assocName
)]
=
Factory
.
createManyToManySetter
(
table
,
_table
,
assocName
,
backAssocName
)
_table
.
prototype
[
Sequelize
.
Helper
.
SQL
.
addPrefix
(
'get'
,
backAssocName
)]
=
Factory
.
createManyToManyGetter
(
_table
,
table
,
backAssocName
,
assocName
)
_table
.
prototype
[
Sequelize
.
Helper
.
SQL
.
addPrefix
(
'set'
,
backAssocName
)]
=
Factory
.
createManyToManySetter
(
_table
,
table
,
backAssocName
,
assocName
)
Factory
.
addManyToManyMethods
(
table
,
_table
,
assocName
,
backAssocName
)
Factory
.
addManyToManyMethods
(
_table
,
table
,
backAssocName
,
assocName
)
}
else
{
table
.
prototype
[
Sequelize
.
Helper
.
SQL
.
addPrefix
(
'get'
,
assocName
)]
=
Factory
.
createOneToManyGetter
(
table
,
_table
,
assocName
)
table
.
prototype
[
Sequelize
.
Helper
.
SQL
.
addPrefix
(
'set'
,
assocName
)]
=
Factory
.
createOneToManySetter
(
table
,
assocName
)
Factory
.
addOneToManyMethods
(
table
,
_table
,
assocName
)
}
return
association
},
hasOne
:
function
(
assocName
,
_table
)
{
table
.
associations
.
push
({
name
:
assocName
,
table
:
_table
,
type
:
'hasOne'
})
var
association
=
{
name
:
assocName
,
table
:
_table
,
type
:
'hasOne'
}
table
.
associations
.
push
(
association
)
table
.
prototype
[
Sequelize
.
Helper
.
SQL
.
addPrefix
(
'get'
,
assocName
)]
=
function
(
callback
)
{
var
whereConditions
=
{}
...
...
@@ -188,15 +181,19 @@ exports.SequelizeTable = function(Sequelize, sequelize, tableName, attributes, o
})
}
return
table
return
association
},
belongsTo
:
function
(
assocName
,
_table
,
backAssociation
)
{
if
(
typeof
backAssociation
==
'undefined'
)
throw
new
Error
(
"Calling belongsTo with only two parameters is deprecated! Please take a look at the example in the repository!"
)
// this will overwrite the association name of the before defined hasOne or hasMany relation
backAssociation
.
name
=
assocName
// start - overwrite the association of the before defined hasOne or hasMany relation, to fit the belongsTo foreign keys
var
Factory
=
new
require
(
"./Factory"
).
Factory
(
Sequelize
,
sequelize
)
delete
_table
.
prototype
[
Sequelize
.
Helper
.
SQL
.
addPrefix
(
'get'
,
backAssociation
.
name
)]
delete
_table
.
prototype
[
Sequelize
.
Helper
.
SQL
.
addPrefix
(
'set'
,
backAssociation
.
name
)]
Factory
.
addOneToManyMethods
(
_table
,
table
,
assocName
,
backAssociation
.
name
)
// end - overwrite the association of the before defined hasOne or hasMany relation, to fit the belongsTo foreign keys
table
.
associations
.
push
({
name
:
assocName
,
table
:
_table
,
type
:
'belongsTo'
})
...
...
@@ -210,8 +207,8 @@ exports.SequelizeTable = function(Sequelize, sequelize, tableName, attributes, o
// setter
table
.
prototype
[
Sequelize
.
Helper
.
SQL
.
addPrefix
(
'set'
,
assocName
,
true
)]
=
function
(
object
,
callback
)
{
var
attr
=
{}
,
self
=
this
var
self
=
this
,
attr
=
{}
attr
[
Sequelize
.
Helper
.
SQL
.
asTableIdentifier
(
assocName
)]
=
object
.
id
...
...
@@ -220,7 +217,7 @@ exports.SequelizeTable = function(Sequelize, sequelize, tableName, attributes, o
})
}
return
table
return
_
table
}
}
// don't put this into the hash!
...
...
Write
Preview
Markdown
is supported
Attach a file
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to post a comment