Bravo3\Orm\Services\QueryManager::persistTableQueries PHP Method

persistTableQueries() public method

Persist entity indices
public persistTableQueries ( object $entity, Entity $metadata = null, Reader $reader = null, string $local_id = null )
$entity object Local entity object
$metadata Bravo3\Orm\Mappers\Metadata\Entity Optionally provide entity metadata to prevent recalculation
$reader Bravo3\Orm\Services\Io\Reader Optionally provide the entity reader
$local_id string Optionally provide the local entity ID to prevent recalculation
    public function persistTableQueries($entity, Entity $metadata = null, Reader $reader = null, $local_id = null)
    {
        /** @var $metadata Entity */
        list($metadata, $reader, $local_id) = $this->buildPrerequisites($entity, $metadata, $reader, $local_id);
        foreach ($metadata->getSortables() as $sortable) {
            $key = $this->getKeyScheme()->getTableSortKey($metadata->getTableName(), $sortable->getName());
            // Test conditions
            $pass = true;
            foreach ($sortable->getConditions() as $condition) {
                if ($condition->getColumn()) {
                    if (!$condition->test($reader->getPropertyValue($condition->getColumn()))) {
                        $pass = false;
                        break;
                    }
                } elseif ($condition->getMethod()) {
                    if (!$condition->test($reader->getMethodValue($condition->getMethod()))) {
                        $pass = false;
                        break;
                    }
                }
            }
            if ($pass) {
                // Conditions met, add the index
                $this->getDriver()->addSortedIndex($key, $reader->getPropertyValue($sortable->getColumn()), $local_id);
            } else {
                // Conditions failed, remove the index
                $this->getDriver()->removeSortedIndex($key, $local_id);
            }
        }
        return $this;
    }