Brian Barnes
Brian Barnes

Reputation: 1027

Themeing a one to many relationship

I am developing a one to many relationship in Silverstripe and trying to theme it but I can't get the many part on the page. has_many on the object is defined as:

public static $has_many = array(
    'PortsPages' => 'PortsPage'
);

and when I try to loop through it using

<% loop $PortsPages %>
    <li>$Title</li>
<% end_loop %>

only one record comes back (with $Title being nothing - $Name comes back with PortsPage - not the title of the object), despite there being 2 in the database. I believe I'm using version 3.1 and I have gone through http://doc.silverstripe.org/framework/en/tutorials/5-dataobject-relationship-management, which hasn't helped me (and is yet to be updated for the latest version).

Edit:

The $has_one is defined as follows:

private static $has_one = array(
    'CityPage' => 'CityPage'
);

When I look at the PortsPage table, it appears that the links are in there (there is a CityPageID column that is populated and looks correct). When I look at the PortsPage_Live, The CityPageID column isn't populated.

Upvotes: 1

Views: 99

Answers (2)

Brian Barnes
Brian Barnes

Reputation: 1027

While Cam is the correct programming solution, it turns out my error was in that they weren't linked correctly within the admin side.

While I had selected the Ports from within the city, I was still required to go into each individual port and save and publish them (although I thought I had already published them before linking them).

What tipped me off is that I looked at the CityPageID column in the PortsPage page table was correct, but when I looked at the PortsPage_Live table, the CityPageID column was still 0.

Upvotes: 1

Cam
Cam

Reputation: 174

If you are using version 3.1 of SilverStripe you will need to make your static variable "private" not "public".

schellmax is correct in pointing out the missing "s".

$Title will only return something if your DataObject "PortsPage" has that as one of it's object properties.

<% loop $PortsPages %>
    <li>$Title</li>
<% end_loop %>

Also if your PortsPages in the SiteTree are direct children of the root level page you can access them in the template engine using <% loop Children %>.

Upvotes: 4

Related Questions