nextras / orm

Orm with clean object design, smart relationship loading and powerful collections.
https://nextras.org/orm
MIT License
310 stars 57 forks source link

Can access #303

Closed japlavaren closed 6 years ago

japlavaren commented 6 years ago

Hi,

I have problem when I am trying to call repository function from related entity. Is this not possible, or I am doing something wrong?

When I am calling $userEntity->unverifiedShortRegistrations I am getting error:

MemberAccessException: Call to undefined method Nextras\Orm\Mapper\Dbal\DbalCollection::findAllUnverified().

I here I have relation to ShortRegistrationEntity inside $allShortRegistrations and I am trying to create virtua to get only verified ones. Method findAllUnverified() is defined in ShortRegistrationRepository attached bellow:

/**
 * @property int $id {primary}
 * @property OneHasMany|ShortRegistrationEntity $allShortRegistrations {1:m ShortRegistrationEntity::$user}
 * @property OneHasMany|ShortRegistrationEntity $unverifiedShortRegistrations {virtual}
 */
class UserEntity extends Entity
{
    protected function getterUnVerifiedShortRegistrations(): ICollection
    {
        return $this->allShortRegistrations->get()->findAllUnverified();
    }
}

ShortRegistrationEntity with relation to UserEntity:

/**
 * @property int $id {primary}
 * @property bool $verified
 * @property UserEntity $user {m:1 UserEntity::$allShortRegistrations}
 */
class ShortRegistrationEntity extends Entity
{
}

And ShortRegistrationRepository with method which I want to call:

class ShortRegistrationRepository extends Repository
{
    public static function getEntityClassNames(): array
    {
        return [ShortRegistrationEntity::class];
    }

    public function findAllUnverified(): ICollection
    {
        return $this->findBy(['verified' => false]);
    }
}
hrach commented 6 years ago

Relationships' get() method (allShortRegistrations->get()) returns ICollection instance, not a IRepostory instance. It's up to you re-use somehow (ICollection filtering) logic between repository and the entity. This is wanted behavior, when relationship is modified and not persisted, you will receive ArrayCollection and you will work with PHP's collection.