Get all entries with same Relationship field selection
Posted: 26 March 2008 03:10 PM   [ Ignore ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  720
Joined  08-31-2005

I have a weblog with a Relationship field for “Location”. I’m trying to limit a weblog to only output entries that have a certain location selected. I was thinking I could just use the search parameter

search:location="LocationID"

But I soon realized with some digging in the MySQL tables that it doesn’t store it as the Related entries ID. Instead it makes a copy of the entire Relationship entry into s separate table exp_relationships and just stores the ID of that. Long story short, that means that in the exp_weblog_data table there is not a common ID to search the location field for.

So how can I create a weblog tag that will pull all the entries with the same Location Relationship field?

(Sorry if this isn’t the most elegant wording. Hopefully you can figure out what I mean)

Profile
 
 
Posted: 07 July 2008 08:11 PM   [ Ignore ]   [ # 1 ]  
Lab Technician
Avatar
RankRankRankRank
Total Posts:  2124
Joined  03-26-2006

I need to do the exact same thing. I would rather limit my entries by a custom field that just holds the entry ID of the related entry - NOT the relationship ID - because I can’t loop through reverse_related_entries the way I need to.

I know exactly what you mean - and wish I had an answer. I’m throwing my $0.02 in here in case someone else can come along and enlighten.

You didn’t happen to come upon an elegant solution?

 Signature 

ryan masuga
—————
Masuga Design (EE Pro) | devot:ee
MD Markitup | All Add-Ons
@masuga | @masugadesign | @devot_ee

Profile
 
 
Posted: 07 July 2008 10:00 PM   [ Ignore ]   [ # 2 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  720
Joined  08-31-2005

elegant? no. my solution involved using a custom query.

Here is mine…it isn’t pretty.

{exp:query sql= "SELECT rel_parent_id AS entry FROM exp_relationships WHERE rel_child_id in (SELECT entry_id AS entry FROM exp_weblog_titles WHERE url_title='{segment_2}') AND rel_parent_id in (SELECT rel_parent_id AS entry FROM exp_relationships WHERE rel_child_id in (SELECT entry_id AS entry FROM exp_category_posts WHERE cat_id='{category_id}')) LIMIT 1"}
Profile
 
 
Posted: 07 July 2008 10:16 PM   [ Ignore ]   [ # 3 ]  
Lab Technician
Avatar
RankRankRankRank
Total Posts:  2124
Joined  03-26-2006

My solution was to write a custom extension, which I called “Simple Relation”. It makes a dropdown that pulls from a weblog I specify (the “parent”), and I select an entry from it.

The difference? It only stores the related entry’s ID. Now I can use the weblog:entries search parameter on this field instead of the ridiculous reverse related entries embed contraption I had going on, which didn’t even work. Check the attached pic - can YOU tell which is the “true” relationship field, and which is the “Simple Relation”? wink

Image Attachments
simple_relation.jpg
Click thumbnail to see full-size image
 Signature 

ryan masuga
—————
Masuga Design (EE Pro) | devot:ee
MD Markitup | All Add-Ons
@masuga | @masugadesign | @devot_ee

Profile
 
 
Posted: 07 July 2008 10:40 PM   [ Ignore ]   [ # 4 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  720
Joined  08-31-2005

well… i did get my ridiculous reverse relationship query to work!!! wink

so with your extension, i assume you have to do some sort of embed and use the id in a weblog tag to pull the data out correct? I also assume you would have to build in the logic into the embedded template that would handle if the related entry was deleted… what happens on the control panel side if the selected related item is deleted?

anyway you could rig the extension to handle multiple related selections (using checkboxes instead of having to control click)?

are you going to make this publicly available?... This is way better than what I ended up having to do! What a mess!!!

Profile
 
 
Posted: 07 July 2008 11:06 PM   [ Ignore ]   [ # 5 ]  
Lab Technician
Avatar
RankRankRankRank
Total Posts:  2124
Joined  03-26-2006

Hey, congrats on getting it to work and thanks for sharing, believe me!

Basically, my solution avoids the relationship table entirely (hence the name “Simple Relation”. It’s really no different than having a text field and entering a number in it manually that ties to another entry’s entry_id - except I supply a handy dropdown complete with titles to look at.

I’ll use it like this:

I have a “portfolio” weblog (parent), and “portfolio_images” weblog (all the children that relate to that parent).

I navigate to a single-entry “portfolio” page. On this page in the sidebar, I need to link to all the entries from portfolio_images, sorted by category (I cannot easily do that with reverse_related_entries, which is why I went to the trouble of making this). So I put this on the template:

{embed="portfolio/_gallerylinks" entry_id="{entry_id}"}

Then, the embedded template, I do the category archive tag that loops through the categories and spits out links to each “child entry’s” image:

{exp:weblog:category_archive weblog="portfolio_images" style="linear"}
{categories}
{exp
:weblog:entries weblog="portfolio_images" search:img_rel_proj="{embed:entry_id}"
dynamic="off" status="open" category="{category_id}" orderby="date" sort="desc"}

{if count
== '1'}
<h3>{category_name}</h3>
<
ul>
{/if}

  
<li><a href="{img_image}">{title}</a></li>

{if count == total_results}
</ul>
{/if}
{
/exp:weblog:entries}
{
/categories}
{
/exp:weblog:category_archive}

Note the search parameter in the entries tag in the embedded template:

search:img_rel_proj="{embed:entry_id}"

Because all I’m storing is the entry_id, that field is just saying search:img_rel_proj=“3” or search:img_rel_proj=“22” or whatever entry_id gets fed there. See the attached image for the output of the above code.

I think if the related entry was deleted…you would never see the output of this tag on a template for that entry. As far as the CP…lemme see… looks like if I delete a “parent” entry, the children’s dropdown goes back to “None”, which is good, no?

EDIT: Note, this isn’t a multi-select (real basic…I just made it about an hour ago), and if I can polish a couple more things, I’ll release it for sure. Right now you have to open the extension and manually enter some things, but I’m trying to get it to automate that by creating a custom field type instead.

Image Attachments
simple_relation_output.jpg
Click thumbnail to see full-size image
 Signature 

ryan masuga
—————
Masuga Design (EE Pro) | devot:ee
MD Markitup | All Add-Ons
@masuga | @masugadesign | @devot_ee

Profile
 
 
Posted: 08 July 2008 12:54 PM   [ Ignore ]   [ # 6 ]  
Lab Technician
Avatar
RankRankRankRank
Total Posts:  2124
Joined  03-26-2006

In case you didn’t get the news, I released Simple Relation this morning.

 Signature 

ryan masuga
—————
Masuga Design (EE Pro) | devot:ee
MD Markitup | All Add-Ons
@masuga | @masugadesign | @devot_ee

Profile
 
 
Posted: 08 July 2008 12:55 PM   [ Ignore ]   [ # 7 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  720
Joined  08-31-2005

That was quick!

Profile
 
 
Posted: 14 July 2008 10:04 AM   [ Ignore ]   [ # 8 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  671
Joined  06-29-2005

I’m not clear in understanding why you need to use “search” to accomplish this. I am doing what sounds like it is very similar using a straight-ahead reverse-relationships template entry.

Check this page: http://ncwildflower.org/index2.php/plants/details/hymenocallis-crassifolia/, or for more plant images, this page: http://ncwildflower.org/index2.php/plants/details/franklinia-alatamaha/

I have two weblogs going here. The first holds the plant data, the second holds the plant images. The second has a relationship field to the title field of the plant data weblog. When I enter a plant into the plant image publish form, there is a drop-down list that shows all the scientific names of the plants entered in the plants data weblog so far.

This is used to isolate the images so that they relate to that one plant’s scientific name only.

Then in the display template, I use a reverse-related entry to show all of a particular scientific name’s plant images. It works like magic with no fiddling needed.

{reverse_related_entries status="open" sort="asc" orderby="date"}
  
<tr>
    <
td colspan="2">
  
{if plant_image_caption}
      
<p class="caption">{plant_image_caption}</p>
  
{/if}
  {if plant_image_subcaption}
      
<p>{plant_image_subcaption}</p>
  
{/if}
      
<p>{plant_image}</p>
  
{if plant_image_credit}
      
<p class="credit">{plant_image_credit}</p>
  
{/if}
  {if plant_image_info}
      
<p class="info">{plant_image_info}</p>
  
{/if}
    
</td>
  </
tr>
{/reverse_related_entries}

This, of course, resides between exp:weblog:entries tags that are pulling from the plant data weblog.

I was most surprised that I didn’t have to specify which weblog the plants images came from or anything, just supply the field names to pull the data from them. That seemed beyond my expectations, so, well, I didn’t expect it to work! (I just went ahead and tried the code as pasted from the docs).

Am I accomplishing the same aim you had?

Terry

 Signature 

Terry Leigh Britton
http://twitter.com/TerryBritton
http://twitter.com/Terr_ee

Search the web using Terry Leigh Britton to find all kinds of stuff I’m into!
Or use Yasni for Terry Leigh Britton

Profile
 
 
Posted: 24 July 2008 02:56 AM   [ Ignore ]   [ # 9 ]  
Lab Technician
Avatar
RankRankRankRank
Total Posts:  1917
Joined  01-05-2007

Not sure if this will be helpful for me as I don’t use categories but instead I have 3 weblogs -

Artists
Gallery
Exhibition

the first holds the Artists details
the second holds individual pieces or art with details and is related to an Artist
the third holds details on the exhibition and can be related to more than one Artist

I want to create a page which shows the latest exhibition along with the artist that is featuring in that exhibition and also a piece of art related to that artist.

At the moment I have embedded templates coming out of my ears. Could this be a better way?

 Signature 

 
Steven Hambleton | Follow me on Twitter!


ExpressionEngine Outsourcing for Marketing, Graphic & Web Agencies
Partner with a professional ExpressionEngine development team to turn your designs into functioning sites.

Profile
 
 
Posted: 24 July 2008 06:43 PM   [ Ignore ]   [ # 10 ]  
Lab Technician
Avatar
RankRankRankRank
Total Posts:  1917
Joined  01-05-2007

Doesn’t matter. I have worked it out another way.

 Signature 

 
Steven Hambleton | Follow me on Twitter!


ExpressionEngine Outsourcing for Marketing, Graphic & Web Agencies
Partner with a professional ExpressionEngine development team to turn your designs into functioning sites.

Profile
 
 
Posted: 24 July 2008 08:22 PM   [ Ignore ]   [ # 11 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  671
Joined  06-29-2005
stinhambo - 24 July 2008 06:43 PM

Doesn’t matter. I have worked it out another way.

How, pray tell, sir?

Terry

 Signature 

Terry Leigh Britton
http://twitter.com/TerryBritton
http://twitter.com/Terr_ee

Search the web using Terry Leigh Britton to find all kinds of stuff I’m into!
Or use Yasni for Terry Leigh Britton

Profile
 
 
Posted: 24 July 2008 08:26 PM   [ Ignore ]   [ # 12 ]  
Lab Technician
Avatar
RankRankRankRank
Total Posts:  1917
Joined  01-05-2007

I used an embed and passed the entry_id as a variable (using the Playa Extension by the way) -

{exp:weblog:entries weblog="homepage" entry_id="62" disable="categories|member_data|pagination|trackbacks"}
    {related_entries id
="homepage_choice"}
        {if weblog_short_name
== "exhibitions"}
            {embed
="includes/homepage_exhibition" exhibition_item="{entry_id}"}
          {if
:elseif weblog_short_name == "gallery"}
            {embed
="includes/homepage_gallery" gallery_item="{entry_id}"}
        {
/if}
    {
/related_entries}     
{
/exp:weblog:entries}

Then this is one of the embedded entries -

{exp:weblog:entries weblog="exhibitions" entry_id="{embed:exhibition_item}" show_future_entries="yes" show_expired="yes" disable="member_data|pagination|trackbacks"}
    
<a href="{url_title_path=">{related_entries id="exhibition_intro_image_rel"}<img src="{artists_works_piece}" alt="{title} {gallery_year}">{/related_entries}</a>
    <
p style="text-align: right;">
    <
a href="{url_title_path=">
    <
strong>{related_entries id="exhibitions_related_artist"}{title}{if count >= 0 AND count < "total_results"} - {/if}{/related_entries}{title}</strong></a><br />
    
{exp:nice_date date="{entry_date format="%Y-%m-%d"}" format="%F %j%S" localize="yes"} - {exp:nice_date date="{expiration_date format="%Y-%m-%d"}" format="%F %j%S, %Y" localize="yes"}
    
</p>
{/exp:weblog:entries}

PS. The HTML wasn’t mine to start with so don’t mention that!

 Signature 

 
Steven Hambleton | Follow me on Twitter!


ExpressionEngine Outsourcing for Marketing, Graphic & Web Agencies
Partner with a professional ExpressionEngine development team to turn your designs into functioning sites.

Profile
 
 
Posted: 20 January 2009 02:35 AM   [ Ignore ]   [ # 13 ]  
Grad Student
Avatar
Rank
Total Posts:  63
Joined  05-31-2007

James,

I had a situation that seems to be identical to yours, and spent a lot of time coming up with a way to get, for a specific weblog, all entries that have a specific relationship field’s value set to _X_.  I came up with a complicated SQL/Query module solution, but then I realized that reverse_related_entries is really all I needed for this.

For those of you that are trying to do the same thing but with categories instead of relationship fields, I am not sure this applies.

{exp:weblog:entries weblog="product_type" dynamic="off" url_title="{segment_3}"}
    <?php $outer_title
= "{title}"; ?>   
    {reverse_related_entries}
        Title
: {title}<br />
        
Product Line: <?php echo $outer_title; ?>
        
<hr />   
    
{/reverse_related_entries}
{
/exp:weblog:entries}

This code actually shows all the Products that have their “Product Type Relationship Field” set to whatever is is in segment_2. 

Let me know if this is the same problem you faced or not.

 Signature 

Need EE Help? Hire me: Jacksonville Website Design
Install Freo Add-on Installer to make installing Modules, Extensions, & Plugins a breeze!

Profile
 
 
Posted: 18 February 2010 10:54 AM   [ Ignore ]   [ # 14 ]  
Summer Student
Avatar
Total Posts:  12
Joined  10-16-2009

Does anybody know of a way to organize reverse related entries by category? Kinda like how a category archive works, only with reverse related entries.

Profile
 
 
   
 
 
Post Marker Legend
New Topic New posts Hot Topic Hot Topic with new posts New Poll New Poll Moved Topic Moved Topic Sticky Topic Sticky topic
Old Topic No new posts Hot Old Topic Hot Topic with no new posts Old Poll Old Poll Closed Topic Closed Topic Announcement Announcements
Theme
Change Theme
Visitor Statistics
The most visitors ever was 1743, on December 02, 2009 03:47 PM
Total Registered Members: 120363 Total Logged-in Users: 74
Total Topics: 126488 Total Anonymous Users: 35
Total Replies: 665186 Total Guests: 345
Total Posts: 791674    
Members ( View Memberlist )