<?php
namespace App\Definition;

use Stringy\Stringy;

trait hasEstado
{
	public function estados()
	{
		$table = $this->getTable();
		$self = Stringy::create(get_class($this));
		$ns = $self->substr(0, $self->indexOfLast('\\'));
		$self = $self->substr($self->indexOfLast('\\') + 1);
		$column = $self->underscored();
		$class = $ns . '\\Estado' . $self;
		if (substr($table, -1, 1) == 's') {
			$column .= '_id';
		}
		
		return $this->has_many($class, $column)->findMany();
	}
	public function estado()
	{
		$table = $this->getTable();
		$self = Stringy::create(get_class($this));
		$ns = $self->substr(0, $self->indexOfLast('\\'));
		$self = $self->substr($self->indexOfLast('\\') + 1);
		$column = $self->underscored();
		$class = $ns . '\\Estado' . $self;
		if (substr($table, -1, 1) == 's') {
			$column .= '_id';
		}
		
		$id = $this->has_many($class, $column)->max('id');
		return $this->has_many($class, $column)->findOne($id);
	}
}
?>