IsraelOrtuno / Eavquent

EAV modeling package for Laravel and Eloquent – This package is abandoned, please use this fork https://github.com/rinvex/attributes
63 stars 13 forks source link

How to register entity attributes? #38

Closed codercms closed 8 years ago

codercms commented 8 years ago

I addeed following entry to the eav_attributes table:

INSERT INTO `eav_attributes` (`id`, `code`, `label`, `model`, `entity`, `collection`, `default_value`) VALUES
(2, 'city', 'city', 'App\\Models\\Books', '*', 0, NULL);

Code of books model:

<?php
namespace App\Models;

use Devio\Eavquent\Eavquent;
use Illuminate\Database\Eloquent\Model;

class Books extends Model
{
    use Eavquent;

    protected $fillable = ['title', 'author', 'isbn'];

    // Eager loading all the registered attributes
    protected $with = ['eav'];
}

Code of books controller:

<?php
namespace App\Http\Controllers;

use App\Models\Books;
use Illuminate\Http\Request;

class BooksController extends Controller
{
    public function add(Request $request)
    {
        $book = new Books($request->all());

        $book->city = 'Test';

        $this->validate($request, [
            'title' => 'required',
            'author' => 'required',
            'isbn' => 'required'
        ]);

        $book->saveOrFail();
    }
}

Call stack:

in Connection.php line 408
at PDO->prepare('insert into `books` (`title`, `author`, `isbn`, `city`, `updated_at`, `created_at`) values (?, ?, ?, ?, ?, ?)') in Connection.php line 408
at Connection->Illuminate\Database\{closure}(object(MySqlConnection), 'insert into `books` (`title`, `author`, `isbn`, `city`, `updated_at`, `created_at`) values (?, ?, ?, ?, ?, ?)', array('Test book', 'Coder', '228', 'Test', '2016-04-20 21:29:48', '2016-04-20 21:29:48')) in Connection.php line 666
at Connection->runQueryCallback('insert into `books` (`title`, `author`, `isbn`, `city`, `updated_at`, `created_at`) values (?, ?, ?, ?, ?, ?)', array('Test book', 'Coder', '228', 'Test', '2016-04-20 21:29:48', '2016-04-20 21:29:48'), object(Closure)) in Connection.php line 629
at Connection->run('insert into `books` (`title`, `author`, `isbn`, `city`, `updated_at`, `created_at`) values (?, ?, ?, ?, ?, ?)', array('Test book', 'Coder', '228', 'Test', '2016-04-20 21:29:48', '2016-04-20 21:29:48'), object(Closure)) in Connection.php line 409
at Connection->statement('insert into `books` (`title`, `author`, `isbn`, `city`, `updated_at`, `created_at`) values (?, ?, ?, ?, ?, ?)', array('Test book', 'Coder', '228', 'Test', '2016-04-20 21:29:48', '2016-04-20 21:29:48')) in Connection.php line 365
at Connection->insert('insert into `books` (`title`, `author`, `isbn`, `city`, `updated_at`, `created_at`) values (?, ?, ?, ?, ?, ?)', array('Test book', 'Coder', '228', 'Test', '2016-04-20 21:29:48', '2016-04-20 21:29:48')) in Processor.php line 32
at Processor->processInsertGetId(object(Builder), 'insert into `books` (`title`, `author`, `isbn`, `city`, `updated_at`, `created_at`) values (?, ?, ?, ?, ?, ?)', array('Test book', 'Coder', '228', 'Test', '2016-04-20 21:29:48', '2016-04-20 21:29:48'), 'id') in Builder.php line 1963
at Builder->insertGetId(array('title' => 'Test book', 'author' => 'Coder', 'isbn' => '228', 'city' => 'Test', 'updated_at' => '2016-04-20 21:29:48', 'created_at' => '2016-04-20 21:29:48'), 'id')
at call_user_func_array(array(object(Builder), 'insertGetId'), array(array('title' => 'Test book', 'author' => 'Coder', 'isbn' => '228', 'city' => 'Test', 'updated_at' => '2016-04-20 21:29:48', 'created_at' => '2016-04-20 21:29:48'), 'id')) in Builder.php line 1337
at Builder->__call('insertGetId', array(array('title' => 'Test book', 'author' => 'Coder', 'isbn' => '228', 'city' => 'Test', 'updated_at' => '2016-04-20 21:29:48', 'created_at' => '2016-04-20 21:29:48'), 'id')) in Model.php line 1621
at Builder->insertGetId(array('title' => 'Test book', 'author' => 'Coder', 'isbn' => '228', 'city' => 'Test', 'updated_at' => '2016-04-20 21:29:48', 'created_at' => '2016-04-20 21:29:48'), 'id') in Model.php line 1621
at Model->insertAndSetId(object(Builder), array('title' => 'Test book', 'author' => 'Coder', 'isbn' => '228', 'city' => 'Test', 'updated_at' => '2016-04-20 21:29:48', 'created_at' => '2016-04-20 21:29:48')) in Model.php line 1590
at Model->performInsert(object(Builder), array()) in Model.php line 1481
at Model->save() in BooksController.php line 31
at BooksController->add(object(Request))
at call_user_func_array(array(object(BooksController), 'add'), array(object(Request))) in Container.php line 507
at Container->call(array(object(BooksController), 'add'), array()) in RoutesRequests.php line 586
at Application->callControllerCallable(array(object(BooksController), 'add'), array()) in RoutesRequests.php line 553
at Application->callLumenController(object(BooksController), 'add', array(true, array('uses' => 'App\Http\Controllers\BooksController@add'), array())) in RoutesRequests.php line 526
at Application->callControllerAction(array(true, array('uses' => 'App\Http\Controllers\BooksController@add'), array())) in RoutesRequests.php line 494
at Application->callActionOnArrayBasedRoute(array(true, array('uses' => 'App\Http\Controllers\BooksController@add'), array())) in RoutesRequests.php line 479
at Application->handleFoundRoute(array(true, array('uses' => 'App\Http\Controllers\BooksController@add'), array())) in RoutesRequests.php line 376
at Application->Laravel\Lumen\Concerns\{closure}() in RoutesRequests.php line 629
at Application->sendThroughPipeline(array(), object(Closure)) in RoutesRequests.php line 382
at Application->dispatch(null) in RoutesRequests.php line 327
at Application->run() in index.php line 28
IsraelOrtuno commented 8 years ago

You are creating an attribute for *... I know package is not yet documented...

INSERT INTO `eav_attributes` (`id`, `code`, `label`, `model`, `entity`, `collection`, `default_value`) VALUES
(2, 'city', 'city', 'Devio\\Eavquent\\Value\\Data\\Varchar', 'App\\Models\\Book', 0, NULL);

The entity represents the model you attach the value to, and the model is the data type... I am updating this naming to make it more understandable.

Remember package is not production ready and things may break / change.

IsraelOrtuno commented 8 years ago

If you are pointing to dev-master there are two columns that have changed on the Attributes table:

ping @codercms

codercms commented 8 years ago

@IsraelOrtuno thanks for the information ;)