Jump to:

23491 Posts in 18996 Topics by 2878 members

General Questions

SilverStripe Forums » General Questions » Query Data from a custom Table

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 2 3 4
Go to End
Author Topic: 4198 Views
  • Breastfed
    Avatar
    Community Member
    44 Posts

    Query Data from a custom Table Link to this post

    Hello

    how can i retreive all Entries of a Custom Table?
    I set up a table "LocationSubmission" and i can insert it with a form, but i dont know how to start the ouput on a LocationHolder.

    Anybody with a basic (!) Tutorial?

    Thanks!

  • Breastfed
    Avatar
    Community Member
    44 Posts

    Re: Query Data from a custom Table Link to this post

    Ok i got it running now, but maybe somebody can help me now with the following:

    I have a LocationHolder for all Locations and a Location (Pagetype) for the Detailview.

    How can i link from the Holder with a <a>Tag to the Location and get the Pagedetails there?

  • Sam
    Avatar
    Administrator
    679 Posts

    Re: Query Data from a custom Table Link to this post

    you should juts be able to use this:

    DataObject::get("LocationSubmission")

    if you make a method on your controller like thisÚ

    function Submissions() {
    return DataObject::get("LocationSubmission");
    }

    Then you can have a <% control Submissions %> entry in your template:

    <ul>
    <% control Submissions %>
    <li><a href="$Link">$Title</a></li>
    <% end_control %>
    </ul>

    $Link and $Title will be taken from each LocationSubmission object and inserted into the repeating <% control %> ... <% end_control %> block in the template.

  • Breastfed
    Avatar
    Community Member
    44 Posts

    Re: Query Data from a custom Table Link to this post

    Hi Sam,

    thanks for answering.
    Everything is working except the Link to a detailed Page.

    When i am inserting the $Link it will give me nothing back.

    As i know it from other PHP Applications i should be able to link to the detailed Page and add an ID or Name to $_GET it from the Database, maybe you can explain how to get the Link going and how to receive Data on the detailed Page.

    Would be very thankful!

  • Breastfed
    Avatar
    Community Member
    44 Posts

    Re: Query Data from a custom Table Link to this post

    Somebody who might help me here?

    On the HolderPage i got an Overview of ALL Locations inerted in the DataTable.

    By control in can create the Loop.

    But now i want to go to the detailed Page with more Infos about the Location.
    So i tought to create the following Anchor tage

       <a href="kicker-location/$ID">mehr</a>

    The Page "kicker-location" is the Detailpage.

    On the Holder i have this DataObejct:

          function LocationShow ()
          {
             $sort = 'Created DESC';
             
             $data = DataObject::get('LocationSubmission','',$sort,'','');
             return $data;
          }
       

    How can i retrieve Data on the DetailPage now by editing the DataObject?

    Would be very happy about Help.

    Thanks!

  • Breastfed
    Avatar
    Community Member
    44 Posts

    Re: Query Data from a custom Table Link to this post

    Sorry - but i am stuck now - would be so glad for Information.

  • Aram
    Avatar
    Community Member
    598 Posts

    Re: Query Data from a custom Table Link to this post

    Hi

    This works just as sam described using the $Link variable. You can't use IDs as links or anything else, you need to use the variable that is provided. You should only need one Location holder and make all the locations children of this page.

    In your LocationHolder controller you need a function like this:

    function GetLocations()
    {
    return DataObject::get('Location', 'ParentID = {$this->ID}', 'ID DESC');
    }

    This will return all the locations which are children of this page and order them by their ID (essentailly the same as ordering by created as IDs are auto incremented)

    Then in your template:

    <% GetLocations %>
    <a href="$Link" >$Title</a>
    <% end_control %>

    If there is nothing in the href then check that you have $Link with a capital L.

  • Breastfed
    Avatar
    Community Member
    44 Posts

    Re: Query Data from a custom Table Link to this post

    Hello Aram,

    this is My LocationHolder.php

    <?php
       class LocationHolder extends Page {
          
          static $allowed_children = array(
             'Location'
          );
          
          function LocationShow ()
          {
             $sort = 'Created DESC';
             
             $data = DataObject::get('LocationSubmission','',$sort,'','');
             return $data;
          }
          
          function GetLocations()
          {
             return DataObject::get('Location', 'ParentID = {$this->ID}', 'ID DESC');
          }
       
       }
       
       class LocationHolder_Controller extends Page_Controller {
       

       
       }
       

    ?>

    and this is the LocationHolder.ss

    <div id="col1">
    <% include SideBar %>
    </div>
    <div id="col2">
    <h2>Locations</h2>

    <% control LocationShow %>
    <!-- Location Row -->
    <% if Odd %>
    <div style="margin-bottom: 10px; padding-bottom: 5px; border-bottom: 1px dotted #000;">
    <% else %>
    <div style="margin-bottom: 10px; padding-bottom: 5px; border-bottom: 1px dotted #000; background-color: #eaeaea;">
    <% end_if %>
       $ID <br />
       $Name
       $Description
       $Adress
       $PLZ
       <a href="$Link">mehr</a>

    </div>
    <!-- end: Location Row -->
    <% end_control %>

    </div>

    <div id="col3">
    </div>

    But the output of $Link is just empty.

    Sorry - could you help me?

    4198 Views
Page: 1 2 3 4
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.