0

I have a many-to-many relation, having P, PF and F I want to filter P using F through PF. Like:

final query = Query<P>(context)
      ..where( (p)=>p.pfSet.firstWhere( (pf)=>pf.f.cod == 1 ).f ).isNotNull();

and the classes:

class P extends ManagedObject<_P> implements _P {}
class _P{
  @primaryKey
  int cod;
  ...

  ManagedSet<ProdutoFilial> pfSet;
}

class PF extends ManagedObject<_PF> implements _PF {}
class _PF{

  @primaryKey
  int cod;
  @(Relate #pfSet)
  P p;
  @(Relate #pfSet)
  F f;
  bool active;    
}

class F extends ManagedObject<_F> implements _F {}
class _F{

  @primaryKey
  int cod;
  ...
  ManagedSet<ProdutoFilial> pfSet;

}

How can I filter this?

4

2 回答 2

0

可以使用将保留的自己的查询where

..predicate = QueryPredicate(
          " exists (select f.cod from pf where pf.fcod = @fcod and pf.pcod = p.cod) ",
          { "fcod": 1 });

如果您join在代码查询中使用表名将更改。

于 2019-02-15T18:02:41.190 回答
0

QueryPredicate 不是一个很好的解决方案,因为您无法在连接表中设置条件(我们不知道连接表的别名)。我创建了自己的程序 - 试试看:

    QueryExpression<dynamic, dynamic> RelationWhereExt(Query query, List<String> links) {
    ManagedEntity e = query.entity;
    KeyPath path;

    ManagedPropertyDescription property;
    links.forEach((link) {
      property = e.properties[link];
      if (path==null){
        // рутовое условие
        path = KeyPath(property);
      } else {
        // следующие условия
        path.add(property);
      }

      if (property is ManagedRelationshipDescription) {
        e = (property as ManagedRelationshipDescription).destinationEntity; // меняем entity
      }
    });

    QueryExpression<dynamic, dynamic> queryExpression = QueryExpression<dynamic, dynamic>(path);
    (query as MySqlQuery).expressions.add(queryExpression);

    return queryExpression;
  }

在你的情况下: RelationWhereExt(query, ['pfSet', 'f']).isNotNull();

于 2021-04-05T14:18:28.020 回答