Re: filtering objects in relation

From: martin ruff (mruf..imeon.ch)
Date: Tue Dec 03 2002 - 11:06:36 EST

  • Next message: Andrei Adamchik: "Re: filtering objects in relation"

    Hi Craig,
    thanks a lot this works, but I wonder if there could be another
    solution, one with which I have only to generate one query and not
    iterate over the results of my first query and for every entry do a new
      query
    what I mean is somethins like an additional expresion
    expressionList.add(ExpressionFactory. binaryPathExp(
                                            Expression.GREATER_THAN_EQUAL_TO,
                                               "toTaskArray",
                                               begindate));
    where toTaskArray is the relation to my taksk from the projects, so if I
    call myproject.getToTaskArray cayenne would return only the objects that
    qualify for the expression above, this would be very nice and a lot less
    code for me to write.
    but thanks again it works, but I'm still curious, if my idea above is
    somehow applicable with cayenne
    martin

    >If you have a project, you could find all the tasks for that project >that
    >are in a given time range by using the following to create an >expression,
    >assuming that "Tasks" has a to-one relationship to "Projects" named
    >"project"
    >List expressionList=new ArrayList();
    >expressionList.add(ExpressionFactory. binaryPathExp(
    > >Expression.LESS_THAN_EQUAL_TO,
    > "displaybegin",
    > currentdate));
    >expressionList.add(ExpressionFactory. binaryPathExp(
    >
    >Expression.GREATER_THAN_EQUAL_TO,
    > "displayend",
    > currentdate));
    >expressionList.add(ExpressionFactory. binaryPathExp(
    >
    >Expression.GREATER_THAN_EQUAL_TO,
    > "project",
    > project));
    >Expression fullExpression=ExpressionFactory.joinExp(Expression.AND,
    >expresionList);
    >
    >And then execute a query on Tasks with this expression.
    >
    >I hope this helps,
    >Craig

    On Mon, 2 Dec 2002, martin ruff wrote:

    >> Hi,
    >> I've go the following two tables:
    >> CREATE TABLE projects(
    >> id serial NOT NULL PRIMARY KEY,
    >> name varchar(100) NOT NULL UNIQUE,
    >> displaybegin date,
    >> displayend date,);
    >>
    >> and
    >>
    >> CREATE TABLE tasks(
    >> id serial NOT NULL PRIMARY KEY,
    >> fk_projectid int4 NOT NULL,
    >> sortorder numeric,
    >> displaybegin date,
    >> displayend date,
    >> FOREIGN KEY (fk_projectid) REFERENCES projects (id));
    >>
    >> now I want to get all projects in a time range, i.e. all projects that
    >> are visible today (this works) and for every project its tasks also in a
    >> given time range:
    >> I defined a relation from projects to tasks (tasksArray)
    >> then I have to following expression:
    >> At least I think that would work :-=
    >> query.addOrdering("name", true);
    >> projects = theDataContext.performQuery(query);
    >>
    >> this returns me all projects that are visible today, works fine:
    >> But how can I set the filter for the related tasks?
    >> the relation taksk is defined as:
    >> <db-relationship name="tasksArray" source="projects" target="tasks"
    >> toDependentPK="false" toMany="true">
    >> <db-attribute-pair source="ID" target="FK_projectid"/>
    >> </db-relationship>
    >>
    >> thanks for help
    >> martin
    >>
    >>

    >Craig Miskell
    >Programmer, Black Albatross, Otago University, New Zealand
    >-----BEGIN GEEK CODE BLOCK-----
    >Version: 3.1
    >GCS d- s+:- a-->? C++++(++)$ ULXH+++$>++++ P+>++++ L++$>++++$ E--- >W+++$
    >N+ K? w--- !O M-- V? PS--- PE Y t++ 5 X+++ R-- tv+ b+>+++ DI++++ D+ G+
    >e++
    >h--- r+++ y+++
    >------END GEEK CODE BLOCK------



    This archive was generated by hypermail 2.0.0 : Tue Dec 03 2002 - 11:06:42 EST