Code to return associations from Express Objects

Permalink
Hi All

I've been a developer for a few years now but am new to Concrete5. I've been following the tutorials but am struggling to return data held against express object associations. To explain, Im building a dance school site and have the following setup for express objects:

Course:
|_ Title (attribute)
|_ Start Date (attribute)
|_ Cost (attribute)
|_ Course Image (attribute)
|_ Dance School (Express Object)
|_ Venue (Express Object)
|_ Name (Attribute)
|_ Teachers (Express Object)
|_ Name (Attribute)

So I have taken the express_enitty_list view code and I'm creating a custom template in the application folder. So far this is my code:

$results = $result->getItemListObject()->getResults();
$im = Loader::helper('image');
if (count($results)) {
?>
<div class="row">
<?php
$rowClass = 'ccm-block-express-entry-list-row-a';
foreach($results as $idx => $course):
?>
<div class="col-sm-4">
<h2><?php echo $course->getCourseTitle(); ?> - <?php echo date_format($course->getStartDate(),'d/m/Y'); ?></h2>
<h3><?php echo $course->getNumberWeeks();?> Week Course - £<?php echo $course->getCost();?> per person</h3>
<div class="img-responsive">
<?php $im->outputThumbnail($course->getCourseImage(),400,400,'',false,true);?>
</div>
</div>
<?php
endforeach;
?>
</div>


<?php } else { ?>
<p><?=t('No "%s" entries can be found', $entity->getEntityDisplayName())?>
<?php } ?>

So where I could do with some pointers is retrieving the Dance School, Venue and Teacher details in the above loop?

Any help is greatly appreciated?
Cheers

 
mnakalay replied on at Permalink Reply
mnakalay
Did you check this page of the dochttps://documentation.concrete5.org/developers/express/creating-read... ?

Particularly the chapter "Getting Data From an Express Entry" with its sub-chapter "Getting Associated Entries" ?
jiveboogie replied on at Permalink Reply
Thanks for the pointer @mnakalay. I did read that chapter but it didn't seem to work in my case, and here is why.

After my foreach loop I added this code:
$school = $course->getDanceschool();

Singular makes sense in this case because there is only one school per course, but concrete5 kept throwing a null error when I tried to retrieve the name of the school with $school->getSchoolName().

However I tried a one-to-many relationship and for some reason when I assigned the school to a second course the first association was lost. I then changed this to a many-to-many relationship. This was overkill but it served a purpose.

Now I have changed the code to:
$school = $course->getDanceschools()

The error message changed when I tried $venue = $school->getVenue() to trying to get a method on an array. This then makes sense because of the many-to-many relationship its obvious now that an array of danceschools would be returned for each $course. So now the code reads:

$school = $course->getDanceschools();
$venue = $school[0]->getVenue(); // Even though there is a many-to-many relationship the first index is all i need.

So thanks for the pointer I got it to work in the end.

Cheers
Graham
mnakalay replied on at Permalink Reply
mnakalay
If the information in your first message is accurate, it should be getDanceSchool() with a capital S for school, not getDanceschool()
jiveboogie replied on at Permalink Reply
Yes that would be correct if the handle were dance_school. But for some weird reason I chose to use danceschool as the handle, hence getDanceschool().

Still its all working now, so thanks for your help.

cheers
G