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

Propel 1.7 OutOfMemoryException from cross-ref add #1057

Open
jwong-dayspring opened this issue Mar 28, 2018 · 0 comments
Open

Propel 1.7 OutOfMemoryException from cross-ref add #1057

jwong-dayspring opened this issue Mar 28, 2018 · 0 comments

Comments

@jwong-dayspring
Copy link

I have a CrossRef table users_roles that joins my users and roles tables. Propel generates User::addRole() and Role::addUser() functions in the Base object.

With a large dataset, calling User::addRole() results in PHP running out of memory. The same code works just fine with a smaller dataset, or with Propel 1.6.x.

Propel 1.7 sets the back reference when adding on a CrossRef object. In my project, this adds a $role->getUsers() call to User::doAddRole(). Adding a role to a user ends up fetching all users that with that role.

In my case, I don't need the back reference counts to be updated. Since the problem code is in the Base class, I was able to override doAddRole() in the subclass.

I replaced the generated code:

protected function doAddRole(Role $role)
    {
        // set the back reference to this object directly as using provided method either results
        // in endless loop or in multiple relations
        if (!$role->getUsers()->contains($this)) { $userRole = new UserRole();
        $userRole->setRole($role);
        $this->addUserRole($userRole);

            $foreignCollection = $role->getUsers();
            $foreignCollection[] = $this;
        }
    }

with (same as what Propel 1.6.x generated):

protected function doAddRole(Role $role)
    {
        $userRole = new UserRole();
        $userRole->setRole($role);
        $this->addUserRole($userRole);
    }

Initially it didn't seem like $foreignCollection was necessary, but it updates the PropelObjectCollection held by Role and subsequent calls to getUsers() returns the updated collection, including the User the role was just added to.

I can see why setting the back reference is helpful, but #677/#678 introduces a nasty side-effect for lookup table relationships and large databases.

See PHP5ObjectBuilder::addCrossFKDoAdd()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant