<?php
/* This file has been auto-generated. Do not edit this file directly. */
namespace SabaiApps\Directories\Component\Entity\Model\Base;

use SabaiApps\Framework\Model\Model;
use SabaiApps\Framework\Criteria\AbstractCriteria;
use SabaiApps\Framework\DB\AbstractRowset;
use SabaiApps\Framework\Model\AbstractEntity;
use SabaiApps\Framework\Model\AbstractEntityRepository;

abstract class Field extends AbstractEntity
{
    public function __construct(Model $model)
    {
        parent::__construct('Field', $model);
        $this->_vars = ['field_data' => null, 'field_id' => 0, 'field_created' => 0, 'field_updated' => 0, 'field_bundle_name' => null, 'field_fieldconfig_name' => null];
    }

    public function __clone()
    {
        $this->_vars = ['field_id' => 0, 'field_created' => 0, 'field_updated' => 0] + $this->_vars;
    }

    public function __toString()
    {
        return 'Field:' . $this->__get('id');
    }

    public function __get($name)
    {
        if ($name === 'data')
            return $this->_vars['field_data'];
        elseif ($name === 'id')
            return $this->_vars['field_id'];
        elseif ($name === 'created')
            return $this->_vars['field_created'];
        elseif ($name === 'updated')
            return $this->_vars['field_updated'];
        elseif ($name === 'bundle_name')
            return $this->_vars['field_bundle_name'];
        elseif ($name === 'fieldconfig_name')
            return $this->_vars['field_fieldconfig_name'];
        elseif ($name === 'Bundle')
            return $this->_fetchEntity('Bundle', 'bundle_name');
        elseif ($name === 'FieldConfig')
            return $this->_fetchEntity('FieldConfig', 'fieldconfig_name');
        else
            return $this->fetchObject($name);
    }

    public function __set($name, $value)
    {
        if ($name === 'data')
            $this->_setVar('field_data', $value);
        elseif ($name === 'id')
            $this->_setVar('field_id', $value);
        elseif ($name === 'bundle_name')
            $this->_assignEntityById('Bundle', $value, 'field_bundle_name');
        elseif ($name === 'Bundle') {
            $_value = is_array($value) ? $value[0] : $value;
            if (is_object($_value)) {
                $this->_assignEntity($_value, 'field_bundle_name');
            } else {
                $this->_assignEntityById('Bundle', $_value, 'field_bundle_name');
            }
        }
        elseif ($name === 'fieldconfig_name')
            $this->_assignEntityById('FieldConfig', $value, 'field_fieldconfig_name');
        elseif ($name === 'FieldConfig') {
            $_value = is_array($value) ? $value[0] : $value;
            if (is_object($_value)) {
                $this->_assignEntity($_value, 'field_fieldconfig_name');
            } else {
                $this->_assignEntityById('FieldConfig', $_value, 'field_fieldconfig_name');
            }
        }
        else
            $this->assignObject($name, $value);
    }

    protected function _initVar($name, $value)
    {
        if ($name === 'field_data')
            $this->_vars['field_data'] = @unserialize($value);
        elseif ($name === 'field_id')
            $this->_vars['field_id'] = (int)$value;
        elseif ($name === 'field_created')
            $this->_vars['field_created'] = (int)$value;
        elseif ($name === 'field_updated')
            $this->_vars['field_updated'] = (int)$value;
        else
            $this->_vars[$name] = $value;
    }
}

abstract class FieldRepository extends AbstractEntityRepository
{
    public function __construct(Model $model)
    {
        parent::__construct('Field', $model);
    }

    public function fetchByBundle($id, $limit = 0, $offset = 0, $sort = null, $order = null)
    {
        return $this->_fetchByForeign('field_bundle_name', $id, $limit, $offset, $sort, $order);
    }

    public function paginateByBundle($id, $perpage = 10, $sort = null, $order = null)
    {
        return $this->_paginateByEntity('Bundle', $id, $perpage, $sort, $order);
    }

    public function countByBundle($id)
    {
        return $this->_countByForeign('field_bundle_name', $id);
    }

    public function fetchByBundleAndCriteria($id, AbstractCriteria $criteria, $limit = 0, $offset = 0, $sort = null, $order = null)
    {
        return $this->_fetchByForeignAndCriteria('field_bundle_name', $id, $criteria, $limit, $offset, $sort, $order);
    }

    public function paginateByBundleAndCriteria($id, AbstractCriteria $criteria, $perpage = 10, $sort = null, $order = null)
    {
        return $this->_paginateByEntityAndCriteria('Bundle', $id, $criteria, $perpage, $sort, $order);
    }

    public function countByBundleAndCriteria($id, AbstractCriteria $criteria)
    {
        return $this->_countByForeignAndCriteria('field_bundle_name', $id, $criteria);
    }

    public function fetchByFieldConfig($id, $limit = 0, $offset = 0, $sort = null, $order = null)
    {
        return $this->_fetchByForeign('field_fieldconfig_name', $id, $limit, $offset, $sort, $order);
    }

    public function paginateByFieldConfig($id, $perpage = 10, $sort = null, $order = null)
    {
        return $this->_paginateByEntity('FieldConfig', $id, $perpage, $sort, $order);
    }

    public function countByFieldConfig($id)
    {
        return $this->_countByForeign('field_fieldconfig_name', $id);
    }

    public function fetchByFieldConfigAndCriteria($id, AbstractCriteria $criteria, $limit = 0, $offset = 0, $sort = null, $order = null)
    {
        return $this->_fetchByForeignAndCriteria('field_fieldconfig_name', $id, $criteria, $limit, $offset, $sort, $order);
    }

    public function paginateByFieldConfigAndCriteria($id, AbstractCriteria $criteria, $perpage = 10, $sort = null, $order = null)
    {
        return $this->_paginateByEntityAndCriteria('FieldConfig', $id, $criteria, $perpage, $sort, $order);
    }

    public function countByFieldConfigAndCriteria($id, AbstractCriteria $criteria)
    {
        return $this->_countByForeignAndCriteria('field_fieldconfig_name', $id, $criteria);
    }

    protected function _getCollectionByRowset(AbstractRowset $rs)
    {
        return new FieldsByRowset($rs, $this->_model->create('Field'), $this->_model);
    }

    public function createCollection(array $entities = [])
    {
        return new Fields($this->_model, $entities);
    }
}

class FieldsByRowset extends \SabaiApps\Framework\Model\EntityCollection\RowsetEntityCollection
{
    public function __construct(AbstractRowset $rs, Field $emptyEntity, Model $model)
    {
        parent::__construct('Fields', $rs, $emptyEntity, $model);
    }
}

class Fields extends \SabaiApps\Framework\Model\EntityCollection\ArrayEntityCollection
{
    public function __construct(Model $model, array $entities = [])
    {
        parent::__construct($model, 'Fields', $entities);
    }
}