laravel-mysql-spatial
laravel-mysql-spatial copied to clipboard
Trait method newEloquentBuilder has not been applied, because there are collisions with other trait methods
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.
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?
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 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 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 :)
As referenced above, I also have this problem in combination with GeneaLabs/laravel-model-caching package.
Same error with the NodeTrait https://github.com/lazychaser/laravel-nestedset
same issue with https://github.com/jenssegers/laravel-mongodb
Same issue with https://github.com/renoki-co/laravel-eloquent-query-cache
use SomeBaseTrait, SpatialTrait {
SomeBaseTrait::newBaseQueryBuilder insteadof SpatialTrait;
}