XF 2.1 Is there a reverse of addAttachmentsToContent... or is there a way of getting the attachment data related content info using finder?

Kevin

Well-known member
OK, please bear with me on this one as I explain what I'm trying to do...

This is a follow-up to my earlier question where I have a new side table that contains a list of attachment_id values. The table is one-to-one relationship between it and the attachment table using attachment_id as the foreign key relationship. I have an entity definition for the new side table with a relation to XF:Attachment and that relation has a 'with' for the associated Data. In the add-on I'm working on it has corresponding finder & repository classes.

That stuff is all working beautifully and I get my list of rows from my side table with the associated attachments in the relations collection. With what I have in place so far I can display the attachments on in my output page and can work with them as expected.

But... besides the attachment info I'd like to get the content info that the attachment is associated with. If it is a 'post' attachment content_type then I'd like to be able to get the post entity info and it's associated relations so I can grab the thread title, user name, and so on.

I know from my earlier question (by digging into the demo portal code) that if I had collection of, for example, thread entities then I can grab the first post of the thread and use addAttachmentsToContent to retrieve the attachments associated to the post. What I haven't been able to find is if there is any type of function to go the other way around, to retrieve the Content based on the Attachment.

Any suggestions? 🤔
 
Assuming I'm understanding the question correctly, you could do $content = $sideTableEntity->Attachment->getContainer(); which should grab the entity that contains the attachment.
 
Assuming I'm understanding the question correctly, you could do $content = $sideTableEntity->Attachment->getContainer(); which should grab the entity that contains the attachment.
Yep, you understood the question correctly and that does indeed work for what I was after! Thank you 👍


Tangent rant (can be ignored, just Kevin slightly ranting)...

Last night I was working on it and realized that the second half of my problem was that, now that I can get the content entity that the attachment is for, how do I add it to my custom entity so I can return it along with the rest of the info in my view params back to my page. After playing around I realize the answer is "I can't". 🤣 I can't add a blank entity placeholder to my custom entity and populate it later because the content entity can change (post, gallery, whatever else the attachment might be for...) and XF doesn't allow me to easily add to the object on the fly. I then thought of taking my custom entity and using 'toArray' and merging the content entity, arrayed, into the custom entity array but quickly found that entity 'toArray' is not recursive so while it builds an array of the entity object values it ignores the entity relations. Then I thought of doing a 'fetchRaw' on the finder but immediately got an error that my filter option on the finder doesn't work with 'fetchRaw' (that might be something I either missed or have misconfigured, don't know). So I think my final attempted method will be an ugly method, of looping through my finder results, creating a second array variable containing the content info I'm after, and then returning both the finder results and my second array to the view params to the page view and then as I loop through the finder results to reference the second array to grab the content info I'm after. Yeah, ugly, but it's simple and and it should work.
 
how do I add it to my custom entity so I can return it along with the rest of the info in my view params back to my page. After playing around I realize the answer is "I can't". 🤣
In the template, you can use {$yourEntity.Attachment.getContainer()} to get the entity same as in PHP. You can call a lot of functions like that, as long as they have naming prefixes such as get, is, and has.

Then I thought of doing a 'fetchRaw' on the finder but immediately got an error that my filter option on the finder doesn't work with 'fetchRaw' (that might be something I either missed or have misconfigured, don't know).
That is because ->filter() is a method of the AbstractCollection class, and ->fetchRaw() returns a plain array.
 
In the template, you can use {$yourEntity.Attachment.getContainer()} to get the entity same as in PHP. You can call a lot of functions like that, as long as they have naming prefixes such as get, is, and has.
Oooh, something else for me to dig into exploring. :D

From a purely performance perspective, does getting the attachment container in the template versus the controller make a difference?
 
From a purely performance perspective, does getting the attachment container in the template versus the controller make a difference?
Not a single bit. If the container has been fetched in the controller, it'll remain fetched in the template. This is because objects in PHP are always passed by reference, meaning that any modifications to an object (setting a new property, relations are just properties anyway) will carry over from one location to another.
 
Top Bottom