nextras / orm

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

Removing entities in foreach #434

Closed skybamar closed 4 years ago

skybamar commented 4 years ago

I get error Property Smartlook\Domain\Project\ProjectPermission::$project is not nullable., when in 2nd foreach run call remove method.

Code:

// un-assign user from projects
foreach ($user->projectPermissions as $projectPermission) {
    $this->projectPermissionRepository->remove($projectPermission);
}
$this->userRepository->flush();

Entity:

/**
 * @property-read int $id {primary-proxy}
 * @property Project $project {m:1 Project::$userPermissions} {primary}
 * @property User $user {m:1 User::$projectPermissions} {primary}
 * @property bool $isOwner
 * @property-read int $permission
 */
class ProjectPermission extends Entity

Versions::

hrach commented 4 years ago

Hi, thanks for reporting. Sadly I failed reproduce this - here is repro doing the same: https://github.com/nextras/orm/commit/d19f981ac76ea68e90961237a0511b79dc5a63a5 So basically there will be other hidden aspect - probably some other relationship? Also, it could help if you would provide stack trace (tracy bluescreen), if needed - only privately on mail. Thank you!

pupitooo commented 4 years ago

I tried to reproduce it in tests, but I was not able to push it to do same error. I hope you will find something in our Tracy (send it on Slack).

There is more from our code:

/**
 * @property-read int $id {primary}
 * @property OneHasMany|OrganizationPermission[] $organizationPermissions {1:m OrganizationPermission::$user}
 */
class User extends Entity
{
    /** @return OrganizationPermission[]|ICollection */
    public function getOwnersPermissions(): ICollection
    {
        /** @var OrganizationPermission[]|ICollection $collection */
        $collection = $this->organizationPermissions->toCollection()->findBy(['isOwner' => true]);
        return $collection;
    }
    /** @return OrganizationPermission[]|ICollection */
    public function getAssignedPermissions(): ICollection
    {
        /** @var OrganizationPermission[]|ICollection $collection */
        $collection = $this->organizationPermissions->toCollection()->findBy(['isOwner' => false]);
        return $collection;
    }
}
/**
 * @property-read int $id {primary}
 * @property Organization $organization {m:1 Organization::$userPermissions}
 * @property User $user {m:1 User::$organizationPermissions}
 * @property \DateTimeImmutable $createdAt
 * @property-read bool $isOwner
 */
class OrganizationPermission extends Entity
{
}
/**
 * @property-read int $id {primary}
 * @property OneHasMany|OrganizationPermission[] $userPermissions {1:m OrganizationPermission::$organization}
 */
class Organization extends Entity
{
    /** @return OrganizationPermission[]|ICollection */
    public function getOwners(): ICollection
    {
        /** @var OrganizationPermission[]|ICollection $collection */
        $collection = $this->userPermissions->toCollection()->findBy(['isOwner' => true]);
        return $collection;
    }
    /** @return OrganizationPermission[]|ICollection */
    public function getMembers(): ICollection
    {
        /** @var OrganizationPermission[]|ICollection $collection */
        $collection = $this->userPermissions->toCollection()->findBy(['isOwner' => false]);
        return $collection;
    }
}

Problematic part:

$user = $this->userRepository->getById(377817);
foreach ($user->getOwnersPermissions() as $ownersPermission) {
//foreach ($user->organizationPermissions as $ownersPermission) { // when we us this, everything is ok
    \bdump($ownersPermission->organization->id); // when we commented this (not fetching organization), everything is ok
}
foreach ($user->organizationPermissions as $organizationPermission) {
    $this->organizationPermissionRepository->remove($organizationPermission);
}

Data in our MySql DB:


organizations_users

id  |   organization_id |   user_id |   isOwner |   createdAt
417014      375753          377817      1       2020-05-26 06:38:29
419820      375753          380293      0       2020-06-09 08:17:50
419900      375753          380358      1       2020-06-09 14:43:29
428325      378229          377817      0       2020-07-24 02:10:08
hrach commented 4 years ago

Thanks for the details, helped a lot! Should be fixed in master.