Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

API New $class arg for DataObjectModel->getModelTypeForField() #395

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions src/Schema/DataObject/DataObjectModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -311,13 +311,18 @@ public function getAllOperationIdentifiers(): array
* get that field.
*
* @param string $fieldName
* @param string $class Optional class name for model fields which would result in database queries.
* The database is not always available when the schema is built (e.g. on deployment servers).
* @return ModelType|null
* @throws SchemaBuilderException
*/
public function getModelTypeForField(string $fieldName): ?ModelType
public function getModelTypeForField(string $fieldName, $class = null): ?ModelType
{
$result = $this->getFieldAccessor()->accessField($this->dataObject, $fieldName);
$class = $this->getModelClass($result);
if (!$class) {
$result = $this->getFieldAccessor()->accessField($this->dataObject, $fieldName);
$class = $this->getModelClass($result);
}

if (!$class) {
return null;
}
Expand Down
1 change: 1 addition & 0 deletions src/Schema/Field/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ public function applyConfig(array $config)
'description',
'resolver',
'resolverContext',
'resolvedModelClass',
'plugins',
]);

Expand Down
14 changes: 12 additions & 2 deletions src/Schema/Field/ModelField.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ class ModelField extends Field
*/
private $modelTypeFields = null;

/**
* @var string|null
*/
private $resolvedModelClass = null;

/**
* @var string
*/
Expand All @@ -31,7 +36,7 @@ class ModelField extends Field
* ModelField constructor.
* @param string $name
* @param $config
* @param SchemaModelInterface $model
* @param SchemaModelInterface $model The model containing this field (different from the model this field might resolve to)
* @throws SchemaBuilderException
*/
public function __construct(string $name, $config, SchemaModelInterface $model)
Expand Down Expand Up @@ -71,6 +76,10 @@ public function applyConfig(array $config)
$this->setResolver($this->getModel()->getDefaultResolver($this->getResolverContext()));
}

if (isset($config['resolvedModelClass'])) {
$this->resolvedModelClass = $config['resolvedModelClass'];
}

$this->modelTypeFields = $config['fields'] ?? null;

unset($config['fields']);
Expand Down Expand Up @@ -98,7 +107,8 @@ public function getModelType(): ?ModelType
if (Schema::isInternalType($type)) {
return null;
}
$model = $this->getModel()->getModelTypeForField($this->getName());

$model = $this->getModel()->getModelTypeForField($this->getName(), $this->resolvedModelClass);
if ($model) {
$config = [];
if ($this->modelTypeFields) {
Expand Down