Kevin
Well-known member
First, what I'm trying to do...
I'm working on an add-on that uses a side-table that is related to attachments. It'll be a one-to-one relationship between the new side-table and the attachment table using attachment_id as the foreign key relationship. I've created an entity definition for my new side-table and have it defined in content types. I've created a class extension to add the new entity as a relation to xf_attachment in the getStructure function. I believe that is all working as expected since if I dump the value of $structure in my class extension I can see the new relation entry in the array (by default 'Data' is relation in Attachment so the end result is that I see both 'Data' in the relations array and my new entry).
My problem, or at least my stumbling block I'm running into, is how to get the relation actually included in the Attachment entity when it is retrieved.
Using the demo portal as an example, it uses this bit to retrieve an array of Posts entities and then to associate the attachments to each post in the array.
Taking a look at the addAttachmentsToContent function in XF\Repository\Attachment I see it looping through the passed in array then finding the associated attachments based on the 'post' content_type and content_id matching the passed in post_id. When the attachment entities collection is then added to the relations array they include the 'Data' relation entity.
They don't include my new relation. And that is the part I'm trying to figure out. From poking around I think I need the hydrateRelation but am not totally sure about that or how I'd go about implementing to retrieve the new relation of the attachment entity that itself is in the relation array of the post entity.
So can anybody please give some input on this one... did I miss something and I'm over complicating matters? Is hydrateRelation what I'm after or I'm looking in the wrong spot? If it is what I'm after where would be the spot where I'd need to inject it to retrieve the data for my new relation entity to the attachment when, for example, the attachment is retrieved as part of a post entity? I've been searching the forums and I'm seeing examples of people adding attachment entities to their new custom entity types but nothing about adding a new custom entity relation to the attachment entity.
Thanks
I'm working on an add-on that uses a side-table that is related to attachments. It'll be a one-to-one relationship between the new side-table and the attachment table using attachment_id as the foreign key relationship. I've created an entity definition for my new side-table and have it defined in content types. I've created a class extension to add the new entity as a relation to xf_attachment in the getStructure function. I believe that is all working as expected since if I dump the value of $structure in my class extension I can see the new relation entry in the array (by default 'Data' is relation in Attachment so the end result is that I see both 'Data' in the relations array and my new entry).
My problem, or at least my stumbling block I'm running into, is how to get the relation actually included in the Attachment entity when it is retrieved.
Using the demo portal as an example, it uses this bit to retrieve an array of Posts entities and then to associate the attachments to each post in the array.
Code:
$threads = $featuredThreads->pluckNamed('Thread');
$posts = $threads->pluckNamed('FirstPost', 'first_post_id');
/** @var \XF\Repository\Attachment $attachRepo */ $attachRepo = $this->repository('XF:Attachment');
$attachRepo->addAttachmentsToContent($posts, 'post');
They don't include my new relation. And that is the part I'm trying to figure out. From poking around I think I need the hydrateRelation but am not totally sure about that or how I'd go about implementing to retrieve the new relation of the attachment entity that itself is in the relation array of the post entity.
So can anybody please give some input on this one... did I miss something and I'm over complicating matters? Is hydrateRelation what I'm after or I'm looking in the wrong spot? If it is what I'm after where would be the spot where I'd need to inject it to retrieve the data for my new relation entity to the attachment when, for example, the attachment is retrieved as part of a post entity? I've been searching the forums and I'm seeing examples of people adding attachment entities to their new custom entity types but nothing about adding a new custom entity relation to the attachment entity.
Thanks