49 lines
1.0 KiB
PHP
49 lines
1.0 KiB
PHP
<?php
|
|
|
|
namespace App\Models\Traits;
|
|
|
|
/**
|
|
* Trait ReferenceTrait
|
|
* @property \App\Interfaces\Model $ref_model
|
|
* @property mixed $ref_model_id
|
|
* @package App\Models\Traits
|
|
*/
|
|
trait ReferenceTrait
|
|
{
|
|
/**
|
|
* @param \App\Interfaces\Model $object
|
|
* @return self
|
|
*/
|
|
public function referencesObject($object)
|
|
{
|
|
$this->ref_model = \get_class($object);
|
|
$this->ref_model_id = $object->id;
|
|
$this->save();
|
|
|
|
return $this;
|
|
}
|
|
|
|
/**
|
|
* Return an instance of the object or null
|
|
* @return \App\Interfaces\Model|null
|
|
*/
|
|
public function getReferencedObject()
|
|
{
|
|
if (!$this->ref_model || !$this->ref_model_id) {
|
|
return null;
|
|
}
|
|
|
|
if ($this->ref_model === __CLASS__) {
|
|
return $this;
|
|
}
|
|
|
|
try {
|
|
$klass = new $this->ref_model;
|
|
$obj = $klass->find($this->ref_model_id);
|
|
return $obj;
|
|
} catch (\Exception $e) {
|
|
return null;
|
|
}
|
|
}
|
|
}
|