blt04 / doctrine2-nestedset

A NestedSet extension for Doctrine2
GNU Lesser General Public License v2.1
127 stars 49 forks source link

PDOException on serialize/cache a NodeWrapper #31

Open vocjunior opened 10 years ago

vocjunior commented 10 years ago

I got the exception: PDOException: You cannot serialize or unserialize PDO instances when I tried to save a \DoctrineExtensions\NestedSet\NodeWrapper object in Doctrine Cache. To fix this bug I made the following changes in NodeWrapper class. Please, consider add this changes in next release.

    /**
     * To avoid PDO exception, the property 'manager' cannot be serialized
     * If you need the manager after unserialize the object, you MUST call the attachManager() method in the current wrapper
     * @return array
     */
    function __sleep() {
        return [
            'node',
            'parent',
            'ancestors',
            'descendants',
            'children',
            'level',
            'outlineNumbers',
        ];
    }

    /**
     * Attach a {@see \DoctrineExtensions\NestedSet\Manager} to the current wrapper
     * This method is useful after wakeup a serialized object
     * @param Manager $manager
     */
    public function attachManager(Manager $manager) {
        if (empty($this->manager)) {
            $this->manager = $manager;
        }
    }