Get all related entries that have been used so far

@deprecated Must be refactored.

@param string $tableName The related model
@param string|null $groupField Field to group by
@param string $type Find type
@param array $options
@return \Cake\ORM\Query
192099
public function getRelatedInUse($tableName, $groupField = null, $type = 'all', $options = []) {
		if ($groupField === null) {
			$groupField = $this->belongsTo[$tableName]['foreignKey'];
		}
		$defaults = [
			'contain' => [$tableName],
			'group' => $groupField,
			'order' => isset($this->$tableName->order) ? $this->$tableName->order : [$tableName . '.' . $this->$tableName->getDisplayField() => 'ASC'],
		];
		if ($type === 'list') {
			$defaults['fields'] = [$tableName . '.' . $this->$tableName->getPrimaryKey(), $tableName . '.' . $this->$tableName->getDisplayField()];
			$defaults['keyField'] = $tableName . '.' . $this->$tableName->getPrimaryKey();
			$defaults['valueField'] = $tableName . '.' . $this->$tableName->getDisplayField();
		}
		$options += $defaults;

		return $this->find($type, $options);
	}
1241491
public function getArrayTypeDeclarationSQL(array $field, array $options)
    {
        $type = array_key_exists('type', $options) ? $options['type'] : Type::STRING;
        return 'ARRAY ( ' . Type::getType($type)->getSQLDeclaration($field, $this) . ' )';
    }
868739
public static function get_all($options = []) {
        global $DB;

        $coursecatrecordcache = cache::make('core', 'coursecatrecords');

        $catcontextsql = \context_helper::get_preload_record_columns_sql('ctx');
        $catsql = "SELECT cc.*, {$catcontextsql}
                     FROM {course_categories} cc
                     JOIN {context} ctx ON cc.id = ctx.instanceid";
        $catsqlwhere = "WHERE ctx.contextlevel = :contextlevel";
        $catsqlorder = "ORDER BY cc.depth ASC, cc.sortorder ASC";

        $catrs = $DB->get_recordset_sql("{$catsql} {$catsqlwhere} {$catsqlorder}", [
            'contextlevel' => CONTEXT_COURSECAT,
        ]);

        $types['categories'] = [];
        $categories = [];
        $toset = [];
        foreach ($catrs as $record) {
            $category = new self($record);
            $toset[$category->id] = $category;

            if (!empty($options['returnhidden']) || $category->is_uservisible()) {
                $categories[$record->id] = $category;
            }
        }
        $catrs->close();

        $coursecatrecordcache->set_many($toset);

        return $categories;

    }
1365320
public static function findById($className, $id, array $options): ResultInterface
    {
        list($tableName, , $columnId) = self::getTable($className);
        $instance = self::getInstance($className);

        $query = Query::table($tableName)->className($className)->where($columnId, $id)->selectInstance($instance)->addDecorator(function ($result) {
            if (isset($result[0])) {
                return $result[0];
            }

            return null;
        });

        $options['limit'] = 1;
        $query            = self::applyOptions($query, $options);
        $fields           = self::getFieldsFromOptions($options);

        return $query->get($fields);
    }
1211588
private function get_field_definition( $type, $options = null ) {

		$definition = PodsForm::field_method( $type, 'schema', $options );

		return $this->do_hook( 'field_definition', $definition, $type, $options );
	}
441240
private static function findPluginInstance($field, array $plugin, $tableName)
    {
        if (!isset(self::$_pluginInstances[$tableName][$field])) {
            self::$_pluginInstances[$tableName][$field] = NgRest::createPluginObject($plugin['type']['class'], $plugin['name'], $plugin['alias'], $plugin['i18n'], $plugin['type']['args']);
        }
    
        return self::$_pluginInstances[$tableName][$field];
    }
1131139
public function filterField($fieldName, array $options = array())
    {
        $filter = '';

        $internalType = $this->_context->type($fieldName);

        if (preg_match('/_id$/', $fieldName))
        {
            $filter .= $this->filterSelect($fieldName, $options);
        }
        else
        {
            switch ($internalType)
            {
                case 'datetime':
                    $filter .= $this->filterDatetime($fieldName, $options);
                    break;

                case 'date':
                    $filter .= $this->filterDate($fieldName, $options);
                    break;

                case 'integer':
                case 'tinyinteger':
                case 'smallinteger':
                case 'biginteger':
                    $filter .= $this->filterInteger($fieldName, $options);
                    break;

                case 'float':
                case 'decimal':
                    $filter .= $this->filterFloat($fieldName, $options);
                    break;

                case 'boolean':
                    $filter .= $this->filterBoolean($fieldName, $options);
                    break;

                default:
                    $filter .= $this->filterText($fieldName, $options);
                    break;
            }
        }

        return $filter;
    }
