Page 1 of 1

get related objects

PostPosted: Mon Oct 22, 2012 7:30 am
by joaquinv
Hello!
I have two tables with a one to many relationship. I am trying to get a dashboard page going without permissions so I can list the contents of the main table. I would like to add to the listing a value from the related table. I am using a html template and a php page to store the array of the records of the main table to display on the dashboard page. How can this be done?

Thanks!
Joaquin

Re: get related objects

PostPosted: Tue Oct 23, 2012 12:16 am
by silma
You could use the function __sql__() in your main table delagate class to replace the current sql request by the one you want.
However, this will transform the main table for all the application, which is not often the best way to do it.

You could also make an action (http://xataface.com/documentation/tutorial/getting_started/dataface_actions).

in you case your actions/myaction.php should look like :
Code: Select all
<?php
class actions_myaction {

   function handle(&$params){
                 
       $res = mysql_query("select table1.*, table2.field FROM table1 LEFT JOIN table2  ON (table1.fieldID=table2.relatedfieldID)")or die("Error: ".mysql_error());
     
       while ($row = mysql_fetch_assoc($res)) {
         $items[] = $row;
      }
   df_display(array('tableau'=>$items), 'mytemplate.html');

}
   
?>   



and the templates/mytemplate.html
Code: Select all
{use_macro file="Dataface_Main_Template.html"}

   {fill_slot name="main_section"}

      <table id="relatedList" class="listing" >
         <thead>
            <tr>
                <th>Field1title</th>
                <th>Field2title</th>
                <th>Field3title</th>
                               </tr>            
         </thead>
         
         <tbody id="relatedList-body">
            {foreach from=$tableau item=record}

            <tr class="{cycle values='even,odd'}" >
                <td> {$record.Field1}</td>
               <td> {$record.Field2}</td>
               <td> {$record.Field3}</td>
            </tr>
            {/foreach}
         </tbody>
      </table>

   {/fill_slot}

{/use_macro}

Re: get related objects

PostPosted: Sat Nov 03, 2012 6:09 pm
by joaquinv
Thank you Thank you Thank you! And thank you again!