Returning one to many relationship
I am trying to get my head around how to do this...
I have a Project pod and a People pod. I want to return all the people you have a particular project. I have a PICK column in my People pod to choose a project. I am wanting to display these on a project page. Do I have have a new Pod('people') along with new Pod('projects' )in order to get the people too?
Thanks for suggestions.
Shawn
12 Answers
No. You can do this:
$pod = new Pod('project', 1);
$project = $pod->get_field('name');
$people = $pod->get_field('people.name');
echo "$project is being worked on by these people: " . implode(', ', $people);
edited Dec 8 '11 at 1:23
Awesome. Thanks Chris. I will try that. Slowly learning...
So when you create a PICK column, they two pods are automatically joined, right?
I am not getting anything returned, but still working on it.
Yes, when you create a Pick relationship, Pods automatically creates the joins needed to display the data.
I just can't get it to work. I don't need to have a Pick column in the Project POD too? It seems simple, but no results are getting returned.
Here is the link to the page if that might help... http://test.cmfi.org.php5-10.websitetestlink.com/partner/buv-tanzania-transportation-business
There should be at least one name listed there.
global $pods;
$project_slug = pods_url_variable(last);
$project = new Pod('projects', $project_slug);
//echo $project_slug;
if( !empty( $project->data ) )
{
$found_project = true;
// set our variables
$project_name = $project->get_field('name');
$project_location = $project->get_field('location.name');
$project_projecttype = $project->get_field('projecttype.name');
$project_description = $project->get_field('projectdesc');
$project_summary = $project->get_field('summarydesc');
$project_mpowercode = $project->get_field('mpowercode');
$project_image = $project->get_field('projectimage');
$project_fundinggoal = $project->get_field('projectfundinggoal');
$project_featured = $project->get_field('featuredproject');
$project_people = $project->get_field('people.name');
<?php echo $project_name . ' is being worked on by these people: ' . implode(', ', $project_people); ?>
Above are the relevant parts of my code. Do you see anything wrong?
Thanks again.
Everything looks good in your last post. I reread your original question after looking at this, and I think I see where your problem is:
You are selecting the Project from the People pod, but you want to display the People from the Project pod. To make this work your Pick needs to be bidirectional. If you don't, Pods won't automatically create the joins needed.
If not, you'd need to have something like this:
$people_pod = new Pod ('people');
$project_id = $project->get_field('id');
$people_pod->findRecords('id ASC', -1, "project.id == $project_id");
Just because I am trying to understand...
When you say bi-directional is it like this in relational db terms:
Project table: Project.id, Project.name, People.id
People table: People.id, People.name, Project.id
Right now, according to above, I am missing the Project.People.id column. So I need to just create a PICK column in the Project POD that relates to the People POD. Don't necessarily have to select all the people related to a project and vice versa in their respective tables, correct? So the PICK column in the Project POD is there to just create the join.
edited Dec 9 '11 at 1:54
To have a bi-directional relationship in Pods, you need to create a Pick column in both pods. In your case the people.projects needs to be a multi-select pick. Your project.people may or may not need to be one. You'll have more than just the joins for your display query. From the manage screen of either pod, you'll be able to set the people working on a project or the projects people are working on.
If you need more help, see the user guide for info on setting up a bidirectional pick. (It's at the bottom of the page).
Thanks Chris. I am following the user guide and have a questions about step 6, the second part.
...now go to the second drop-down below the drop-down you select the related Pod / WP Object -- Select the first field's name from the list
I don't have another drop-down. I have ColumnType (chose Relationship PICK), Related To (chose People or Project). I don't see where to select the field's name from a list.
I got it to work. I deleted the pick column and recreated it. Then the menu to pick the field name showed up. Now just working on it returning data...
Chris, Was the last example you gave for if I was not using a bidirectional PICK column?
Should my code from above work if the PICK is bidirectional?


