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

Is it possible to filter related entities with dynamic conditions? #247

Open
nenadmilosavljevic opened this issue Oct 14, 2017 · 4 comments
Labels

Comments

@nenadmilosavljevic
Copy link

Hello, I know that I can filter related entities using this:

public static function relations(MapperInterface $mapper, EntityInterface $entity)
{
    return [
        "english" => $mapper
                        ->hasMany($entity, "Message", "parent_id")
                        ->where(["language" => "en"]),
        "estonian" => $mapper
                        ->hasMany($entity, "Message", "parent_id")
                        ->where(["language" => "et")
    ];
}

Id' like to do something like ->where(["userId" => $userId) but userId is read dinamically each time.

@FlipEverything
Copy link
Contributor

You can use php's $_SESSION variable for that.

->where(["language" => isset($_SESSION["user"]["language"]) ? $_SESSION["user"]["language"] : "en"])

@vlucas
Copy link
Collaborator

vlucas commented Oct 16, 2017

@nenadmilosavljevic You can add more query conditions on your relations dynamically when calling them too, i.e.:

$page->translations->where(['language' => $user->lang]);

You can then put this in to a helper function or something (anything you want really):

function getPageTranslationForUser(Page $page, User $user) {
    return $page->translations->where(['language' => $user->lang]);
}

@nenadmilosavljevic
Copy link
Author

Does this mean that if I want to get all pages for specific language I have to querry db for each page?
What is $page exactly?

@vlucas
Copy link
Collaborator

vlucas commented Nov 6, 2017

$page doesn't matter - it was just an example of getting page content specific to that user, so both the page and user are required. That can be literally any entity.

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

No branches or pull requests

3 participants