Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

multiple instance of same class


Reply


7 Posts   770 Views

Avatar
isolated

Community Member, 28 Posts

14 October 2012 at 12:28am

Consider example http://doc.silverstripe.org/framework/en/tutorials/2-extending-a-basic-site

By doing this, we can have staff section with multiple users. Lets say i want to have multiple different staff section.
For example staff1 (for miners)->with miners details, staff2 (for some other department)->with this department employees.
Users can be in one section only.

How such model could be implemented?

Avatar
Sam

Administrator, 685 Posts

15 October 2012 at 2:01pm

I would have 3 page types:

* StaffHolderPage
* StaffCategoryPage
* StaffPage

StaffHolderPage would be the parent. It's children would be StaffCategoryPage, and their children would be StaffPage.

On any page, you can use <% control Children %> to loop through the children. So on the StaffHolderPage this would loop through the categories, and on the StaffCategoryPage this would loop through the staff.

You can also put one <% control Children %> block inside another, to list grandchildren on the StaffHolderPage. For example:

<% control Children %>
<h2>Category: $Title</h2>
<ul>
<% control Children %>
<li>Staff member: $Title</li>
<% end_control %>
</ul>
<% end_control %>

Avatar
isolated

Community Member, 28 Posts

15 October 2012 at 10:05pm

Hi,

maybe my questions was not clear.
It is not related with representation- i want to have "multiple different staff section"
staff1 - staffholder
staff2 - staffholder
However with current implementation (as in example), i could create only one staffholder type page (i could create the staff2, however it was not inaccessible).

So the question - if i want to have miners staff and it staff (etc), do i need 2 different page types? staffholder_mine and staffholder_it?

Avatar
martimiz

Forum Moderator, 1098 Posts

16 October 2012 at 5:22am

Hi, maybe I don't understand you correctly, but just in case:

You don't need to create differrent pagetypes for different staff sections. You should be able to create a couple of separate pages in the CMS, all of the 'StaffHolder' type (one for miners, one for ...) where they would all have their own children. That should just work...

Avatar
isolated

Community Member, 28 Posts

16 October 2012 at 9:16am

well, now it seems that it doesn't, as when i create a new page type staffholder -it has the same childs...

here is my code:
product group (as staffholder)
   public static $db = array(
      "NumberOfProductsPerPage" => "Int",
      "LevelOfProductsToShow" => "Int",
      "DefaultSortOrder" => "Varchar(50)",
      "DefaultFilter" => "Varchar(50)",
      "DisplayStyle" => "Varchar(50)"
   );

   
   public static $has_one = array(
      'Image' => 'Product_Image'
   );
public static $belongs_many_many = array(
      'Products' => 'Product'
   );
public static $default_child = 'Product';

and product:
public static $db = array(
      'Price' => 'Currency',
      'Weight' => 'Decimal(9,4)',
      'Model' => 'Varchar(30)', others...

   public static $has_one = array(
      'Image' => 'Product_Image'
   );
   public static $default_parent = 'ProductGroup';

Maybe i miss something in the code, what prevents from having few same product groups...

   

Avatar
martimiz

Forum Moderator, 1098 Posts

18 October 2012 at 6:55am

As far as I can see this is not entirely a replication of the staff and staffholder example, as for instance I see an orfaned $belongs_many_many relation that is not part of the staffpage example. So to really know what might be the problem you'd have to show us your complete code, including templates.

In the staff/staffholder example there is no relation as such ($many_many or $belongs_many_many) between the staffpage and the staffholderpage, just that staffpages are created as childpages to the staffholder. Or in your situation: it should be one or more ProductGroup pages, each with a load of children = Product pages of their own. Then the ProductGroup template will loop through all these childpages and show the details.

Or like in the staffpageholder example:

<% loop Children %>
        <article>
            <h2><a href="$Link" title="Read more on &quot;{$Title}&quot;">$Title</a></h2>
            $Photo.SetWidth(150)
            <p>$Content.FirstParagraph</p>
            <a href="$Link" title="Read more on &quot;{$Title}&quot;">Read more &gt;&gt;</a>
        </article>
    <% end_loop %>

This way every ProductGroup page can only display details of its own children...

Avatar
isolated

Community Member, 28 Posts

18 October 2012 at 7:19am

mhm will check, but then i don't get really the idea of belongs... and has...
For example the same with image - has image, and has many images...