laravel-mongodb icon indicating copy to clipboard operation
laravel-mongodb copied to clipboard

Eloquent\Model->drop() doesn't return int

Open stevensgsp opened this issue 3 years ago • 2 comments

  • Laravel-mongodb Version: 3.6.3
  • PHP Version: 7.3.19
  • Database Driver & Version: mongodb 1.7.5

Description:

The drop() method of the Jenssegers\Mongodb\Eloquent\Model class has a docblock that says the returning value is an int, but the actual value is being returned is an instance of the Jenssegers\Mongodb\Eloquent\Builder class.

This is because the drop() method is returning the resulting value of the unset() method of the Jenssegers\Mongodb\Eloquent\Builder instance. But this call to the unset() method is being forwarded to the Jenssegers\Mongodb\Query\Builder class by the Illuminate\Database\Eloquent::__call() magic method, which means it returns the Jenssegers\Mongodb\Eloquent\Builder instance.

This can be fixed by adding a __call() method to the Jenssegers\Mongodb\Eloquent\Builder class, that can be like this:

    /**
     * @inheritdoc
     */
    public function __call($method, $parameters)
    {
        if ($method == 'unset') {
            return call_user_func_array([$this->query, 'drop'], $parameters);
        }

        return parent::__call($method, $parameters);
    }

Similar to the _call() method of Jenssegers\Mongodb\Eloquent\Model and Jenssegers\Mongodb\Query\Builder classes.

Steps to reproduce

  1. Get a Jenssegers\Mongodb\Eloquent\Model instance.
  2. Try to drop an attribute of this model instance by calling the drop($attribute) method.
  3. The attribute is successfully dropped, but the returning value of the drop() method is an instance of the Jenssegers\Mongodb\Eloquent\Builder class instead of an integer.

Expected behaviour

The drop() method of the Jenssegers\Mongodb\Eloquent\Model class should return an int value.

Actual behaviour

The drop() method of the Jenssegers\Mongodb\Eloquent\Model class is returning a Jenssegers\Mongodb\Eloquent\Builder instance.

stevensgsp avatar Feb 05 '21 18:02 stevensgsp

Actually, a best solution would be add 'unset' to the protected passthru array property of the Jenssegers\Mongodb\Eloquent\Builder class.

stevensgsp avatar Feb 05 '21 20:02 stevensgsp

Don't want to open separate issue: Builder->drop also doesn't support additional $options param for performUpdate(), like Builder->update for example, so we are unable to pass conditional option to remove some nested array like ['arrayFilter' => something]

alexsmko avatar Jun 21 '21 16:06 alexsmko