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

The relationship method on a undefined method should return an alias of the relation type #9

Closed
FCO opened this issue Aug 18, 2018 · 8 comments
Labels
help wanted Extra attention is needed question Further information is requested

Comments

@FCO
Copy link
Owner

FCO commented Aug 18, 2018

model Post {
   has UInt $!author-id is referencing{ Person.id };
   has Person $.author = .^relates: $.author-id;
}

Post.author; # returns a Person alias named author
Post.author.id; # column id from alias author from Person

Post.where: { .author.name eq FCO };
@FCO FCO mentioned this issue Aug 18, 2018
@FCO
Copy link
Owner Author

FCO commented Aug 21, 2018

if its an Array, its returning a FakeSeq

@FCO
Copy link
Owner Author

FCO commented Aug 23, 2018

Not a FakeSeq, but a ResultSeq

@FCO
Copy link
Owner Author

FCO commented Aug 23, 2018

model Post {
   has UInt   $!author-id is referencing{ Person.id };
   has Person @.author    is relationship{ .author-id };
}

Post.author;             # returns a Person::ResultSeq :of() an alias named author
Post.author.map( *.id ); # Seq of UInt

Post.all.grep: { .author.name eq FCO };

Post.new.author;         # returns a Person::ResultSet

@FCO FCO added help wanted Extra attention is needed question Further information is requested labels Aug 23, 2018
@FCO
Copy link
Owner Author

FCO commented Aug 24, 2018

How can I know if I should continue on the ResultSeq chain or if I have to get the values and return a true Seq?

@FCO
Copy link
Owner Author

FCO commented Aug 24, 2018

The of of the ResultSeq should define if its only creating the query or if it should run the query and iterate over the results

Person.all                            # returns an Person::ResultSeq (:of(Person))
   .grep({ .name.starts-with("F") })  # adds an "WHERE name like 'F%'" on the query
                                      # and return another Person::ResultSeq
   .flatmap( *.posts )                # now it returns a Post::ResultSeq
                                      # adding a join with the Person
   .grep( *.title.chars == 5 )        # adds "WHERE LENGTH(title) = 5"
   .map( *.body )                     # returns an Red::Default::ResultSeq of Str
                                      # Str doesn't Model, so the next call
                                      # will run the query
   .elems
;
SELECT
   post.body
FROM
   person JOIN post ON(post.author_id = person.id)
WHERE
   person.name like 'F%'
AND
   LENGTH(post.title) = 5
;

@FCO
Copy link
Owner Author

FCO commented Sep 13, 2018

It's now possible to call the do-it method on a ResultSeq to return a DoneResultSeq that will run the query on calling iterator method

@FCO
Copy link
Owner Author

FCO commented Dec 19, 2018

if you call .Seq on a ResultSeq it will return a Seq that will run the query

@FCO
Copy link
Owner Author

FCO commented Dec 19, 2018

but its automatic if you try to consume the seq

@FCO FCO closed this as completed Jan 1, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed question Further information is requested
Projects
None yet
Development

No branches or pull requests

1 participant