Introduce endpoints for managing product options and department categories, including listing, creating, editing, and deleting functionalities. Updated related objects and traits to support new operations, including added array serialization methods and improved query handling for better flexibility.
92 lines
2.6 KiB
PHP
92 lines
2.6 KiB
PHP
<?php
|
|
|
|
namespace objects;
|
|
|
|
use classes\db;
|
|
use classes\object_property;
|
|
use Exception;
|
|
use traits\db_object_t;
|
|
|
|
class product_options_o extends db
|
|
{
|
|
use db_object_t;
|
|
|
|
public object_property $product_id;
|
|
public object_property $option_id;
|
|
public object_property $name;
|
|
|
|
public function structure(): void
|
|
{
|
|
$this->setTable('products_options');
|
|
}
|
|
|
|
/**
|
|
* Add a product option, and set this object to the new object
|
|
* @param int $product_id
|
|
* @param int $option_id
|
|
* @return void
|
|
* @throws Exception If the object was not created successfully
|
|
*/
|
|
public function add(int $product_id, int $option_id): void
|
|
{
|
|
$tmp_id = self::add_object([
|
|
'product_id' => $product_id,
|
|
'option_id' => $option_id
|
|
]);
|
|
$this->id = $tmp_id;
|
|
self::getObjectProperties();
|
|
self::objectChanged();
|
|
}
|
|
|
|
public function getObjectProperties(): void
|
|
{
|
|
$this->product_id = new object_property($this->table, $this->id, 'product_id', 'int', false);
|
|
$this->option_id = new object_property($this->table, $this->id, 'option_id', 'int', false);
|
|
$this->name = new object_property($this->table, $this->id, 'name', 'string', false);
|
|
}
|
|
|
|
public function objectChanged(): void
|
|
{
|
|
//TODO: Add cache invalidation
|
|
}
|
|
|
|
/**
|
|
* Set the name of the product option
|
|
* @param string $name
|
|
* @throws Exception If the object was not selected
|
|
*/
|
|
public function set_name(string $name): void
|
|
{
|
|
self::requireSelected();
|
|
$this->name->set($name);
|
|
self::objectChanged();
|
|
}
|
|
|
|
public function getProductOptions(int $id): array
|
|
{
|
|
$options = self::getFieldsWhere(['product_id' => $id],
|
|
[
|
|
'id',
|
|
'product_id',
|
|
'option_id',
|
|
'name',
|
|
'created_at',
|
|
'updated_at'
|
|
]
|
|
);
|
|
$tmp = [];
|
|
foreach ( $options as $option ) {
|
|
$tmp_product = (new products_o())->select($option['option_id'])->asArray();
|
|
$tmp[] = [
|
|
'id' => (int)$option['id'],
|
|
'product_id' => (int)$option['product_id'],
|
|
'option_id' => (int)$option['option_id'],
|
|
'name' => (string)$option['name'] === '' ? $tmp_product['name'] : (string)$option['name'],
|
|
'created_at' => (string)$option['created_at'],
|
|
'updated_at' => (string)$option['updated_at'],
|
|
'product' => $tmp_product
|
|
];
|
|
}
|
|
return $tmp;
|
|
}
|
|
} |