Use find_related in place of find_cake

This commit is contained in:
Chris Tsang 2021-06-28 01:34:49 +08:00
parent 688891f706
commit 9f93c7ed6f
9 changed files with 16 additions and 40 deletions

View File

@ -72,14 +72,4 @@ impl Related<super::filling::Entity> for Entity {
}
}
impl Model {
pub fn find_fruit(&self) -> Select<super::fruit::Entity> {
Entity::find_related().belongs_to::<Entity>(self)
}
pub fn find_filling(&self) -> Select<super::filling::Entity> {
Entity::find_related().belongs_to::<Entity>(self)
}
}
impl ActiveModelBehavior for ActiveModel {}

View File

@ -62,10 +62,4 @@ impl Related<super::cake::Entity> for Entity {
}
}
impl Model {
pub fn find_cake(&self) -> Select<super::cake::Entity> {
Entity::find_related().belongs_to::<Entity>(self)
}
}
impl ActiveModelBehavior for ActiveModel {}

View File

@ -159,7 +159,7 @@ async fn find_many_to_many(db: &DbConn) -> Result<(), QueryErr> {
let cheese = Cake::find_by_id(1).one(db).await?;
if let Some(cheese) = cheese {
let fillings: Vec<filling::Model> = cheese.find_filling().all(db).await?;
let fillings: Vec<filling::Model> = cheese.find_related(Filling).all(db).await?;
println!();
for ff in fillings.iter() {
@ -172,7 +172,7 @@ async fn find_many_to_many(db: &DbConn) -> Result<(), QueryErr> {
let lemon = Filling::find_by_id(2).one(db).await?;
if let Some(lemon) = lemon {
let cakes: Vec<cake::Model> = lemon.find_cake().all(db).await?;
let cakes: Vec<cake::Model> = lemon.find_related(Cake).all(db).await?;
println!();
for cc in cakes.iter() {

View File

@ -119,6 +119,7 @@ impl DatabaseConnection {
None
}
#[cfg(feature = "mock")]
pub fn into_transaction_log(self) -> Vec<Transaction> {
let mut mocker = self.as_mock_connection().get_mocker_mutex().lock().unwrap();
mocker.drain_transaction_log()

View File

@ -1,4 +1,4 @@
use crate::{EntityTrait, QueryResult, TypeErr};
use crate::{Select, EntityTrait, Related, QueryFilter, QueryResult, TypeErr};
pub use sea_query::Value;
use std::fmt::Debug;
@ -8,6 +8,13 @@ pub trait ModelTrait: Clone + Debug {
fn get(&self, c: <Self::Entity as EntityTrait>::Column) -> Value;
fn set(&mut self, c: <Self::Entity as EntityTrait>::Column, v: Value);
fn find_related<R>(&self, _: R) -> Select<R>
where
R: EntityTrait,
Self::Entity: Related<R> {
<Self::Entity as Related<R>>::find_related().belongs_to(self)
}
}
pub trait FromQueryResult {

View File

@ -258,11 +258,11 @@ pub trait QueryFilter: Sized {
}
/// Apply a where condition using the model's primary key
fn belongs_to<E>(mut self, model: &E::Model) -> Self
fn belongs_to<M>(mut self, model: &M) -> Self
where
E: EntityTrait,
M: ModelTrait,
{
for key in E::PrimaryKey::iter() {
for key in <M::Entity as EntityTrait>::PrimaryKey::iter() {
let col = key.into_column();
self = self.filter(col.eq(model.get(col)));
}

View File

@ -62,7 +62,7 @@ where
#[cfg(test)]
mod tests {
use crate::tests_cfg::{cake, filling, fruit};
use crate::{ColumnTrait, EntityTrait, QueryFilter, QueryTrait};
use crate::{ColumnTrait, EntityTrait, QueryFilter, QueryTrait, ModelTrait};
use sea_query::MysqlQueryBuilder;
#[test]
@ -139,7 +139,7 @@ mod tests {
};
assert_eq!(
cake_model.find_fruit().build(MysqlQueryBuilder).to_string(),
cake_model.find_related(fruit::Entity).build(MysqlQueryBuilder).to_string(),
[
"SELECT `fruit`.`id`, `fruit`.`name`, `fruit`.`cake_id` FROM `fruit`",
"INNER JOIN `cake` ON `cake`.`id` = `fruit`.`cake_id`",

View File

@ -73,14 +73,4 @@ impl Related<super::filling::Entity> for Entity {
}
}
impl Model {
pub fn find_fruit(&self) -> Select<super::fruit::Entity> {
Entity::find_related().belongs_to::<Entity>(self)
}
pub fn find_filling(&self) -> Select<super::filling::Entity> {
Entity::find_related().belongs_to::<Entity>(self)
}
}
impl ActiveModelBehavior for ActiveModel {}

View File

@ -63,10 +63,4 @@ impl Related<super::cake::Entity> for Entity {
}
}
impl Model {
pub fn find_cake(&self) -> Select<super::cake::Entity> {
Entity::find_related().belongs_to::<Entity>(self)
}
}
impl ActiveModelBehavior for ActiveModel {}