Skip to content

Commit

Permalink
remove invalid attribute only upon database insertion
Browse files Browse the repository at this point in the history
  • Loading branch information
Trexology committed Jan 18, 2016
1 parent 1a5727e commit 5d9b263
Showing 1 changed file with 17 additions and 24 deletions.
41 changes: 17 additions & 24 deletions src/Model/Order.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,12 @@ public function order($user_id, $data = null, $draft = FALSE)
$order->items_number = 0;
$order->items_total = 0;

$attributes = $this->getAllColumnsNames($order);
if ($data) {
foreach ($data as $key => $value) {
if (!array_key_exists($key,$attributes)) {
// remove unrecognized values
unset($data[$key]);
}
}
$order->fill($data);
}

if (!$draft) {
$order = $this->removeInvalidAttributes($order);
$order->state = config("order.init");
$order->save();
}
Expand Down Expand Up @@ -112,14 +106,7 @@ public function addItem($order, Model $object, $price, $qty, $data = null, $vat
{
$orderItem = new OrderItem();

$attributes = $this->getAllColumnsNames($orderItem);
if ($data) {
foreach ($data as $key => $value) {
if (!array_key_exists($key,$attributes)) {
// remove unrecognized values
unset($data[$key]);
}
}
$orderItem->fill($data);
}

Expand All @@ -135,6 +122,7 @@ public function addItem($order, Model $object, $price, $qty, $data = null, $vat
$orderItem->order_id = $order->id;

if ($order->id > 0) {
$orderItem = $this->removeInvalidAttributes($orderItem);
$orderItem->save();
$order = $this->updateOrder($order);
}
Expand All @@ -152,23 +140,16 @@ public function batchAddItems($order, $orderItems)
foreach ($orderItems as $item) {
$orderItem = new OrderItem();

$attributes = $this->getAllColumnsNames($orderItem);
if ($item) {
foreach ($item as $key => $value) {
if (!array_key_exists($key,$attributes)) {
// remove unrecognized values
unset($item[$key]);
}
}
$orderItem->fill($data);
$orderItem->fill($item);
}

$orderItem->total_price = $orderItem->quantity * $orderItem->price;
$orderItem->total_price += $orderItem->total_price * $orderItem->vat;

$orderItem->order_id = $order->id;

if ($order->id > 0) {
$orderItem = $this->removeInvalidAttributes($orderItem);
$orderItem->save();
$order = $this->updateOrder($order);
}
Expand Down Expand Up @@ -277,7 +258,8 @@ public function updateOrder($order)
$order->items_number = $this->count($order);

if ($order->id > 0) {
$order->save(); //unable to save with additional fields
$order = $this->removeInvalidAttributes($order);
$order->save();
}

return $order;
Expand Down Expand Up @@ -344,6 +326,17 @@ public function orderItems()
return $this->hasMany('Trexology\LaravelOrder\Model\OrderItem');
}

public function removeInvalidAttributes(Model $obj)
{
$attributes = $this->getAllColumnsNames($obj);
foreach ($obj->getAttributes() as $key => $value) {
if (!array_key_exists($key,$attributes)) {
unset($obj->$key); // remove unrecognized values
}
}
return $obj;
}

public function getAllColumnsNames($obj)
{
switch (DB::connection()->getConfig('driver')) {
Expand Down

0 comments on commit 5d9b263

Please sign in to comment.