2
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.

TreeFrogのORMオブジェクト読み込み

Last updated at Posted at 2014-06-30

TreeFrogのORMオブジェクトの基本的な読み込み方法です。
以下のようなblogテーブルから tspawn s blog で生成したモデルを想定しています。

Field Type Extra
id int(11) auto_
title varchar(20)
body varchar(200)
created_at timestamp
updated_at timestamp
lock_revision int(11)

##全モデルを取得

QList<Blog> blogList = Blog::getAll();

##主キーで1レコードを取得

TSqlORMapper<BlogObject> mapper;
Blog blog = Blog::get(id);

###モデルが見つからない場合、404ページを表示

TSqlORMapper<BlogObject> mapper;
Blog blog = Blog::get(id);
if (blog.isNull()) {
    renderErrorResponse(404);
    return;
}

##TCriteriaを使用してクエリーする

TSqlORMapper<BlogObject> mapper;
TCriteria crt(BlogObject::Title, "hoge");
BlogObject blog = mapper.findFirst(crt);

##条件を追加する

TSqlORMapper<BlogObject> mapper;
TCriteria crt(BlogObject::Title, "hoge");
crt.add(BlogObject::Body, TSql::NotEqual , "");
BlogObject blog = mapper.findFirst(crt);

##OR

TSqlORMapper<BlogObject> mapper;
TCriteria crt(BlogObject::Title, "hoge");
crt.addOr(BlogObject::Body, "piyo");
BlogObject blog = mapper.findFirst(crt);

##BETWEEN

TSqlORMapper<BlogObject> mapper;
QDateTime from = QDateTime::fromString("2014-06-27 17:21:40", Qt::ISODate);
QDateTime to = QDateTime::fromString("2014-06-27 17:21:50", Qt::ISODate);
TCriteria crt(BlogObject::CreatedAt, TSql::Between, from, to);
BlogObject blog = mapper.findFirst(crt);

##NOT BETWEEN

TSqlORMapper<BlogObject> mapper;
QDateTime from = QDateTime::fromString("2014-06-27 17:21:40", Qt::ISODate);
QDateTime to = QDateTime::fromString("2014-06-27 17:21:50", Qt::ISODate);
TCriteria crt(BlogObject::CreatedAt, TSql::NotBetween, from, to);
BlogObject blog = mapper.findFirst(crt);

##IN

TSqlORMapper<BlogObject> mapper;
QList<QVariant> list;
list << "hoge" << "piyo";
TCriteria crt(BlogObject::Title, TSql::In, list);
BlogObject blog = mapper.findFirst(crt);

##NULL

TSqlORMapper<BlogObject> mapper;
TCriteria crt(BlogObject::Title, TSql::IsNull);
BlogObject blog = mapper.findFirst(crt);

##ORDER BY

TSqlORMapper<ReceiptObject> mapper;
mapper.setSortOrder(BlogObject::CreatedAt, Tf::DescendingOrder);
BlogObject blog = mapper.findFirst();

##OFFSETとLIMIT

TSqlORMapper<ReceiptObject> mapper;
mapper.setOffset(1);
mapper.setLimit(1);
BlogObject blog = mapper.findFirst();

##TCriteriaを使用して集計を行う
###count

TSqlORMapper<BlogObject> mapper;
TCriteria cri(BlogObject::Title, "Hello world");
int count = mapper.find(cri);

TCriteriaで必要なクエリーを生成できない場合、 TSqlQueryを使用する。

##直接SQL文を使用

TSqlQuery query;
query.prepare("select max(id) from blog");
query.exec(); // クエリ実行
query.next();
int cnt = query.value(0).toInt();
2
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
2
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?