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

updated addIdentifiersToQuery to work with custom types. #938

Conversation

oleg-andreyev
Copy link
Contributor

@oleg-andreyev oleg-andreyev commented Oct 14, 2019

Subject

Batch actions do not work with custom types.

I am targeting this branch, because bug.

Changelog

### Fixed
- `batchAction` now works with custom-types.
  • Update the tests

@greg0ire greg0ire added the patch label Oct 14, 2019
@core23
Copy link
Member

core23 commented Nov 9, 2019

Can you please add some tests @oleg-andreyev ?

@oleg-andreyev
Copy link
Contributor Author

@core23 planning to find some time on weekends.

@stale
Copy link

stale bot commented Jan 30, 2020

Is this still relevant? If so, what is blocking it? Is there anything you can do to help move it forward?

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.

@stale stale bot added the wontfix label Jan 30, 2020
@SonataCI SonataCI removed the wontfix label Jan 30, 2020
@core23
Copy link
Member

core23 commented Jan 31, 2020

Can you please add a test

@oleg-andreyev
Copy link
Contributor Author

@core23 I’ve got message for stale bot and planning to do it on weekdays. Thanks!

@oleg-andreyev oleg-andreyev force-pushed the batch-actions-does-not-work-with-custom-type branch from b478c30 to cc77f69 Compare February 2, 2020 20:01
src/Model/ModelManager.php Outdated Show resolved Hide resolved
OskarStark
OskarStark previously approved these changes Feb 2, 2020
@SonataCI
Copy link
Collaborator

Could you please rebase your PR and fix merge conflicts?

src/Model/ModelManager.php Outdated Show resolved Hide resolved
@VincentLanglet
Copy link
Member

Hi @oleg-andreyev ! Do you have time to finish this PR ? This seems like almost done ! :)

@oleg-andreyev
Copy link
Contributor Author

Failing tests aren't related to current PR, can someone please trigger travis manually?

@VincentLanglet
Copy link
Member

@oleg-andreyev Done

@oleg-andreyev
Copy link
Contributor Author

@core23 please review PR

src/Model/ModelManager.php Outdated Show resolved Hide resolved
@VincentLanglet
Copy link
Member

@core23 @phansys Can you take a new look at the PR ? :)

src/Model/ModelManager.php Outdated Show resolved Hide resolved
src/Model/ModelManager.php Outdated Show resolved Hide resolved
src/Model/ModelManager.php Outdated Show resolved Hide resolved
src/Model/ModelManager.php Outdated Show resolved Hide resolved
src/Model/ModelManager.php Outdated Show resolved Hide resolved
tests/Model/ModelManagerTest.php Outdated Show resolved Hide resolved
tests/Model/ModelManagerTest.php Outdated Show resolved Hide resolved
tests/Model/ModelManagerTest.php Outdated Show resolved Hide resolved
tests/Model/ModelManagerTest.php Outdated Show resolved Hide resolved
tests/Model/ModelManagerTest.php Outdated Show resolved Hide resolved
phansys
phansys previously approved these changes May 28, 2020
@SonataCI
Copy link
Collaborator

Could you please rebase your PR and fix merge conflicts?

@jordisala1991
Copy link
Member

What is the status of this Pr?

@VincentLanglet
Copy link
Member

Are you still interested by this PR @oleg-andreyev ?

@oleg-andreyev
Copy link
Contributor Author

oleg-andreyev commented Nov 15, 2020

I'll rebase this branch, but it's taking too long to merge, so I completely forgot about this PR.

@VincentLanglet
Copy link
Member

I'll rebase this branch, but it's taking too long to merge, so I completely forgot about this PR.

There is few contirbutors, but lot of work/issues/PR.
Don't hesitate to ping us to friendly remember us to take a review.

