정확히 이해가 안되는데 selected를 모두 100으로 바꿨는데 어떤 모델은 10이고 어떤 모델은 100이라는 말인가요?
그리고 hero는 model인가요? collection 인가요? model이면 length 속성은 없고 collection이면 save 속성이 없는데 설명에는 둘다 있어서 헷갈립니다.
이상하군요. 저는 잘됩니다. 혹시 model 선언한 코드 전체를 공유해 줄수 있으면 원인을 알수도 있을 것 같아요.
그리고 sql 아답터 쓸때 주의할 점은 이건 db이다보니 config의 adapter나 colums을 변경할 경우 앱을 삭제후 다시 깔거나 migration으로 해당 부분을 커버해줘야합니다.
모델 파일 생성한 후, idAttribute를 지정하지 않은 채로 앱을 한번 실행했던건 아니신가요? 그 이후에 idAttribute를 지정해도 이게 db에서는 primary key로 동작 안합니다. (이미 만들어진 alloy_id가 key로 동작합니다.)
(그래서 저는 이런 이유로 앱에 데이터를 따로 저장해야할때는 property 아답터를 선호합니다.)
혹시나 해서 아래와 같이 간단히 만든 후에 테스트 해봤는데도 잘되요.
exports.definition = {
config: {
columns: {
"selected": "int",
"h_id": "string"
},
adapter: {
type: "sql",
collection_name: "car",
idAttribute : 'h_id'
}
},
extendModel: function(Model) {
_.extend(Model.prototype, {
// extended functions and properties go here
});
return Model;
},
extendCollection: function(Collection) {
_.extend(Collection.prototype, {
// extended functions and properties go here
});
return Collection;
}
};
exports.definition = {
config: {
columns: {
"h_id": "text",
"national": "text",
"updateDate": "text",
"selected": "int"
},
adapter: {
type: "sql",
collection_name: "hero",
idAttribute: "h_id"
}
},
extendModel: function(Model) {
_.extend(Model.prototype, {
// extended functions and properties go here
});
return Model;
},
extendCollection: function(Collection) {
_.extend(Collection.prototype, {
// extended functions and properties go here
});
return Collection;
}
};
업데이트 했던 방법
var hid = 12;
var q = "SELECT * FROM hero WHERE h_id = " + hid;
var hero = Alloy.Collections.instance('hero');
hero.fetch({ query : q });
var h = hero.at(0);
h.save({selected : 100 });
hid로 모델을 컬렉션에 찾아서 selected를 100으로 저장합니다.
그러면 h자체는 저장이 잘되었는데 쿼리를 통해 검색해서 확인하면 업데이트가 안되어있습니다.
해결습니다.;;
모델에 fetch를 주니 해결이 되네요.
머 사실 아직도 잘 이해가 안되지만 일단 해결했으니 완성후 다시 살펴봐야겠습니다.
var hid = 12;
var q = "SELECT * FROM hero WHERE h_id = " + hid;
var hero = Alloy.Collections.instance('hero');
hero.fetch({ query : q });
var h = hero.at(0);
h.set({selected : 100 }).save();
h.fetch();