Jump to:

23490 Posts in 18996 Topics by 2878 members

General Questions

SilverStripe Forums » General Questions » multiple instance of same class

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

Page: 1
Go to End
Author Topic: 739 Views
  • isolated
    Avatar
    Community Member
    28 Posts

    multiple instance of same class Link to this post

    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?

  • Sam
    Avatar
    Administrator
    679 Posts

    Re: multiple instance of same class Link to this post

    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 %>

  • isolated
    Avatar
    Community Member
    28 Posts

    Re: multiple instance of same class Link to this post

    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?

  • martimiz
    Avatar
    Forum Moderator
    1086 Posts

    Re: multiple instance of same class Link to this post

    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...

  • isolated
    Avatar
    Community Member
    28 Posts

    Re: multiple instance of same class Link to this post

    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...

       

  • martimiz
    Avatar
    Forum Moderator
    1086 Posts

    Re: multiple instance of same class Link to this post

    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...

  • isolated
    Avatar
    Community Member
    28 Posts

    Re: multiple instance of same class Link to this post

    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...

    739 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.