This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.
I have a controller which implements both FormController
and RelationController
behaviors.
In the update and create pages, I want to add a new relationship, and refresh the relations list from a custom ajax function. This is the code that I have in the ajax handler:
if ($recordId !== null) {
$wordModel = $this->formFindModelObject($recordId);
} else {
$wordModel = new WordModel();
}
$this->initForm($wordModel);
// $this->initRelation($wordModel, 'meanings'); => It's not necessary to have this line, so I commented it
$sessionKey = $this->formGetSessionKey();
$meaningsRelation = $wordModel->meanings();
if ($context === 'update') {
$meaningsRelation->syncWithoutDetaching([$meaningWordId]);
} else {
$word = WordModel::where('id', '=', $meaningWordId)->firstOrFail();
$meaningsRelation->add($word, $sessionKey);
}
return $this->relationRefresh('meanings');
When $context
is "update
" (I am in update page), the above code works perfectly, and the relation list is refreshed successfully. But when $context
is "create
", the relation list is not refreshed, even though the relationship has been added. Am I doing something wrong?
You probably need to use a deferred binding with a session key.
ref. https://octobercms.com/docs/database/relations#deferred-binding
Yes, but did you enable deferred binding in your controller? Make sure you actually are getting a session key in your handler.
As you can see in my initial post, I have commented that line because LukeTowers
and daftspunk
believe that it's not necessary: https://github.com/octobercms/october/issues/1752#issuecomment-453680762
Now it works, the problem was the wrong relation name "meanings
" in this line:
$meaningsRelation = $wordModel->meanings();
I changed it to "english_meanings()
" and everything worked!
For future reference, if you want to refresh your relation manager list (e.g. list of tags for a blog post) using a custom ajax handler, do this:
public function onAddTag($recordId = null)
{
if ($recordId !== null) {
$blogPostModel = $this->formFindModelObject($recordId);
} else {
$blogPostModel = $this->formCreateModelObject();
}
$this->initForm($blogPostModel);
$sessionKey = $this->formGetSessionKey();
$tagsRelation = $blogPostModel->tags();
$tagId = post('tag_lookup');
$context = $this->action;
if (is_numeric($tagId)) {
if ($context === 'update') {
$tagsRelation->syncWithoutDetaching([$tagId]);
} else {
$tag = TagModel::find($tagId);
$tagsRelation->add($tag, $sessionKey);
}
} else {
// if the word that user selected, was not in the list, tagId does not contain any ID,
// but it's the actual new tag the needs to be added to the tags table
$newTag = TagModel::create(
[
'title' => $tagId,
]
);
if ($context === 'update') {
$tagsRelation->attach($newTag->id);
} else {
$tagsRelation->add($newTag, $sessionKey);
}
}
// refresh the relation manager list
return $this->relationRefresh('tags');
}
Last updated
1-9 of 9