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

IModel->getRepository() support #18

Closed
mabar opened this issue May 4, 2021 · 10 comments
Closed

IModel->getRepository() support #18

mabar opened this issue May 4, 2021 · 10 comments
Labels

Comments

@mabar
Copy link

mabar commented May 4, 2021

Would it please be possible to add support for $userRepository = $model->getRepository(UserRepository::class);? Currently it's just IRepository for phpstorm.

I can't add annotations to an extended Model class and find myself quite often in situation with 3-6 repositories where replacing them with model would reduce boilerplate significantly.

Thank you for consideration :)

@mabar
Copy link
Author

mabar commented May 4, 2021

I tried to solve it via .phpstorm.meta.php which resolves warning when assigning to private UserRepository $userRepository;, but phpstorm still thinks it is UserRepository|IRepository and acts like with IRepository

<?php declare(strict_types = 1);

namespace PHPSTORM_META;

override(
	\Nextras\Orm\Model\IModel::getRepository(0),
	type(0)
);

@hrach
Copy link
Member

hrach commented May 4, 2021

What does it mean to "act slike with IRepository"?
It works for me. Also try please

override(\Nextras\Orm\Model\IModel::getRepository(0), map(['' => '@']));

which may be more correct.

@mabar
Copy link
Author

mabar commented May 4, 2021

What does it mean to "act slike with IRepository"?

It's the type phpstorm shows when I am inspecting property type via ctrl and mouse pointer over $this->userRepository

Check the following example - in comments are types which phpstorm thinks variables have

use Nextras\Orm\Model\IModel;
use OriCMF\Core\User\UserRepository;

final class Test
{

	private IModel $model;

	private UserRepository $userRepository;

	public function __construct(IModel $model)
	{
		$this->model = $model;
		$this->userRepository = $model->getRepository(UserRepository::class);
	}

	public function test(): void
	{
		// $this->userRepository is IRepository|UserRepository
		// $user1 is IEntity
		$user1 = $this->userRepository->getByIdChecked(1);

		// $collection is ICollection
		$collection = $this->userRepository->findAll();
		// $user is mixed
		foreach ($collection as $user) {

		}
	}

}

Your syntax has the same problem. I think it should even be equal, mine is just syntax sugar from newer phpstorm release.

@mabar
Copy link
Author

mabar commented May 4, 2021

phpstorm meta just helped me to get rid of warning Incompatible types: Expected property of type '\OriCMF\Core\User\UserRepository', '\Nextras\Orm\Repository\IRepository' provided and got the repository-specific methods to work, but calling any findBy* and getBy* methods result only into IEntity (even to mixed in ICollection) and also the conditions autocomplete in findBy() and getBy() does not work.
I guess the plugin does not play well with the union type? But I understand it's phpstorm error, because the IRepository type in property with UserRepository type is impossible.

@hrach hrach added the bug label May 5, 2021
@hrach hrach closed this as completed in f2e0b62 May 5, 2021
@hrach
Copy link
Member

hrach commented May 5, 2021

Thanks for detailed description, the failing resolution over class property was plugin's bug.

@hrach
Copy link
Member

hrach commented May 5, 2021

Released as 0.8.0. Either wait for the review from JB, or use the zip in the release:
https://github.com/nextras/orm-intellij/releases/tag/v0.8.0

@mabar
Copy link
Author

mabar commented May 6, 2021

Thanks, it works as expected, in combination with .phpstorm.meta.php

@mabar
Copy link
Author

mabar commented May 6, 2021

I just found that one minor issue is still left, assignment of repository to variable is needed.

With variable assignment, $user is User

$userRepository = $this->model->getRepository(UserRepository::class);
$user = $userRepository->getByIdChecked(1);
assert($user instanceof User);

Without variable assignment, $user is IEntity

$user = $this->model->getRepository(UserRepository::class)->getByIdChecked(1);
assert($user instanceof IEntity);

@hrach
Copy link
Member

hrach commented May 6, 2021

Create new issue please.

@hrach
Copy link
Member

hrach commented May 6, 2021

Phpstorm meta added to repo: nextras/orm@f0d67ae

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

2 participants