From 73915c682d39548fe9d492eae2b144f896f70af8 Mon Sep 17 00:00:00 2001 From: Jon Wiersma Date: Thu, 9 Jul 2015 15:30:27 -0700 Subject: [PATCH 1/3] Added model classes to config so they are easier to extend. Now you can specify the model classes to use in the config file and then write classes to extend them without having use traits and re-write all the relationships. Should work for all models other than the base model. --- src/Models/Category.php | 2 +- src/Models/Inventory.php | 10 ++++---- src/Models/InventorySku.php | 2 +- src/Models/InventoryStock.php | 8 +++--- src/Models/InventoryStockMovement.php | 2 +- src/Models/InventoryTransaction.php | 4 +-- src/Models/InventoryTransactionHistory.php | 2 +- src/Models/Location.php | 2 +- src/Models/Metric.php | 2 +- src/Models/Supplier.php | 2 +- src/config/config.php | 29 ++++++++++++++++++++++ 11 files changed, 47 insertions(+), 18 deletions(-) diff --git a/src/Models/Category.php b/src/Models/Category.php index 69a2db7c..b3d91b7f 100644 --- a/src/Models/Category.php +++ b/src/Models/Category.php @@ -30,6 +30,6 @@ class Category extends Node */ public function inventories() { - return $this->hasMany('Stevebauman\Inventory\Models\Inventory', 'category_id', 'id'); + return $this->hasMany(config('inventory.models.inventory'), 'category_id', 'id'); } } diff --git a/src/Models/Inventory.php b/src/Models/Inventory.php index ff215832..f0c675c7 100644 --- a/src/Models/Inventory.php +++ b/src/Models/Inventory.php @@ -26,7 +26,7 @@ class Inventory extends BaseModel */ public function category() { - return $this->hasOne('Stevebauman\Inventory\Models\Category', 'id', 'category_id'); + return $this->hasOne(config('inventory.models.category'), 'id', 'category_id'); } /** @@ -36,7 +36,7 @@ public function category() */ public function metric() { - return $this->hasOne('Stevebauman\Inventory\Models\Metric', 'id', 'metric_id'); + return $this->hasOne(config('inventory.models.metric'), 'id', 'metric_id'); } /** @@ -46,7 +46,7 @@ public function metric() */ public function sku() { - return $this->hasOne('Stevebauman\Inventory\Models\InventorySku', 'inventory_id', 'id'); + return $this->hasOne(config('inventory.models.inventory_sku'), 'inventory_id', 'id'); } /** @@ -56,7 +56,7 @@ public function sku() */ public function stocks() { - return $this->hasMany('Stevebauman\Inventory\Models\InventoryStock', 'inventory_id', 'id'); + return $this->hasMany(config('inventory.models.inventory_stock'), 'inventory_id', 'id'); } /** @@ -66,7 +66,7 @@ public function stocks() */ public function suppliers() { - return $this->belongsToMany('Stevebauman\Inventory\Models\Supplier', 'inventory_suppliers', 'inventory_id')->withTimestamps(); + return $this->belongsToMany(config('inventory.models.supplier'), 'inventory_suppliers', 'inventory_id')->withTimestamps(); } /** diff --git a/src/Models/InventorySku.php b/src/Models/InventorySku.php index 718731bb..f85451ab 100644 --- a/src/Models/InventorySku.php +++ b/src/Models/InventorySku.php @@ -22,6 +22,6 @@ class InventorySku extends BaseModel */ public function item() { - return $this->belongsTo('Stevebauman\Inventory\Models\Inventory', 'inventory_id', 'id'); + return $this->belongsTo(config('inventory.models.inventory'), 'inventory_id', 'id'); } } diff --git a/src/Models/InventoryStock.php b/src/Models/InventoryStock.php index 18cefa38..ece59ca4 100644 --- a/src/Models/InventoryStock.php +++ b/src/Models/InventoryStock.php @@ -22,7 +22,7 @@ class InventoryStock extends BaseModel */ public function item() { - return $this->belongsTo('Stevebauman\Inventory\Models\Inventory', 'inventory_id', 'id'); + return $this->belongsTo(config('inventory.models.inventory'), 'inventory_id', 'id'); } /** @@ -32,7 +32,7 @@ public function item() */ public function movements() { - return $this->hasMany('Stevebauman\Inventory\Models\InventoryStockMovement', 'stock_id', 'id'); + return $this->hasMany(config('inventory.models.inventory_stock_movement'), 'stock_id', 'id'); } /** @@ -42,7 +42,7 @@ public function movements() */ public function transactions() { - return $this->hasMany('Stevebauman\Inventory\Models\InventoryTransaction', 'stock_id', 'id'); + return $this->hasMany(config('inventory.models.inventory_transaction'), 'stock_id', 'id'); } /** @@ -52,6 +52,6 @@ public function transactions() */ public function location() { - return $this->hasOne('Stevebauman\Inventory\Models\Location', 'id', 'location_id'); + return $this->hasOne(config('inventory.models.location'), 'id', 'location_id'); } } diff --git a/src/Models/InventoryStockMovement.php b/src/Models/InventoryStockMovement.php index cd59833d..de29e858 100644 --- a/src/Models/InventoryStockMovement.php +++ b/src/Models/InventoryStockMovement.php @@ -22,6 +22,6 @@ class InventoryStockMovement extends BaseModel */ public function stock() { - return $this->belongsTo('Stevebauman\Inventory\Models\InventoryStock', 'stock_id', 'id'); + return $this->belongsTo(config('inventory.models.inventory_stock'), 'stock_id', 'id'); } } diff --git a/src/Models/InventoryTransaction.php b/src/Models/InventoryTransaction.php index 89bb11a1..425e4bf2 100644 --- a/src/Models/InventoryTransaction.php +++ b/src/Models/InventoryTransaction.php @@ -23,7 +23,7 @@ class InventoryTransaction extends BaseModel implements StateableInterface */ public function stock() { - return $this->belongsTo('Stevebauman\Inventory\Models\InventoryStock', 'stock_id', 'id'); + return $this->belongsTo(config('inventory.models.inventory_stock'), 'stock_id', 'id'); } /** @@ -33,6 +33,6 @@ public function stock() */ public function histories() { - return $this->hasMany('Stevebauman\Inventory\Models\InventoryTransactionHistory', 'transaction_id', 'id'); + return $this->hasMany(config('inventory.models.inventory_transaction_history'), 'transaction_id', 'id'); } } diff --git a/src/Models/InventoryTransactionHistory.php b/src/Models/InventoryTransactionHistory.php index 1b390da8..bad19c60 100644 --- a/src/Models/InventoryTransactionHistory.php +++ b/src/Models/InventoryTransactionHistory.php @@ -22,6 +22,6 @@ class InventoryTransactionHistory extends BaseModel */ public function transaction() { - return $this->belongsTo('Stevebauman\Inventory\Models\InventoryTransaction', 'transaction_id', 'id'); + return $this->belongsTo(config('inventory.models.inventory_transaction'), 'transaction_id', 'id'); } } diff --git a/src/Models/Location.php b/src/Models/Location.php index 7f6659ad..c6fe3428 100644 --- a/src/Models/Location.php +++ b/src/Models/Location.php @@ -27,6 +27,6 @@ class Location extends Node */ public function stocks() { - return $this->hasMany('Stevebauman\Inventory\Models\InventoryStock', 'location_id', 'id'); + return $this->hasMany(config('inventory.models.inventory_stock'), 'location_id', 'id'); } } diff --git a/src/Models/Metric.php b/src/Models/Metric.php index bd3a71dc..c58f9abb 100644 --- a/src/Models/Metric.php +++ b/src/Models/Metric.php @@ -18,6 +18,6 @@ class Metric extends BaseModel */ public function items() { - return $this->hasMany('Stevebauman\Inventory\Models\Inventory', 'metric_id', 'id'); + return $this->hasMany(config('inventory.models.inventory'), 'metric_id', 'id'); } } diff --git a/src/Models/Supplier.php b/src/Models/Supplier.php index b8495b23..8c34585e 100644 --- a/src/Models/Supplier.php +++ b/src/Models/Supplier.php @@ -22,6 +22,6 @@ class Supplier extends BaseModel */ public function items() { - return $this->belongsToMany('Stevebauman\Inventory\Models\Inventory', 'inventory_suppliers', 'supplier_id')->withTimestamps(); + return $this->belongsToMany(config('inventory.models.inventory'), 'inventory_suppliers', 'supplier_id')->withTimestamps(); } } diff --git a/src/config/config.php b/src/config/config.php index 219d15c1..e2c90bbc 100644 --- a/src/config/config.php +++ b/src/config/config.php @@ -64,4 +64,33 @@ */ 'sku_separator' => '', + /* + * The model classes to use if you chose to override them. + * + * For example, if you create your own Inventory class that extends + * Stevebauman\Inventory\Models\Inventory , then you can add: + * 'models' => [ + * 'inventory' => 'App\Inventory' + * ] + * + * All keys are the classnames in snake_case. Any entries that are missing + * will use the default Stevebauman classes. BaseModel cannot be extended in this way. + * + * @var array + */ + 'models' => [ + 'category' => 'Stevebauman\Inventory\Models\Category', + 'inventory' => 'Stevebauman\Inventory\Models\Inventory', + 'inventory_sku' => 'Stevebauman\Inventory\Models\InventorySku', + 'inventory_stock' => 'Stevebauman\Inventory\Models\InventoryStock', + 'inventory_stock_movement' => 'Stevebauman\Inventory\Models\InventoryStockMovement', + 'inventory_transaction' => 'Stevebauman\Inventory\Models\InventoryTransaction', + 'inventory_transaction_history' => 'Stevebauman\Inventory\Models\InventoryTransactionHistory', + 'location' => 'Stevebauman\Inventory\Models\Location', + 'inventory' => 'Stevebauman\Inventory\Models\Inventory', + 'metric' => 'Stevebauman\Inventory\Models\Metric', + 'supplier' => 'Stevebauman\Inventory\Models\Supplier', + ], + + ]; From de9335d8c4d62d26c9a35568eb923e923f5e2876 Mon Sep 17 00:00:00 2001 From: Jon Wiersma Date: Thu, 9 Jul 2015 15:46:21 -0700 Subject: [PATCH 2/3] Updated docs --- docs/INSTALLATION.md | 256 ++++--------------------------------------- 1 file changed, 24 insertions(+), 232 deletions(-) diff --git a/docs/INSTALLATION.md b/docs/INSTALLATION.md index ffd2c042..102f428c 100644 --- a/docs/INSTALLATION.md +++ b/docs/INSTALLATION.md @@ -89,240 +89,32 @@ If you'd like to use them you'll have include them in your use statements: ### I want to customize my models -Create the models, but keep in mind the models need: +Create the models, extend the existing models and add the new models to the models array in app/config/inventory.php. -- The shown fillable attribute arrays (needed for dynamically creating & updating relationship records) -- The shown relationship names (such as `stocks()`) -- The shown relationship type (such as hasOne, hasMany, belongsTo etc) +You are free to modify anything (such as table names, model names, namespace etc!). -You are free to modify anything else (such as table names, model names, namespace etc!). +Example: -Metric: - - class Metric extends Model - { - protected $table = 'metrics'; - } - -Location: - - use Baum\Node; - - class Location extends Node - { - protected $table = 'locations'; - } - -Category: - - use Stevebauman\Inventory\Traits\CategoryTrait; - use Baum\Node; - - class Category extends Node - { - use CategoryTrait; - - protected $table = 'categories'; - - protected $scoped = ['belongs_to']; - - public function inventories() - { - return $this->hasMany('Inventory', 'category_id'); - } - } - -Supplier: - - use Stevebauman\Inventory\Traits\SupplierTrait; - - class Supplier extends BaseModel - { - use SupplierTrait; - - protected $table = 'suppliers'; - - public function items() - { - return $this->belongsToMany('Inventory', 'inventory_suppliers', 'supplier_id')->withTimestamps(); - } - } - -Inventory: - - use Stevebauman\Inventory\Traits\InventoryTrait; - use Stevebauman\Inventory\Traits\InventoryVariantTrait; - - class Inventory extends Model - { - use InventoryTrait; - use InventoryVariantTrait; - - protected $table = 'inventory'; - - public function category() - { - return $this->hasOne('Category', 'id', 'category_id'); - } - - public function metric() - { - return $this->hasOne('Metric', 'id', 'metric_id'); - } - - public function sku() - { - return $this->hasOne('InventorySku', 'inventory_id', 'id'); - } - - public function stocks() - { - return $this->hasMany('InventoryStock', 'inventory_id'); - } - - public function suppliers() - { - return $this->belongsToMany('Supplier', 'inventory_suppliers', 'inventory_id')->withTimestamps(); - } - } - -InventorySku: - - use Stevebauman\Inventory\Traits\InventorySkuTrait; - - class InventorySku extends Model - { - use InventorySkuTrait; - - protected $table = 'inventory_skus'; - - protected $fillable = array( - 'inventory_id', - 'code', - ); - - public function item() - { - return $this->belongsTo('Inventory', 'inventory_id', 'id'); - } - } - InventoryStock: - use Stevebauman\Inventory\Traits\InventoryStockTrait; - - class InventoryStock extends Model - { - use InventoryStockTrait; - - protected $table = 'inventory_stocks'; - - protected $fillable = array( - 'inventory_id', - 'location_id', - 'quantity', - 'aisle', - 'row', - 'bin', - ); - - public function item() - { - return $this->belongsTo('Inventory', 'inventory_id', 'id'); - } - - public function movements() - { - return $this->hasMany('InventoryStockMovement', 'stock_id'); - } - - public function transactions() - { - return $this->hasMany('InventoryTransaction', 'stock_id', 'id'); - } - - public function location() - { - return $this->hasOne('Location', 'id', 'location_id'); - } - } - -InventoryStockMovement: - - use Stevebauman\Inventory\Traits\InventoryStockMovementTrait; - - class InventoryStockMovement extends Model - { - use InventoryStockMovementTrait; - - protected $table = 'inventory_stock_movements'; - - protected $fillable = array( - 'stock_id', - 'user_id', - 'before', - 'after', - 'cost', - 'reason', - ); - - public function stock() - { - return $this->belongsTo('InventoryStock', 'stock_id', 'id'); - } - } - -InventoryTransaction: - - use Stevebauman\Inventory\Traits\InventoryTransactionTrait; - use Stevebauman\Inventory\Interfaces\StateableInterface; - - class InventoryTransaction extends BaseModel implements StateableInterface - { - use InventoryTransactionTrait; - - protected $table = 'inventory_transactions'; - - protected $fillable = array( - 'user_id', - 'stock_id', - 'name', - 'state', - 'quantity', - ); - - public function stock() - { - return $this->belongsTo('InventoryStock', 'stock_id', 'id'); - } - - public function histories() - { - return $this->hasMany('InventoryTransactionHistory', 'transaction_id', 'id'); - } - } - -InventoryTransactionHistory: - - use Stevebauman\Inventory\Traits\InventoryTransactionHistoryTrait; - - class InventoryTransactionHistory extends BaseModel - { - use InventoryTransactionHistoryTrait; - - protected $table = 'inventory_transaction_histories'; - - protected $fillable = array( - 'user_id', - 'transaction_id', - 'state_before', - 'state_after', - 'quantity_before', - 'quantity_after', - ); - - public function transaction() - { - return $this->belongsTo('InventoryTransaction', 'transaction_id', 'id'); - } - } +Create your model: +``` + [ + ... + 'inventory_stock' => 'App\InventoryStock', + ... + ], +... +``` From 84f0beec27c3bbc02c89e31f9f53b328f7dd5997 Mon Sep 17 00:00:00 2001 From: Jon Wiersma Date: Thu, 9 Jul 2015 15:54:38 -0700 Subject: [PATCH 3/3] Removed duplicate entry in config --- src/config/config.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/config/config.php b/src/config/config.php index e2c90bbc..f8d311d8 100644 --- a/src/config/config.php +++ b/src/config/config.php @@ -87,10 +87,8 @@ 'inventory_transaction' => 'Stevebauman\Inventory\Models\InventoryTransaction', 'inventory_transaction_history' => 'Stevebauman\Inventory\Models\InventoryTransactionHistory', 'location' => 'Stevebauman\Inventory\Models\Location', - 'inventory' => 'Stevebauman\Inventory\Models\Inventory', 'metric' => 'Stevebauman\Inventory\Models\Metric', 'supplier' => 'Stevebauman\Inventory\Models\Supplier', ], - ];