Skip to content

Commit

Permalink
Merge pull request #1 from liam-wiltshire/replace-getTableFields
Browse files Browse the repository at this point in the history
Use getColumnListing to get col list
  • Loading branch information
liam-wiltshire committed Sep 19, 2019
2 parents a4af637 + 3f8face commit 4113436
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 50 deletions.
42 changes: 21 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,46 +3,49 @@
liam-wiltshire/laravel-model-meta is an extension to the default Laravel Eloquent model to add metadata to a model.

# What MetaData?
While Eloquant is very good at handling relational data, however not all data we deal with works like this. Without going as far as using a NoSQL solution such as Mongo, using mySQL to hold relational data, but with a JSON meta field is a potential solution.
While Eloquent is very good at handling relational data, however not all data we deal with works like this. Without going as far as using a NoSQL solution such as Mongo, using mySQL to hold relational data, but with a JSON meta field is a potential solution.

liam-wiltshire/laravel-model-meta allows you to use standard Eloquent getters and setters to add and remove metadata to your models. Any attributes that relate to a column in your database table will be handled as a standard attribute, but anything else will be added to the meta data.

This gives you great flexibility to have structured data where appropriate (for example an book will always have a title, author etc), but to then have other related, unstructured data (for example some books might have the number of pages, others might not)

When you are structuring your data, it's important to consider if you want to be able to query this data. Unless you use something like virtual columns at the DB level, this data isn't queryable - it's not designed to store the primary data for that record.

# Example

```php
$test = new \App\Models\Test();
$book = new \App\Models\Book();

$test->forceFill(
$book->forceFill(
[
'subject_id' => 1,
'level_id' => 1,
'slug' => 'old-slug',
'title' => 'Test Title',
'instructions' => 'Some instructions'
'title' => 'Laravel Quickstart Guide',
'author_id' => 90,
'publisher_id' => 5,
'description' => 'This is an awesome book'
]
);

$test->save();
$book->save();

$test = \App\Models\Test::find(1);
$book = \App\Models\Book::find(1);

//This is an actual field in the DB, so this will be set to that attribute
$test->slug = 'test-slug';
$book->title = 'Laravel Quickstart Guide, Second Edition';

//This isn't a field in the DB, and isn't a relationship etc, so will be stored in the meta field
$test->meta_subject = 'This is a meta subjects';
$book->page_count = 200;

$test->save();
```
This code would generate a new `Test` model and save it to the DB. Assuming that `meta_subject` is not a column in our table, the `meta_subject` will automatically be added to the metadata:

```text
root@localhost:[homestead]> SELECT id, slug, title, meta FROM tests;
+----+-----------+------------+--------------------------------------------+
| id | slug | title | meta |
+----+-----------+------------+--------------------------------------------+
| 1 | test-slug | Test Title | {"meta_subject":"This is a meta subjects"} |
+----+-----------+------------+--------------------------------------------+
root@localhost:[homestead]> SELECT id, title, meta FROM books;
+----+------------------------------------------+--------------------+
| id | title | meta |
+----+------------------------------------------+--------------------+
| 1 | Laravel Quickstart Guide, Second Edition | {"page_count":200} |
+----+------------------------------------------+--------------------+
```

# Installation
Expand All @@ -62,6 +65,3 @@ If the name of your meta column is different, then add a `$metaDbField` property
```php
protected $metaDbField = 'metaData';
```

# Limitations
This is a pre-release. Many cases have not been completely tested yet, and unexpected results may be returned. You have been warned!
8 changes: 5 additions & 3 deletions src/Concerns/HasMeta.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace LiamWiltshire\LaravelModelMeta\Concerns;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Collection;

trait HasMeta
{
Expand All @@ -18,8 +17,11 @@ trait HasMeta
public function getTableFields(): array
{
if (!$this->tableFields) {
$fields = new Collection($this->getConnection()->select("DESC {$this->getTable()}"));
$this->tableFields = array_flip($fields->pluck(["Field"])->toArray());
$connection = $this->getConnection();

$fields = $connection->getSchemaBuilder()->getColumnListing($this->getTable());

$this->tableFields = array_flip($fields);
}

return $this->tableFields;
Expand Down
13 changes: 0 additions & 13 deletions tests/AltTraitModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,6 @@ class AltTraitModel extends Model {

public $status = 9;

//SQLLite doesn't have DESCRIBE, so....
public function getTableFields()
{
return [
'id' => 0,
'trait_model_id' => 1,
'title' => 2,
'meta' => 3,
'created_at' => 4,
'updated_at' => 5
];
}

public function myRelationship()
{
return $this->hasOne(AltTraitModel::class);
Expand Down
13 changes: 0 additions & 13 deletions tests/TraitModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,6 @@ class TraitModel extends Model {

public $status = 9;

//SQLLite doesn't have DESCRIBE, so....
public function getTableFields()
{
return [
'id' => 0,
'trait_model_id' => 1,
'title' => 2,
'meta' => 3,
'created_at' => 4,
'updated_at' => 5
];
}

public function myRelationship()
{
return $this->hasOne(TraitModel::class);
Expand Down

0 comments on commit 4113436

Please sign in to comment.