core23
core23 previously approved these changes Nov 15, 2020
@oleg-andreyev oleg-andreyev dismissed stale reviews from core23 and phansys via 6553c7d November 18, 2020 20:14
@oleg-andreyev oleg-andreyev force-pushed the batch-actions-does-not-work-with-custom-type branch from 63c36cd to 6553c7d Compare November 18, 2020 20:14
@@ -982,22 +982,21 @@ public function testGetUrlsafeIdentifierNull(): void
*/
public function testAddIdentifiersToQuery(array $expectedParameters, array $identifierFieldNames, array $ids): void
{
$modelManager = $this->getMockBuilder(ModelManager::class)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mocking target is a bad practice.

@oleg-andreyev
Copy link
Contributor Author

@OskarStark @core23 @phansys I've rebased branch on latest 3.x, please review.


$prefix = uniqid();
$prefix = $this->generateIdentifierPrefix();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why introducing this method ? (Especially when we're trying to avoid protected method).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to have an ability to override it in tests, otherwise, it's impossible to predict results with hardcoded unique

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We shouldn't add method for this.
You could check the name by using a regex instead.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One good practice from DDD - avoid having dependencies on I/O outside of the Infrastructure layer, so I tend to remove such dependencies into a separate method/class so that it could be replaced later without affecting main functionality and tests. What do you think about it?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the idea 💡

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@oleg-andreyev In Sonata 4.0, the ModelManager will be final.

  • So a protected method is useless, it's basically a private one.
  • And creating a private method for calling uniqid() seems useless to me.

return Type::getType($fieldType);
}

private function buildInnerIdentifier(string $prefix, string $field, $value, int $pos, QueryBuilder $qb, ClassMetadata $classMetadata): string
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a lot of parameters in this private method.

Is there a way to reduce the number ?
Maybe the parameterName can be an argument instead of passing the prefix and the pos.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Initially wanted to make addIdentifiersToQuery a bit cleaner and remove chunk of code from foreach-loop but other hand I can revert this private method

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The method addIdentifiersToQuery is complex, so this private method has a lot of sens.

$parameterName = sprintf('field_%s_%s_%d', $prefix, $name, $pos);
$this-> buildInnerIdentifier($field, $value, $paraterName, $qb, $classMetadata)

Was just a suggestion, not a blocking point.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It will reduce a few args. Good idea. Will do it.

Comment on lines +412 to +413
if ($this->hasFieldCustomType($fieldType)) {
$type = $this->getFieldCustomType($fieldType);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When I look at the logic inside these function, I'm not sure we need to add private method for this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's used in two places. I usually try to remove duplicates. This reduces the burden on the developers when they need to update "Type" class usage.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I saw the duplicate.

But it's basically like a method

private function foo($a) {
    return $this->bar($a);
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right. Similar to findBy and findOneBy

public function findBy($class, array $criteria = [])
{
return $this->getEntityManager($class)->getRepository($class)->findBy($criteria);
}
public function findOneBy($class, array $criteria = [])
{
return $this->getEntityManager($class)->getRepository($class)->findOneBy($criteria);
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But these one are public, and required by the interface.

src/Model/ModelManager.php Outdated Show resolved Hide resolved
oleg-andreyev and others added 2 commits November 18, 2020 23:54
- updated addIdentifiersToQuery to work with custom types.
- added test for addIdentifiersToQuery: testAddIdentifiersToQueryWithCustomIdMapping
- extracted Type operations into getFieldCustomType, extracted loop-part from addIdentifiersToQuery to buildInnerIdentifier
@oleg-andreyev oleg-andreyev force-pushed the batch-actions-does-not-work-with-custom-type branch from 560e9f0 to 16efac2 Compare November 18, 2020 21:54
@oleg-andreyev
Copy link
Contributor Author

@core23 @OskarStark @phansys can you please take a look?

@VincentLanglet VincentLanglet requested a review from a team December 14, 2020 15:33

$prefix = uniqid();
$prefix = $this->generateIdentifierPrefix();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@oleg-andreyev In Sonata 4.0, the ModelManager will be final.

  • So a protected method is useless, it's basically a private one.
  • And creating a private method for calling uniqid() seems useless to me.

@VincentLanglet VincentLanglet requested a review from a team December 14, 2020 15:39
@github-actions
Copy link

This PR has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@github-actions github-actions bot added the stale label Jun 12, 2021
@github-actions github-actions bot closed this Jun 19, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

8 participants