560497
public function versionAssociation($field = null, $options = [])
    {
        $name = $this->_associationName($field);

        if (!$this->_table->associations()->has($name)) {
            $model = $this->_config['referenceName'];

            $options += [
                'className' => $this->_config['versionTable'],
                'foreignKey' => $this->_config['foreignKey'],
                'strategy' => 'subquery',
                'dependent' => true
            ];

            if ($field) {
                $options += [
                    'conditions' => [
                        $name . '.model' => $model,
                        $name . '.field' => $field,
                    ],
                    'propertyName' => $field . '_version'
                ];
            } else {
                $options += [
                    'conditions' => [
                        $name . '.model' => $model
                    ],
                    'propertyName' => '__version'
                ];
            }

            $this->_table->hasMany($name, $options);
        }

        return $this->_table->getAssociation($name);
    }
812167
public function fields( $field = null, $option = null ) {

		$field_data = null;

		if ( empty( $this->fields ) ) {
			// No fields found.
			$field_data = array();
		} elseif ( empty( $field ) ) {
			// Return all fields.
			$field_data = (array) $this->fields;
		} elseif ( ! isset( $this->fields[ $field ] ) && ! isset( $this->pod_data['object_fields'][ $field ] ) ) {
			// Field not found.
			$field_data = array();
		} elseif ( empty( $option ) ) {
			// Return all field data.
			if ( isset( $this->fields[ $field ] ) ) {
				$field_data = $this->fields[ $field ];
			} elseif ( isset( $this->pod_data['object_fields'] ) ) {
				$field_data = $this->pod_data['object_fields'][ $field ];
			}
		} else {
			$options = array();

			// Merge options.
			if ( isset( $this->fields[ $field ] ) ) {
				$options = array_merge( $this->fields[ $field ], $this->fields[ $field ]['options'] );
			} elseif ( isset( $this->pod_data['object_fields'] ) ) {
				$options = array_merge( $this->pod_data['object_fields'][ $field ], $this->pod_data['object_fields'][ $field ]['options'] );
			}

			if ( 'data' === $option && in_array( pods_v( 'type', $options ), PodsForm::tableless_field_types(), true ) ) {
				// Get a list of available items from a relationship field.
				$field_data = PodsForm::field_method( 'pick', 'get_field_data', $options );
			} elseif ( isset( $options[ $option ] ) ) {
				// Return option.
				$field_data = $options[ $option ];
			}
		}//end if

		/**
		 * Modify the field data before returning
		 *
		 * @since unknown
		 *
		 * @param array       $field_data The data for the field.
		 * @param string|null $field      The specific field that data is being return for, if set when method is called or null.
		 * @param string|null $option     Value of option param when method was called. Can be used to get a list of available items from a relationship field.
		 * @param Pods|object $this       The current Pods class instance.
		 */
		return apply_filters( 'pods_pods_fields', $field_data, $field, $option, $this );

	}
1066631
public static function findAll(string $className, array $condition = [], array $options = [])
    {
        $instance = self::getInstance($className);
        $query    = Query::table($className)->className($className)->selectInstance($instance);

        if (!empty($condition)) {
            $query = $query->condition($condition);
        }

        $query  = self::applyOptions($query, $options);
        $fields = self::getFieldsFromOptions($options);

        return $query->get($fields);
    }
1328364
public function getOneResult($table, $where = null, $groupBy = null, $orderBy = null)
    {
        return $this->Select($table, $where, $groupBy, $orderBy, 1);
    }
