/** @test */ public function it_should_make_a_casted_value() { $property = Factory::addProperty(ContactProperties::class, 'single', Factory::TYPE_INTEGER); $value = $this->factory->addValue($property, 'aa'); static::assertInstanceOf(Values\IntegerValue::class, $value); static::assertEquals(0, $value->value); }
/** * Create a new model instance that is existing. * * @param array $attributes * @param string|null $connection * * @return static */ public function newFromBuilder($attributes = [], $connection = null) { /* @var Value $instance */ $property = Factory::getPropertyById($attributes->property_id); $instance = Factory::getType($property->type) ?: $this; $model = $instance->newInstance([], true); $model->setRelation('property', $property); $model->setRawAttributes((array) $attributes, true); $model->setConnection($connection ?: $this->connection); return $model; }
/** * Register the service provider. */ public function register() { $this->mergeConfigFrom($this->configPath(), 'eloquent-extra'); $this->app->bind('command.eloquent-extra.logging-table', function (Application $app) { return new Logging\TableCommand($app->make('migration.creator'), $app->make('composer')); }, true); $this->app->bind('command.eloquent-extra.properties-table', function (Application $app) { return new Properties\MakeMigrationCommand($app->make('migration.creator'), $app->make('composer')); }, true); $this->commands('command.eloquent-extra.logging-table'); $this->commands('command.eloquent-extra.properties-table'); $types = (array) $this->app->make('config')->get('eloquent-extra.property_types', []); foreach ($types as $type => $class) { Factory::registerType($type, $class); } }
/** * @param Builder $builder * @param Eloquent $model * * @throws \InvalidArgumentException */ public function apply(Builder $builder, Eloquent $model) { $query = $builder->getQuery(); if (count($loads = $builder->getEagerLoads()) === 0 && count($query->wheres) === 0 || count($properties = Factory::getPropertiesByEntity($model)) === 0) { return; } //compact eager loading if (count($loads) > 0) { $eadgeLoads = []; $props = []; foreach ($loads as $load => $data) { if ($properties->has($load)) { $props[$load] = $properties->get($load); //$eadgeLoads[$load] = $data; } else { $eadgeLoads[$load] = $data; } } if (count($props)) { $eadgeLoads = ['values' => function (Values $relation) use($props) { $relation->setProperties(new Collection($props)); }] + $eadgeLoads; } $builder->setEagerLoads($eadgeLoads); } $table = $model->getTable(); $columns = $this->parseWhere($properties, $query->wheres, $table); if (count($columns) === 0) { return; } $value = new Value(); $query->select($table . '.*'); $multiple = false; foreach ($columns as $alias => $property) { if ($property->multiple) { $multiple = true; } $query->leftJoin($value->getTable() . ' AS ' . $alias, function (JoinClause $join) use($model, $alias, $property) { $join->on($model->getTable() . '.' . $model->getKeyName(), '=', $alias . '.entity_id'); $join->where($alias . '.property_id', '=', $property->id); }); } if ($multiple) { //Distinct if condition by multiple values $query->distinct(); } }
/** * Add new value to property. * * @param mixed $value */ public function add($value) { $this->factory->addValue($this->property, $value); $this->factory->updateValue($this->property->name); }