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 4d39a3bd
authored
Sep 11, 2010
by
Sascha Depold
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
hasAndBelongsToMany association!
1 parent
14b399db
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
53 additions
and
21 deletions
examples/Associations/app.js
lib/sequelize/Factory.js
lib/sequelize/SequelizeTable.js
examples/Associations/app.js
View file @
4d39a3b
var
Sequelize
=
require
(
__dirname
+
"/../../lib/sequelize/Sequelize"
).
Sequelize
,
var
Sequelize
=
require
(
__dirname
+
"/../../lib/sequelize/Sequelize"
).
Sequelize
,
sequelize
=
new
Sequelize
(
"sequelize_test"
,
"root"
,
null
)
sequelize
=
new
Sequelize
(
"sequelize_test"
,
"root"
,
null
,
{
disableLogging
:
true
}
)
var
Person
=
sequelize
.
define
(
'person'
,
{
var
Person
=
sequelize
.
define
(
'person'
,
{
name
:
Sequelize
.
STRING
name
:
Sequelize
.
STRING
...
@@ -9,8 +9,8 @@ var Pet = sequelize.define('pet', {
...
@@ -9,8 +9,8 @@ var Pet = sequelize.define('pet', {
name
:
Sequelize
.
STRING
name
:
Sequelize
.
STRING
})
})
/*Person.hasMany('brothers', Person
)
Person
.
hasMany
(
'brothers'
)
Person.hasMany('sisters'
, Person)*/
Person
.
hasMany
(
'sisters'
)
var
fatherAssoc
=
Person
.
hasOne
(
'father'
,
Person
)
var
fatherAssoc
=
Person
.
hasOne
(
'father'
,
Person
)
var
motherAssoc
=
Person
.
hasOne
(
'mother'
,
Person
)
var
motherAssoc
=
Person
.
hasOne
(
'mother'
,
Person
)
...
@@ -28,11 +28,11 @@ Sequelize.chainQueries([{drop: sequelize}, {sync: sequelize}], function() {
...
@@ -28,11 +28,11 @@ Sequelize.chainQueries([{drop: sequelize}, {sync: sequelize}], function() {
sister
=
new
Person
({
name
:
'Sister'
}),
sister
=
new
Person
({
name
:
'Sister'
}),
pet
=
new
Pet
({
name
:
'Bob'
})
pet
=
new
Pet
({
name
:
'Bob'
})
Sequelize
.
chainQueries
([{
save
:
person
},
{
save
:
mother
},
{
save
:
father
},
{
save
:
brother
},
{
save
:
sister
},
{
save
:
pet
}],
function
()
{
Sequelize
.
chainQueries
([{
save
:
person
},
{
save
:
mother
},
{
save
:
father
},
{
save
:
brother
},
{
save
:
sister
},
{
save
:
pet
}],
function
()
{
person
.
setMother
(
mother
,
function
(
mom
)
{
Sequelize
.
Helper
.
log
(
'my mom: '
+
mom
.
name
)
})
person
.
setMother
(
mother
,
function
(
mom
)
{
Sequelize
.
Helper
.
log
(
'my mom: '
+
mom
.
name
)
})
person
.
setFather
(
father
,
function
(
dad
)
{
Sequelize
.
Helper
.
log
(
'my dad: '
+
dad
.
name
)
})
person
.
setFather
(
father
,
function
(
dad
)
{
Sequelize
.
Helper
.
log
(
'my dad: '
+
dad
.
name
)
})
/*person.setBrothers([brother], function(bros) { Sequelize.Helper.log('ma bro: '
+ bros[0].name)})
person
.
setBrothers
([
brother
],
function
(
bros
)
{
Sequelize
.
Helper
.
log
(
"ma bro: "
+
bros
[
0
].
name
)})
person.setSisters([sister], function(sis) { Sequelize.Helper.log('ma sis\': ' + sis[0].name)})*/
person
.
setSisters
([
sister
],
function
(
sis
)
{
Sequelize
.
Helper
.
log
(
"ma sis: "
+
sis
[
0
].
name
)})
person
.
setPets
([
pet
],
function
(
pets
)
{
Sequelize
.
Helper
.
log
(
'my pet: '
+
pets
[
0
].
name
)})
person
.
setPets
([
pet
],
function
(
pets
)
{
Sequelize
.
Helper
.
log
(
'my pet: '
+
pets
[
0
].
name
)})
})
})
})
})
\ No newline at end of file
lib/sequelize/Factory.js
View file @
4d39a3b
...
@@ -133,7 +133,6 @@ module.exports.Factory = function(Sequelize, sequelize) {
...
@@ -133,7 +133,6 @@ module.exports.Factory = function(Sequelize, sequelize) {
return
function
(
objects
,
callback
)
{
return
function
(
objects
,
callback
)
{
var
self
=
this
,
var
self
=
this
,
objectIds
=
Sequelize
.
Helper
.
Array
.
map
(
objects
,
function
(
obj
)
{
return
obj
.
id
})
objectIds
=
Sequelize
.
Helper
.
Array
.
map
(
objects
,
function
(
obj
)
{
return
obj
.
id
})
Sequelize
.
Helper
.
log
(
this
)
this
[
Sequelize
.
Helper
.
SQL
.
addPrefix
(
'get'
,
methodName
)](
function
(
currentAssociations
)
{
this
[
Sequelize
.
Helper
.
SQL
.
addPrefix
(
'get'
,
methodName
)](
function
(
currentAssociations
)
{
var
currentIds
=
Sequelize
.
Helper
.
Array
.
map
(
currentAssociations
,
function
(
assoc
)
{
return
assoc
.
id
}),
var
currentIds
=
Sequelize
.
Helper
.
Array
.
map
(
currentAssociations
,
function
(
assoc
)
{
return
assoc
.
id
}),
...
...
lib/sequelize/SequelizeTable.js
View file @
4d39a3b
...
@@ -61,6 +61,12 @@ exports.SequelizeTable = function(Sequelize, sequelize, tableName, attributes, o
...
@@ -61,6 +61,12 @@ exports.SequelizeTable = function(Sequelize, sequelize, tableName, attributes, o
association
.
table
.
attributes
[
assocNameAsTableIdentifier
]
=
{
type
:
Sequelize
.
INTEGER
}
association
.
table
.
attributes
[
assocNameAsTableIdentifier
]
=
{
type
:
Sequelize
.
INTEGER
}
}
}
break
break
case
'hasAndBelongsToMany'
:
var
_attributes
=
{}
_attributes
[
assocNameAsTableIdentifier
]
=
Sequelize
.
INTEGER
_attributes
[
Sequelize
.
Helper
.
SQL
.
asTableIdentifier
(
association
.
table
.
tableName
)]
=
Sequelize
.
INTEGER
sequelize
.
define
(
Sequelize
.
Helper
.
SQL
.
manyToManyTableName
(
association
.
name
,
association
.
table
.
tableName
),
_attributes
)
break
case
'hasOne'
:
case
'hasOne'
:
// adds the foreign key to the associated table
// adds the foreign key to the associated table
// e.g. assocTable.myTableId = Sequelize.INTEGER
// e.g. assocTable.myTableId = Sequelize.INTEGER
...
@@ -134,22 +140,44 @@ exports.SequelizeTable = function(Sequelize, sequelize, tableName, attributes, o
...
@@ -134,22 +140,44 @@ exports.SequelizeTable = function(Sequelize, sequelize, tableName, attributes, o
return
object
return
object
},
},
hasMany
:
function
(
assocName
,
_table
,
backAssocName
)
{
hasAndBelongsToMany
:
function
(
assocName
)
{
var
Factory
=
new
require
(
"./Factory"
).
Factory
(
Sequelize
,
sequelize
),
if
(
typeof
assocName
==
'undefined'
)
association
=
{
name
:
assocName
,
backAssociationName
:
backAssocName
,
table
:
_table
,
type
:
'hasMany'
}
throw
new
Error
(
'Please specify at least an association name!'
)
var
association
=
{
name
:
assocName
,
table
:
table
,
type
:
'hasAndBelongsToMany'
},
Factory
=
new
require
(
"./Factory"
).
Factory
(
Sequelize
,
sequelize
)
table
.
associations
.
push
(
association
)
table
.
associations
.
push
(
association
)
if
(
backAssocName
)
{
table
.
prototype
[
Sequelize
.
Helper
.
SQL
.
addPrefix
(
'get'
,
assocName
)]
=
Factory
.
createManyToManyGetter
(
null
,
table
,
assocName
,
table
.
tableName
)
Factory
.
addManyToManyMethods
(
table
,
_table
,
assocName
,
backAssocName
)
table
.
prototype
[
Sequelize
.
Helper
.
SQL
.
addPrefix
(
'set'
,
assocName
)]
=
Factory
.
createManyToManySetter
(
table
,
table
,
assocName
,
table
.
tableName
)
Factory
.
addManyToManyMethods
(
_table
,
table
,
backAssocName
,
assocName
)
}
else
{
Factory
.
addOneToManyMethods
(
table
,
_table
,
assocName
)
}
return
association
return
association
},
},
hasMany
:
function
(
assocName
,
associateWith
,
backAssocName
)
{
if
(
typeof
assocName
==
'undefined'
)
throw
new
Error
(
'Please specify at least an association name!'
)
if
(
associateWith
)
{
var
Factory
=
new
require
(
"./Factory"
).
Factory
(
Sequelize
,
sequelize
),
association
=
{
name
:
assocName
,
backAssociationName
:
backAssocName
,
table
:
associateWith
,
type
:
'hasMany'
}
table
.
associations
.
push
(
association
)
if
(
backAssocName
)
{
Factory
.
addManyToManyMethods
(
table
,
associateWith
,
assocName
,
backAssocName
)
Factory
.
addManyToManyMethods
(
associateWith
,
table
,
backAssocName
,
assocName
)
}
else
{
Factory
.
addOneToManyMethods
(
table
,
associateWith
,
assocName
)
}
return
association
}
else
{
return
this
.
hasAndBelongsToMany
(
assocName
)
}
},
hasOne
:
function
(
assocName
,
_table
)
{
hasOne
:
function
(
assocName
,
_table
)
{
var
Factory
=
new
require
(
"./Factory"
).
Factory
(
Sequelize
,
sequelize
),
var
Factory
=
new
require
(
"./Factory"
).
Factory
(
Sequelize
,
sequelize
),
association
=
{
name
:
assocName
,
table
:
_table
,
type
:
'hasOne'
}
association
=
{
name
:
assocName
,
table
:
_table
,
type
:
'hasOne'
}
...
@@ -242,6 +270,10 @@ exports.SequelizeTable = function(Sequelize, sequelize, tableName, attributes, o
...
@@ -242,6 +270,10 @@ exports.SequelizeTable = function(Sequelize, sequelize, tableName, attributes, o
return
this
.
invalidFields
.
length
==
0
return
this
.
invalidFields
.
length
==
0
},
},
get
isNewRecord
()
{
return
this
.
id
==
null
},
save
:
function
(
callback
)
{
save
:
function
(
callback
)
{
var
query
=
null
,
var
query
=
null
,
self
=
this
self
=
this
...
@@ -252,7 +284,7 @@ exports.SequelizeTable = function(Sequelize, sequelize, tableName, attributes, o
...
@@ -252,7 +284,7 @@ exports.SequelizeTable = function(Sequelize, sequelize, tableName, attributes, o
}
}
this
.
updatedAt
=
new
Date
()
this
.
updatedAt
=
new
Date
()
if
(
this
.
i
d
==
null
)
{
if
(
this
.
i
sNewRecord
)
{
this
.
createdAt
=
new
Date
()
this
.
createdAt
=
new
Date
()
query
=
Sequelize
.
sqlQueryFor
(
'insert'
,
{
query
=
Sequelize
.
sqlQueryFor
(
'insert'
,
{
...
...
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