get related objects

A place for users and developers of the Xataface to discuss and receive support.

get related objects

Postby joaquinv » Mon Oct 22, 2012 7:30 am

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
joaquinv
 
Posts: 3
Joined: Fri Dec 05, 2008 12:10 pm

Re: get related objects

Postby silma » Tue Oct 23, 2012 12:16 am

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}
silma
 
Posts: 87
Joined: Tue Apr 28, 2009 11:47 pm

Re: get related objects

Postby joaquinv » Sat Nov 03, 2012 6:09 pm

Thank you Thank you Thank you! And thank you again!
joaquinv
 
Posts: 3
Joined: Fri Dec 05, 2008 12:10 pm


Return to Xataface Users

Who is online

Users browsing this forum: No registered users and 24 guests

cron
Powered by Dataface
© 2005-2007 Steve Hannah All rights reserved