4
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Collectionから特定のModelを取得してくる

Posted at

今回やったこと

  • queryで条件を指定してデータを取得してくる

ソースコード

app/controllers/index.js
var library = Alloy.Collections.user;
library.fetch();

$.word.addEventListener('change', function(e){
  Ti.API.debug(e);
  var property = $.property.getValue();
  library.searchByName(property, e.value);
});
app/models/user.js
extendCollection: function(Collection) {
  _.extend(Collection.prototype, {
    // extended functions and properties go here
    searchByName: function(property, word){
      var q = "SELECT * FROM " + this.config.adapter.collection_name + " WHERE " + property + " LIKE '\%" + word + "\%'";
      this.fetch({query: q});
    }
  });

  return Collection;
}

前回まではextendModelの中の_.extend内に色々書いてましたが,今回はModel(レコード)に対してではなくCollection(テーブル)に対して条件指定して絞り込みを行うのでextendCollection内に記述します.

問題点

ソースコードを見ればわかりますが,もろにSQLインジェクションを受けてしまいます.
SQLインジェクション対策としてlibrary.fetch({query: { statement: 'SELECT * from ' + table + ' where author = ?', params: [searchAuthor] }}); // 公式ドキュメントよりのようにqueryをstatementとparamsにわければ良いと書かれていたのでやってみたところ,うまくいかなかったので現在模索中.

4
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?