Jump to:

23379 Posts in 18298 Topics by 2867 members

General Questions

SilverStripe Forums » General Questions » Get current ID in control

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: 1104 Views
  • quanto
    Avatar
    Community Member
    81 Posts

    Get current ID in control Link to this post

    Maybe a stupid question which has been asked lots of times on the forum, but I can't find a solution.

    In my template ProductOverzicht.ss I have the following code:

    <% if Children %>
    <% control GetChildren %>
    <section class="blok">
    <article>

    <% if Top.isNieuw %><div class="nieuw">nieuw</div><% else %>nope $ID<% end_if %>
    <div class="links">
    <% if MyFotos %><% control MyFotos %><% control Plaatje %><% if First %>$setWidth(188)<% end_if %><% end_control %><% end_control %><% else %>
    <img src="themes/agrideals/images/no-image.png" alt="no image"/><% end_if %>
    </div>
    <div class="rechts"><h2>$Title</h2>
    $Content.FirstParagraph
    </div>
    <div class="bekijk"><a href="$Link">Bekijk product</a></div>
    </article>
    </section>
    <% end_control %>
    <% end_if %>

    I want to find out how I can get the current ID of the Child of Top.isNieuw

    The function isNieuw:

    public function isNieuw(){
    $aantalweken = 2;
    $tijd = time() - ((7 * 24 * 60 * 60) * $aantalweken);
    $obj = DataObject::get_one('Product', 'ID='.$this->ID.' AND UNIX_TIMESTAMP(Created) > '.$tijd);

    Debug::show($obj);
    if($obj) return false;
    else return true;

    }

    Currently I get the ID of the parent, not the child...

  • Hattori
    Avatar
    Community Member
    20 Posts

    Re: Get current ID in control Link to this post

    function isNieuw(){
    ...
    ...
    ...
    return $obj; //replace if($obj) return false;else return true; by this
    }

    in template:

    <% control Top.isNieuw %>
    $ID
    <%end_control %>

  • quanto
    Avatar
    Community Member
    81 Posts

    Re: Get current ID in control Link to this post

    Not exactly what i ment.

    I'm trying to get the ID from the subpages wich are called by the control

    Where do i have to put the isNieuw function? Just in Page.php?

    <% control GetChildren %>

    <% control Top.isNieuw %>
    //gives the id from the main page, not the child.. how do I get the child ID?

    <% end_control %>
    <% end_control %>

  • Hattori
    Avatar
    Community Member
    20 Posts

    Re: Get current ID in control Link to this post

    Not sure what you want to do, it's better if have an image shows your site structure with descriptions.

  • Willr
    Avatar
    Forum Moderator
    5490 Posts

    Re: Get current ID in control Link to this post

    how do I get the child ID?

    In SS3.0 you can use $Up to move up the template scope. In 2.* you can't do that, you'll have to open and end multiple controls. Though by the sounds of it isNieuw returns a boolean so you should use if rather than control and this will not alter the template scope so you don't need to change your code.

    <% control GetChildren %>

    <% if Top.isNieuw %>
    $ID
    <% end_if %>
    <% end_control %>
    <% end_control %>

  • quanto
    Avatar
    Community Member
    81 Posts

    Re: Get current ID in control Link to this post

    I recreated the script on some points, but still remaining the same problem.

    Overview of the code:

    ProductOverzicht.php

    class ProductOverzicht_Controller extends Page_Controller {
    ...
    public function GetChildren($Limit = 5){
    return DataObject::get("Product", "ParentID = $this->ID", Null, Null, $Limit);
    }

    public function isNieuw(){
    $aantalweken = 2;
    $tijd = time() - ((7 * 24 * 60 * 60) * $aantalweken);

    if (date('U',strtotime($this->Created)) > $tijd) return true;
    else return false;
    }
    ...
    }

    ProductOverzicht.ss

    ...
    <% if Children %>
    <% control GetChildren %>
    <% if isNieuw %>
    //do something
    <% else %>
    //do something else
    <% end_if %>
    <% end_control %>
    <% end_if %>
    ...


    The above code (isNieuw) doesn't put out anything. When I put in Top.isNieuw it works, but it gets the Created time of the top page in $this->Created, not of the current child in the control. I need the current time of that specific child into my function.

  • Willr
    Avatar
    Forum Moderator
    5490 Posts

    Re: Get current ID in control Link to this post

    The above code (isNieuw) doesn't put out anything. When I put in Top.isNieuw it works, but it gets the Created time of the top page in $this->Created, not of the current child in the control. I need the current time of that specific child into my function.

    So you need to use the Created date of the child? Then why not move the isNiew function to your actual child class (Product). Make sure you have this in your model record (Product) and not your controller.

  • quanto
    Avatar
    Community Member
    81 Posts

    Re: Get current ID in control Link to this post

    Make sure you have this in your model record (Product) and not your controller.

    No way! That easy? Damn I spend days to fix it. Thank you! You saved my day!

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