Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Best practice to search the values in direct associations #445

Closed
phicdy opened this issue Sep 26, 2018 · 3 comments
Closed

Best practice to search the values in direct associations #445

phicdy opened this issue Sep 26, 2018 · 3 comments

Comments

@phicdy
Copy link

phicdy commented Sep 26, 2018

For example, when I'd like to search the books that publisher's name is something, what is the best practice ?

@Table
class Publisher {
  @PrimaryKey
  val id: Long

  @Column
  val name: String
}

@Table
class Book {

    @PrimaryKey
    val id: Long

    @column
    val title: String

    @Column
    val publisher: Publisher
}

My way is here.

val searchWord = "hogehoge"
orma.selectFromBook()
    .where(Publisher_Schema.INSTANCE.name.name + """ like "$searchWord" """)
    .executeAsObservable()
    .toList()
    .subscribeOn(Schedulers.io())
@k-kagurazaka
Copy link
Member

If an entity have a direct association, Orma generates a query builder for the association.

In the case of your example, try the following code.

val searchWord = "hogehoge"
orma.selectFromBook()
    .publisher { it.nameLike(searchWord) }
    .executeAsObservable()
    .toList()
    .subscribeOn(Schedulers.io())

This feature is available since Orma v5.0.0.

@phicdy
Copy link
Author

phicdy commented Oct 1, 2018

@k-kagurazaka Great. Thank you for the code.

@phicdy phicdy closed this as completed Oct 1, 2018
@phicdy
Copy link
Author

phicdy commented Nov 13, 2018

After updated to v5.1.2, I couldn't use this way, but I found the solution. The target column requires @Column(index = true).

@Table
class Publisher {
  @PrimaryKey
  val id: Long

  @Column(index = true)
  val name: String
}

@Table
class Book {

    @PrimaryKey
    val id: Long

    @column
    val title: String

    @Column(index = true)
    val publisher: Publisher
}

Then, we can use the codes below.

val searchWord = "hogehoge"
orma.selectFromBook()
    .publisher { it.nameLike(searchWord) }
    .executeAsObservable()
    .toList()
    .subscribeOn(Schedulers.io())

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants