<?php
namespace Entity;

class SupplierContract extends \Entity\PartnerContractAbstract
{

	/**
	 * Indicate if the operations available for the current entity must be done inside a transaction or not.
	 * @var Boolean
	 */
	protected $saveInsideTransaction = true;

	protected $partnerRefColumnName = 'idSupplier';

	/**
	 * (non-PHPdoc)
	 * @see App\Entity.SelectAbstract::setTableName()
	 */
	protected function setTableName(): void
	{
		global $DBTSuppliersContracts;
		$this->tableName = $DBTSuppliersContracts;
	}

	/**
	 * Return the storage path root
	 */
	public function getFileStoragePath()
	{
		return FILEPATH_SUPPLIER_CONTRACTS;
	}

	/**
	 * (non-PHPdoc)
	 * @see Entity.PartnerContractAbstract::getAttribute()
	 */
	protected function getAttribute($var)
	{
		switch ($var) {
			// supplier
			case 'supplier' : {
				return \Entity\Supplier::selectForUpdate($this->fields['idSupplier']);
			}

			default : {
				return parent::getAttribute($var);
			}
		}
	}

	/**
	 * (non-PHPdoc)
	 * @see Entity.PartnerContractAbstract::setAttribute()
	 */
	protected function setAttribute($var, $value)
	{
		// check that operation is okay
		if ($this->operation != 'insert' and $this->operation != 'update') {
			throw new \Exception('You can use __set magic method only with insert/update operation in `'.__CLASS__.'` class.');
		}

		switch ($var) {
			case 'idSupplier' : {
				$this->fields[$var] = $value;
				break;
			}

			default : {
				parent::setAttribute($var, $value);
			}
		}
	}

	/**
	 * (non-PHPdoc)
	 * @see Entity.PartnerContractAbstract::save_postProcess()
	 */
	protected function save_postProcess(Array $saveData)
	{
		// delete cache
		$this->save_updateCache_addEntry('Supplier', \Entity\SupplierContract::select($this->id)->idSupplier);

		parent::save_postProcess($saveData);

	}

}