1222586
public function select($options = [])
    {
        $sql = str_replace(
            ['%TABLE%', '%DISTINCT%', '%FIELD%', '%JOIN%', '%WHERE%', '%GROUP%', '%HAVING%', '%ORDER%', '%LIMIT%', '%UNION%', '%LOCK%', '%COMMENT%', '%FORCE%'],
            [
                $this->parseTable($options['table'], $options),
                $this->parseDistinct($options['distinct']),
                $this->parseField($options['field'], $options),
                $this->parseJoin($options['join'], $options),
                $this->parseWhere($options['where'], $options),
                $this->parseGroup($options['group']),
                $this->parseHaving($options['having']),
                $this->parseOrder($options['order'], $options),
                $this->parseLimit($options['limit']),
                $this->parseUnion($options['union']),
                $this->parseLock($options['lock']),
                $this->parseComment($options['comment']),
                $this->parseForce($options['force']),
            ], $this->selectSql);
        return $sql;
    }
820477
public function select($table = null, $selectFields = array('*'))
    {
    	$this->queryTypeProcess(self::QUERYTYPE_SELECT, $table, $selectFields);
        return $this;
    }
1201357
public function getField($groupKey, $fieldKey)
    {
        foreach ($this->parts as $part) {
            if ($part->getKey() !== $groupKey) {
                continue;
            }
            
            foreach ($part->getParts() as $field) {
                if ($field->getKey() !== $fieldKey) {
                    continue;
                }
                
                return $field;
            }
        }
        
        return false;
    }
803900
public function GetDelete($TableName, $Wheres = array()) {
      $Conditions = '';
      $Joins = '';
      $DeleteFrom = '';
      
      if (count($this->_Joins) > 0) {
         $Joins .= "\n";
         $Joins .= implode("\n", $this->_Joins);
         
         
         $DeleteFroms = array();
         foreach ($this->_Froms as $From) {
            $Parts = preg_split('`\s`', trim($From));
            if (count($Parts) > 1)
               $DeleteFroms[] = $Parts[1].'.*';
            else
               $DeleteFroms[] = $Parts[0].'.*';
         }
         $DeleteFrom = implode(', ', $DeleteFroms);
      }

      if (count($Wheres) > 0) {
         $Conditions = "\nwhere ";
         $Conditions .= implode("\n", $Wheres);

         // Close any where groups that were left open.
         $this->_EndQuery();

      }

      return "delete $DeleteFrom from ".$TableName.$Joins.$Conditions;
   }
692600
public function getMapTypeDeclarationSQL(array $field, array $options)
    {
        $type = array_key_exists('type', $options) ? $options['type'] : MapType::DYNAMIC;

        $fields = array_key_exists('fields', $options) ? $options['fields'] : array();
        $columns = array();
        foreach ($fields as $field) {
            $columns[$field->getQuotedName($this)] = $this->prepareColumnData($field);
        }
        $objectFields = $this->getColumnDeclarationListSQL($columns);

        $declaration = count($columns) > 0 ? ' AS ( ' . $objectFields . ' )' : '';
        return 'OBJECT ( ' . $type . ' )' . $declaration ;
    }
1060925
protected function _getCreateTableSQL($tableName, array $columns, array $options = [])
    {
        $columnListSql = $this->getColumnDeclarationListSQL($columns);

        if (isset($options['uniqueConstraints']) && ! empty($options['uniqueConstraints'])) {
            foreach ($options['uniqueConstraints'] as $name => $definition) {
                $columnListSql .= ', ' . $this->getUniqueConstraintDeclarationSQL($name, $definition);
            }
        }

        if (isset($options['primary']) && ! empty($options['primary'])) {
            $columnListSql .= ', PRIMARY KEY(' . implode(', ', array_unique(array_values($options['primary']))) . ')';
        }

        if (isset($options['indexes']) && ! empty($options['indexes'])) {
            foreach ($options['indexes'] as $index => $definition) {
                $columnListSql .= ', ' . $this->getIndexDeclarationSQL($index, $definition);
            }
        }

        $query = 'CREATE TABLE ' . $tableName . ' (' . $columnListSql;

        $check = $this->getCheckDeclarationSQL($columns);
        if (! empty($check)) {
            $query .= ', ' . $check;
        }
        $query .= ')';

        $sql[] = $query;

        if (isset($options['foreignKeys'])) {
            foreach ((array) $options['foreignKeys'] as $definition) {
                $sql[] = $this->getCreateForeignKeySQL($definition, $tableName);
            }
        }

        return $sql;
    }