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 b89572f0
authored
Apr 15, 2011
by
Sascha Depold
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fixed null result for belongsTo
1 parent
49723bf5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
50 additions
and
7 deletions
lib/sequelize/associations/belongs-to.js
lib/sequelize/model-definition.js
lib/sequelize/null-emitter.js
lib/sequelize/utils.js
test/Model/belongsTo.js
lib/sequelize/associations/belongs-to.js
View file @
b89572f
...
...
@@ -25,7 +25,8 @@ BelongsTo.prototype.injectGetter = function(obj) {
var
self
=
this
obj
[
Utils
.
_
.
camelize
(
'get_'
+
this
.
associationName
)]
=
function
()
{
return
self
.
target
.
find
(
obj
[
self
.
identifier
])
var
id
=
obj
[
self
.
identifier
]
return
self
.
target
.
find
(
id
)
}
return
this
...
...
@@ -35,7 +36,7 @@ BelongsTo.prototype.injectSetter = function(obj) {
var
self
=
this
obj
[
Utils
.
_
.
camelize
(
'set_'
+
this
.
associationName
)]
=
function
(
associatedObject
)
{
obj
[
self
.
identifier
]
=
associatedObject
.
id
obj
[
self
.
identifier
]
=
associatedObject
?
associatedObject
.
id
:
null
return
obj
.
save
()
}
...
...
lib/sequelize/model-definition.js
View file @
b89572f
...
...
@@ -40,7 +40,8 @@ ModelDefinition.prototype.addDefaultAttributes = function() {
ModelDefinition
.
prototype
.
query
=
function
()
{
var
args
=
Utils
.
_
.
map
(
arguments
,
function
(
arg
,
_
)
{
return
arg
})
,
s
=
this
.
modelManager
.
sequelize
// add this as the second argument
if
(
arguments
.
length
==
1
)
args
.
push
(
this
)
return
s
.
query
.
apply
(
s
,
args
)
}
...
...
@@ -77,8 +78,7 @@ ModelDefinition.prototype.find = function(options) {
// options is not a hash but an id
if
(
typeof
options
==
'number'
)
options
=
{
where
:
options
}
else
{
if
(
Utils
.
argsArePrimaryKeys
(
arguments
,
this
.
primaryKeys
))
{
else
if
(
Utils
.
argsArePrimaryKeys
(
arguments
,
this
.
primaryKeys
))
{
var
where
=
{}
,
self
=
this
...
...
@@ -88,7 +88,9 @@ ModelDefinition.prototype.find = function(options) {
})
options
=
{
where
:
where
}
}
}
else
if
((
options
==
null
)
||
(
options
==
undefined
))
{
var
NullEmitter
=
require
(
"./null-emitter"
)
return
new
NullEmitter
()
}
options
.
limit
=
1
...
...
lib/sequelize/null-emitter.js
0 → 100644
View file @
b89572f
var
Utils
=
require
(
"./utils"
)
var
NullEmitter
=
module
.
exports
=
function
(
delay
)
{
var
self
=
this
delay
=
delay
||
10
setTimeout
(
function
()
{
self
.
emitNull
()
},
delay
)
}
Utils
.
addEventEmitter
(
NullEmitter
)
NullEmitter
.
prototype
.
emitNull
=
function
()
{
this
.
emit
(
'success'
,
null
)
this
.
emit
(
'failure'
,
null
)
}
\ No newline at end of file
lib/sequelize/utils.js
View file @
b89572f
var
client
=
new
(
require
(
"mysql"
).
Client
)()
var
Utils
=
module
.
exports
=
{
_
:
(
function
()
{
var
_
=
require
(
"underscore"
);
...
...
test/Model/belongsTo.js
View file @
b89572f
...
...
@@ -48,5 +48,31 @@ module.exports = {
})
})
})
},
'it should correctly delete associations'
:
function
(
exit
)
{
var
User
=
sequelize
.
define
(
'User'
+
parseInt
(
Math
.
random
()
*
99999999
),
{
username
:
Sequelize
.
STRING
})
var
Task
=
sequelize
.
define
(
'Task'
+
parseInt
(
Math
.
random
()
*
99999999
),
{
title
:
Sequelize
.
STRING
})
User
.
belongsTo
(
'task'
,
Task
)
User
.
sync
({
force
:
true
}).
on
(
'success'
,
function
()
{
Task
.
sync
({
force
:
true
}).
on
(
'success'
,
function
()
{
User
.
create
({
username
:
'asd'
}).
on
(
'success'
,
function
(
u
)
{
Task
.
create
({
title
:
'a task'
}).
on
(
'success'
,
function
(
t
)
{
u
.
setTask
(
t
).
on
(
'success'
,
function
()
{
u
.
getTask
().
on
(
'success'
,
function
(
task
)
{
assert
.
eql
(
task
.
title
,
'a task'
)
u
.
setTask
(
null
).
on
(
'success'
,
function
()
{
u
.
getTask
().
on
(
'success'
,
function
(
task
)
{
assert
.
isNull
(
task
)
exit
(
function
(){})
})
})
})
})
})
})
})
})
}
}
\ No newline at end of file
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