Filtering queries based on the current user state
up vote
4
down vote
favorite
I have a Symfony2 project. I have an Entity Asset
which can have relations with Category
. I store a categoryCount
-field within the Asset
to determine if an asset has one, to select those fast via a DQL-query depending on the current user state.
Now found myself writing something along the lines of this in my AssetRepository
:
/**
* @param string $orderBy
* @param int $mode
*
* @return DoctrineORMQuery
*/
public function getQueryForAll($orderBy = 'DESC', $mode = self::FILTER_WITHOUT_QUARANTINED)
{
$qb = $this->createQueryBuilder('asset');
$qb->orderBy('asset.updatedAt', $orderBy);
$filter = $this->getQuarantinedCriteria($mode);
if ($filter) {
$qb->addCriteria($filter);
}
return $query = $qb->getQuery();
}
/**
* @param int $mode
*
* @return Criteria|null
*/
protected function getQuarantinedCriteria($mode = self::FILTER_WITHOUT_QUARANTINED) {
$filter = null;
if ($mode === self::FILTER_WITHOUT_QUARANTINED) {
$filter = Criteria::create()
->andWhere(Criteria::expr()->gt('categoryCount', 0));
} else if ($mode === self::FILTER_ONLY_QURANTINED) {
$filter = Criteria::create()
->andWhere(Criteria::expr()->eq('categoryCount', 0));
}
return $filter;
}
This doesn't feel right to me. The $mode
flag triggers my code smell alarm, I also am worrying about passing it into each asset querying method.
Furthermore, since I will have to set the mode depending on the state of the current logged in user, I also am worried that I will produce code duplications by doing a lot of if-user-has-the-rights-checks in my actions or services:
/**
* @Route("/all_assets/{page}",
* name="get_paginated_assets",
* requirements={"page" = "d+"},
* defaults={"page" = "1"},
* options={"expose"=true}
* )
*/
public function getPaginatedAssetActions($page)
{
$repo = $this->getAssetRepository();
$isAllowedToViewRestrictedAssets = $this->getCurrentUser()->isAdmin();
if ($isAllowedToViewRestrictedAssets) {
$mode = EntityAssetRepository::FILTER_SHOW_ALL;
}
$query = $repo->getQueryForAll($mode);
$pagerfanta = new Pagerfanta(new DoctrineORMAdapter($query));
$data = $this->getCurrentPagedResults($pagerfanta, $page);
$view = $this->view();
$view->setFormat('json');
$view->setData($data);
return $view;
}
Is this the right way to implement the logic to enable filters in a Repository? Feedback on whether there is a more decoupled way would be highly appreciated.
php php5 doctrine
bumped to the homepage by Community♦ 13 hours ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
add a comment |
up vote
4
down vote
favorite
I have a Symfony2 project. I have an Entity Asset
which can have relations with Category
. I store a categoryCount
-field within the Asset
to determine if an asset has one, to select those fast via a DQL-query depending on the current user state.
Now found myself writing something along the lines of this in my AssetRepository
:
/**
* @param string $orderBy
* @param int $mode
*
* @return DoctrineORMQuery
*/
public function getQueryForAll($orderBy = 'DESC', $mode = self::FILTER_WITHOUT_QUARANTINED)
{
$qb = $this->createQueryBuilder('asset');
$qb->orderBy('asset.updatedAt', $orderBy);
$filter = $this->getQuarantinedCriteria($mode);
if ($filter) {
$qb->addCriteria($filter);
}
return $query = $qb->getQuery();
}
/**
* @param int $mode
*
* @return Criteria|null
*/
protected function getQuarantinedCriteria($mode = self::FILTER_WITHOUT_QUARANTINED) {
$filter = null;
if ($mode === self::FILTER_WITHOUT_QUARANTINED) {
$filter = Criteria::create()
->andWhere(Criteria::expr()->gt('categoryCount', 0));
} else if ($mode === self::FILTER_ONLY_QURANTINED) {
$filter = Criteria::create()
->andWhere(Criteria::expr()->eq('categoryCount', 0));
}
return $filter;
}
This doesn't feel right to me. The $mode
flag triggers my code smell alarm, I also am worrying about passing it into each asset querying method.
Furthermore, since I will have to set the mode depending on the state of the current logged in user, I also am worried that I will produce code duplications by doing a lot of if-user-has-the-rights-checks in my actions or services:
/**
* @Route("/all_assets/{page}",
* name="get_paginated_assets",
* requirements={"page" = "d+"},
* defaults={"page" = "1"},
* options={"expose"=true}
* )
*/
public function getPaginatedAssetActions($page)
{
$repo = $this->getAssetRepository();
$isAllowedToViewRestrictedAssets = $this->getCurrentUser()->isAdmin();
if ($isAllowedToViewRestrictedAssets) {
$mode = EntityAssetRepository::FILTER_SHOW_ALL;
}
$query = $repo->getQueryForAll($mode);
$pagerfanta = new Pagerfanta(new DoctrineORMAdapter($query));
$data = $this->getCurrentPagedResults($pagerfanta, $page);
$view = $this->view();
$view->setFormat('json');
$view->setData($data);
return $view;
}
Is this the right way to implement the logic to enable filters in a Repository? Feedback on whether there is a more decoupled way would be highly appreciated.
php php5 doctrine
bumped to the homepage by Community♦ 13 hours ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
add a comment |
up vote
4
down vote
favorite
up vote
4
down vote
favorite
I have a Symfony2 project. I have an Entity Asset
which can have relations with Category
. I store a categoryCount
-field within the Asset
to determine if an asset has one, to select those fast via a DQL-query depending on the current user state.
Now found myself writing something along the lines of this in my AssetRepository
:
/**
* @param string $orderBy
* @param int $mode
*
* @return DoctrineORMQuery
*/
public function getQueryForAll($orderBy = 'DESC', $mode = self::FILTER_WITHOUT_QUARANTINED)
{
$qb = $this->createQueryBuilder('asset');
$qb->orderBy('asset.updatedAt', $orderBy);
$filter = $this->getQuarantinedCriteria($mode);
if ($filter) {
$qb->addCriteria($filter);
}
return $query = $qb->getQuery();
}
/**
* @param int $mode
*
* @return Criteria|null
*/
protected function getQuarantinedCriteria($mode = self::FILTER_WITHOUT_QUARANTINED) {
$filter = null;
if ($mode === self::FILTER_WITHOUT_QUARANTINED) {
$filter = Criteria::create()
->andWhere(Criteria::expr()->gt('categoryCount', 0));
} else if ($mode === self::FILTER_ONLY_QURANTINED) {
$filter = Criteria::create()
->andWhere(Criteria::expr()->eq('categoryCount', 0));
}
return $filter;
}
This doesn't feel right to me. The $mode
flag triggers my code smell alarm, I also am worrying about passing it into each asset querying method.
Furthermore, since I will have to set the mode depending on the state of the current logged in user, I also am worried that I will produce code duplications by doing a lot of if-user-has-the-rights-checks in my actions or services:
/**
* @Route("/all_assets/{page}",
* name="get_paginated_assets",
* requirements={"page" = "d+"},
* defaults={"page" = "1"},
* options={"expose"=true}
* )
*/
public function getPaginatedAssetActions($page)
{
$repo = $this->getAssetRepository();
$isAllowedToViewRestrictedAssets = $this->getCurrentUser()->isAdmin();
if ($isAllowedToViewRestrictedAssets) {
$mode = EntityAssetRepository::FILTER_SHOW_ALL;
}
$query = $repo->getQueryForAll($mode);
$pagerfanta = new Pagerfanta(new DoctrineORMAdapter($query));
$data = $this->getCurrentPagedResults($pagerfanta, $page);
$view = $this->view();
$view->setFormat('json');
$view->setData($data);
return $view;
}
Is this the right way to implement the logic to enable filters in a Repository? Feedback on whether there is a more decoupled way would be highly appreciated.
php php5 doctrine
I have a Symfony2 project. I have an Entity Asset
which can have relations with Category
. I store a categoryCount
-field within the Asset
to determine if an asset has one, to select those fast via a DQL-query depending on the current user state.
Now found myself writing something along the lines of this in my AssetRepository
:
/**
* @param string $orderBy
* @param int $mode
*
* @return DoctrineORMQuery
*/
public function getQueryForAll($orderBy = 'DESC', $mode = self::FILTER_WITHOUT_QUARANTINED)
{
$qb = $this->createQueryBuilder('asset');
$qb->orderBy('asset.updatedAt', $orderBy);
$filter = $this->getQuarantinedCriteria($mode);
if ($filter) {
$qb->addCriteria($filter);
}
return $query = $qb->getQuery();
}
/**
* @param int $mode
*
* @return Criteria|null
*/
protected function getQuarantinedCriteria($mode = self::FILTER_WITHOUT_QUARANTINED) {
$filter = null;
if ($mode === self::FILTER_WITHOUT_QUARANTINED) {
$filter = Criteria::create()
->andWhere(Criteria::expr()->gt('categoryCount', 0));
} else if ($mode === self::FILTER_ONLY_QURANTINED) {
$filter = Criteria::create()
->andWhere(Criteria::expr()->eq('categoryCount', 0));
}
return $filter;
}
This doesn't feel right to me. The $mode
flag triggers my code smell alarm, I also am worrying about passing it into each asset querying method.
Furthermore, since I will have to set the mode depending on the state of the current logged in user, I also am worried that I will produce code duplications by doing a lot of if-user-has-the-rights-checks in my actions or services:
/**
* @Route("/all_assets/{page}",
* name="get_paginated_assets",
* requirements={"page" = "d+"},
* defaults={"page" = "1"},
* options={"expose"=true}
* )
*/
public function getPaginatedAssetActions($page)
{
$repo = $this->getAssetRepository();
$isAllowedToViewRestrictedAssets = $this->getCurrentUser()->isAdmin();
if ($isAllowedToViewRestrictedAssets) {
$mode = EntityAssetRepository::FILTER_SHOW_ALL;
}
$query = $repo->getQueryForAll($mode);
$pagerfanta = new Pagerfanta(new DoctrineORMAdapter($query));
$data = $this->getCurrentPagedResults($pagerfanta, $page);
$view = $this->view();
$view->setFormat('json');
$view->setData($data);
return $view;
}
Is this the right way to implement the logic to enable filters in a Repository? Feedback on whether there is a more decoupled way would be highly appreciated.
php php5 doctrine
php php5 doctrine
edited Apr 17 '14 at 11:32
rolfl♦
90.6k13190394
90.6k13190394
asked Apr 17 '14 at 10:46
k0pernikus
1538
1538
bumped to the homepage by Community♦ 13 hours ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
bumped to the homepage by Community♦ 13 hours ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
You can try to use SQLFilter for this purpose. It allows you to tune SQL query based on some logic. You can pass information about your user's state into filter through DI container and use this information to tune your SQL query. Please note that unlike repositories - SQLFilter works on SQL level, not on DQL. More information can be found into Doctrine's documentation.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
});
});
}, "mathjax-editing");
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "196"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f47455%2ffiltering-queries-based-on-the-current-user-state%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
You can try to use SQLFilter for this purpose. It allows you to tune SQL query based on some logic. You can pass information about your user's state into filter through DI container and use this information to tune your SQL query. Please note that unlike repositories - SQLFilter works on SQL level, not on DQL. More information can be found into Doctrine's documentation.
add a comment |
up vote
0
down vote
You can try to use SQLFilter for this purpose. It allows you to tune SQL query based on some logic. You can pass information about your user's state into filter through DI container and use this information to tune your SQL query. Please note that unlike repositories - SQLFilter works on SQL level, not on DQL. More information can be found into Doctrine's documentation.
add a comment |
up vote
0
down vote
up vote
0
down vote
You can try to use SQLFilter for this purpose. It allows you to tune SQL query based on some logic. You can pass information about your user's state into filter through DI container and use this information to tune your SQL query. Please note that unlike repositories - SQLFilter works on SQL level, not on DQL. More information can be found into Doctrine's documentation.
You can try to use SQLFilter for this purpose. It allows you to tune SQL query based on some logic. You can pass information about your user's state into filter through DI container and use this information to tune your SQL query. Please note that unlike repositories - SQLFilter works on SQL level, not on DQL. More information can be found into Doctrine's documentation.
answered May 21 '17 at 21:14
Flying
21123
21123
add a comment |
add a comment |
Thanks for contributing an answer to Code Review Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f47455%2ffiltering-queries-based-on-the-current-user-state%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown