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
不要怂,就是干,撸起袖子干!
You need to sign in or sign up before continuing.
Commit 82120c5d
authored
Sep 22, 2010
by
Sascha Depold
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
always force the refetch of associated data when setting them; one-to-many association storage works
1 parent
7eeccd0c
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
36 additions
and
11 deletions
lib/sequelize/Factory.js
lib/sequelize/Factory.js
View file @
82120c5
...
...
@@ -10,7 +10,7 @@ module.exports.Factory = function(Sequelize, sequelize) {
getterName
=
Sequelize
.
Helper
.
SQL
.
addPrefix
(
'get'
,
methodName
||
assocName
)
table1
.
prototype
[
setterName
]
=
Factory
.
createOneToManySetter
(
table1
,
assocName
,
methodName
)
table1
.
prototype
[
getterName
]
=
Factory
.
createOneToManyGetter
(
table1
,
table2
,
assocName
)
table1
.
prototype
[
getterName
]
=
Factory
.
createOneToManyGetter
(
table1
,
table2
,
assocName
,
methodName
)
},
addOneToOneMethods
:
function
(
table1
,
table2
,
assocName
,
backAssocName
)
{
...
...
@@ -51,7 +51,12 @@ module.exports.Factory = function(Sequelize, sequelize) {
},
createManyToManyGetter
:
function
(
table1
,
table2
,
assocName
,
backAssocName
)
{
return
function
(
callback
)
{
return
function
(
options
,
callback
)
{
var
_callback
=
((
typeof
options
==
'object'
)
?
callback
:
options
),
_options
=
(
typeof
options
==
'object'
)
?
options
:
{},
self
=
this
if
(
_options
.
force
||
!
this
.
hasFetchedAssociationFor
(
assocName
))
{
var
Association
=
sequelize
.
tables
[
Sequelize
.
Helper
.
SQL
.
manyToManyTableName
(
assocName
,
backAssocName
)].
klass
,
assocNameAsTableIdentifier
=
Sequelize
.
Helper
.
SQL
.
asTableIdentifier
(
backAssocName
),
whereConditions
=
[
assocNameAsTableIdentifier
,
this
.
id
].
join
(
"="
)
...
...
@@ -62,11 +67,17 @@ module.exports.Factory = function(Sequelize, sequelize) {
return
resultSet
[
Sequelize
.
Helper
.
SQL
.
asTableIdentifier
(
assocName
)]
})
table2
.
findAll
({
where
:
"id IN ("
+
ids
.
join
(
","
)
+
")"
},
callback
)
table2
.
findAll
({
where
:
"id IN ("
+
ids
.
join
(
","
)
+
")"
},
function
(
objects
)
{
// self.fetchedAssociations[assocName] =
if
(
_callback
)
_callback
(
objects
)
})
}
else
{
if
(
callback
)
callback
([])
if
(
_callback
)
_
callback
([])
}
})
}
else
{
if
(
_callback
)
_callback
(
this
.
fetchedAssociations
[
assocName
])
}
}
},
createManyToManySetter
:
function
(
table1
,
table2
,
assocName
,
backAssocName
)
{
...
...
@@ -123,20 +134,34 @@ module.exports.Factory = function(Sequelize, sequelize) {
})
}
},
createOneToManyGetter
:
function
(
table1
,
table2
,
assocName
)
{
return
function
(
callback
)
{
createOneToManyGetter
:
function
(
table1
,
table2
,
assocName
,
methodName
)
{
return
function
(
options
,
callback
)
{
var
_callback
=
((
typeof
options
==
'object'
)
?
callback
:
options
),
_options
=
(
typeof
options
==
'object'
)
?
options
:
{},
accessKey
=
methodName
||
assocName
,
self
=
this
if
(
_options
.
refetchAssociations
||
!
this
.
hasFetchedAssociationFor
(
accessKey
))
{
var
whereConditions
=
[
Sequelize
.
Helper
.
SQL
.
asTableIdentifier
(
assocName
),
this
.
id
].
join
(
"="
)
table2
.
findAll
({
where
:
whereConditions
},
callback
)
table2
.
findAll
({
where
:
whereConditions
},
function
(
result
)
{
self
.
setAssociationDataFor
(
accessKey
,
result
)
if
(
_callback
)
_callback
(
result
)
})
}
else
{
var
result
=
self
.
getAssociationDataFor
(
accessKey
)
if
(
_callback
)
_callback
(
result
)
}
}
},
createOneToManySetter
:
function
(
table1
,
assocName
,
methodName
)
{
return
function
(
objects
,
callback
)
{
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
}),
getterName
=
Sequelize
.
Helper
.
SQL
.
addPrefix
(
'get'
,
methodName
)
this
[
Sequelize
.
Helper
.
SQL
.
addPrefix
(
'get'
,
methodName
)](
function
(
currentAssociations
)
{
this
[
getterName
]({
refetchAssociations
:
true
},
function
(
currentAssociations
)
{
var
currentIds
=
Sequelize
.
Helper
.
Array
.
map
(
currentAssociations
,
function
(
assoc
)
{
return
assoc
.
id
}),
obsoleteAssociations
=
Sequelize
.
Helper
.
Array
.
select
(
currentAssociations
,
function
(
assoc
)
{
return
object
s
Ids
.
indexOf
(
assoc
.
id
)
==
-
1
}),
obsoleteAssociations
=
Sequelize
.
Helper
.
Array
.
select
(
currentAssociations
,
function
(
assoc
)
{
return
objectIds
.
indexOf
(
assoc
.
id
)
==
-
1
}),
queries
=
[]
obsoleteAssociations
.
forEach
(
function
(
assoc
)
{
...
...
@@ -153,7 +178,7 @@ module.exports.Factory = function(Sequelize, sequelize) {
})
Sequelize
.
chainQueries
(
queries
,
function
()
{
self
[
Sequelize
.
Helper
.
SQL
.
addPrefix
(
'get'
,
methodName
)](
callback
)
self
[
getterName
]({
refetchAssociations
:
true
},
callback
)
})
})
}
...
...
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