Issue with`$targetIds` when saving a relation
Open
Nobody has claimed this yet.
- Dominant language
- PHP
- Stars
- 3.6k
- Forks
- 705
- Avg merge
- 1d 5h
- Merged PRs (30d)
- 134
Description
public function afterElementSave(ElementInterface $element, bool $isNew)
{
// Skip if nothing changed, or the element is just propagating and we're not localizing relations
/** @var Element $element */
if (
$element->isFieldDirty($this->handle) &&
(!$element->propagating || $this->localizeRelations)
) {
/** @var ElementQuery $value */
$value = $element->getFieldValue($this->handle);
// $id will be set if we're saving new relations
if ($value->id !== null) {
$targetIds = $value->id ?: [];
} else {
$targetIds = $this->_all($value, $element)->ids();
}
/** @var int|int[]|false|null $targetIds */
Craft::$app->getRelations()->saveRelations($this, $element, $targetIds);
// Reset the field value if this is a new element
if ($isNew) {
$element->setFieldValue($this->handle, null);
}
}
parent::afterElementSave($element, $isNew);
}
$targetIds is at the moment required to be an array making impossible pass a model instance to the relationship
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start in src/fields/BaseRelationField.php at afterElementSave(), using the linked code to trace how $targetIds reaches saveRelations(). Confirm the expected relation input types and identify the relevant coverage before changing the save path. Done means a model instance can be passed for a relation without breaking array-based targets.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- php
- Domain
- backend
- Issue type
- Feature
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100