<?php

namespace domain\Product\ProductAccessory;

use common\components\ActiveRecordCommon;
use domain\Product\Accessory\Accessory;
use domain\Product\Product\Product;
use yii\db\ActiveQuery;

class ProductAccessory extends ActiveRecordCommon
{
    public static function tableName()
    {
        return 'product_accessory';
    }

    public function rules()
    {
        return [
            [['id_product', 'id_accessory', 'quantity'], 'required'],
            [['id_product', 'id_accessory', 'quantity'], 'integer'],
        ];
    }

    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'id_product' => 'Produit',
            'id_accessory' => 'Accessoire',
            'quantity' => 'Quantité',
        ];
    }

    /* Getters / Setters */

    public function getId(): ?int
    {
        return $this->id;
    }

    public function getProduct(): Product
    {
        return $this->productRelation;
    }

    public function setProduct(Product $product): self
    {
        $this->populateFieldObject('id_product', 'productRelation', $product);
        return $this;
    }

    public function getAccessory(): Accessory
    {
        return $this->accessoryRelation;
    }

    public function setAccessory(Accessory $accessory): self
    {
        $this->populateFieldObject('id_accessory', 'accessoryRelation', $accessory);
        return $this;
    }

    public function getQuantity(): ?int
    {
        return $this->quantity;
    }

    public function setQuantity(?int $quantity): self
    {
        $this->quantity = $quantity;
        return $this;
    }

    /* Relations */

    public function getProductRelation(): ActiveQuery
    {
        return $this->hasOne(Product::class, ['id' => 'id_product']);
    }

    public function getAccessoryRelation(): ActiveQuery
    {
        return $this->hasOne(Accessory::class, ['id' => 'id_accessory']);
    }
}