laravel-mysql-spatial icon indicating copy to clipboard operation
laravel-mysql-spatial copied to clipboard

Trait method newEloquentBuilder has not been applied, because there are collisions with other trait methods

Open aletundo opened this issue 7 years ago • 9 comments

I'm using 1.0.0 version of your package together with sofa/eloquence:^5.4. I thought the error in the title was fixed using: use SpatialTrait { Eloquence::newEloquentBuilder insteadof SpatialTrait; SpatialTrait::newEloquentBuilder as newEloquentSpatialBuilder; } because the Eloquence methods I'm using works well.

But now, I'm trying to update the location field of my model using the Point class without success. The exeception is: SQLSTATE[22003]: Numeric value out of range: 1416 Cannot get geometry object from data you send to the GEOMETRY field.

Any idea to fix this collision without impact on the functionalities? I really need both packages. I'm going to open an issue to Eloquence package too.

aletundo avatar Sep 12 '17 17:09 aletundo

You'd have to create your own Illuminate\Database\Eloquent\Builder implementing both:

Maybe something like the following (adjust the namespaces to your app):

namespace Grimzy\LaravelMysqlSpatial\Eloquent;

use Sofa\Eloquence\Builder as EloquenceBuilder;

class MergedBuilder extends EloquenceBuilder
{
    use SpatialBuilderTrait;
}

with SpatialBuilderTrait implementing the methods from Grimzy\LaravelMysqlSpatial\Eloquent \Builder:

namespace Grimzy\LaravelMysqlSpatial\Eloquent;

use Grimzy\LaravelMysqlSpatial\Types\GeometryInterface;

trait SpatialBuilderTrait
{
    public function update(array $values)
    {
        foreach ($values as $key => &$value) {
            if ($value instanceof GeometryInterface) {
                $value = $this->asWKT($value);
            }
        }

        return parent::update($values);
    }

    protected function asWKT(GeometryInterface $geometry)
    {
        return $this->getQuery()->raw("ST_GeomFromText('".$geometry->toWKT()."')");
    }
}

And override Eloquence::newEloquentBuilder() in your model:

use new \Grimzy\LaravelMysqlSpatial\Eloquent\MergedBuilder as Builder;

class SomeModel extends Model;
    use SpatialTrait, Eloquence;

    public function newEloquentBuilder($query)
    {
        return Builder($query);
    }
}

Can you try this out?

grimzy avatar Sep 17 '17 21:09 grimzy

Sorry for the late reply, I'm going to try it asap. I already created a custom solution to fulfill the same needs which brought me to use your package. Therefore, if your solution don't work, no matter.

aletundo avatar Sep 21 '17 08:09 aletundo

@aletundo I've pushed code in builder-as-trait if you want to check it out.

I'm probably merging this with master and the mysql-5.6 branches in versions 2.0.2 and 1.1.3 sometime soon.

grimzy avatar Sep 23 '17 16:09 grimzy

@grimzy So, I should just use this branch instead of all the changes listed before? Jtk, my custom solution consist of a trait with a bunch of scope and accessor methods. Obviously without using the geometric models and so on. I went through unpack the binary returned from MySQL etc. Therefore, my way was not too bad :)

aletundo avatar Sep 28 '17 09:09 aletundo

As referenced above, I also have this problem in combination with GeneaLabs/laravel-model-caching package.

eelco2k avatar Jun 01 '18 20:06 eelco2k

Same error with the NodeTrait https://github.com/lazychaser/laravel-nestedset

Daniyal-Javani avatar Oct 16 '18 05:10 Daniyal-Javani

same issue with https://github.com/jenssegers/laravel-mongodb

vesper8 avatar Apr 06 '20 23:04 vesper8

Same issue with https://github.com/renoki-co/laravel-eloquent-query-cache

tomredhot avatar Jun 18 '20 15:06 tomredhot

use SomeBaseTrait, SpatialTrait {
    SomeBaseTrait::newBaseQueryBuilder insteadof SpatialTrait;
}

SergioFloresG avatar Dec 28 '20 00:12 SergioFloresG