Table delegate class

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

Table delegate class

Postby wisni1rr » Fri Feb 24, 2012 2:44 pm

Is it possible to refer to a tables child records in a delegate class?

My thought is that I can have a record with approx 5 related child records. On the parent record I want to pull the address fields from my child records so I can map in the parent record.
wisni1rr
 
Posts: 107
Joined: Mon Feb 13, 2012 9:03 pm

Re: Table delegate class

Postby Jean » Sat Feb 25, 2012 2:01 am

Please look at the grafted fields
http://xataface.com/wiki/Grafted_fields
Jean
 
Posts: 259
Joined: Wed Nov 07, 2007 1:30 am
Location: Pau, France

Re: Table delegate class

Postby wisni1rr » Tue Feb 28, 2012 11:59 am

Thanks, Jean!

If its not to much to ask, can you elaborate on what grafted fields are. I am not grasping the concept.

Thanks!!
wisni1rr
 
Posts: 107
Joined: Mon Feb 13, 2012 9:03 pm

Re: Table delegate class

Postby shannah » Tue Feb 28, 2012 12:27 pm

What do you mean by "access" child records?

Using the API you can definitely load related records. Using custom sections you can display these records in your view tab. Related records can obviously listed in a sub-related tab by the fact of the relationship definition. You can edit related records in the parent form using the grid widget. You can already search based on related fields in the find form.

What do you mean by "access"?

(Grafted fields won't be a solution for accessing child records if there is more than one child record per parent.... they are quite effective though when you want to "graft" fields of the parent onto a child table).

-Steve
shannah
 
Posts: 4457
Joined: Wed Dec 31, 1969 5:00 pm

Re: Table delegate class

Postby wisni1rr » Tue Feb 28, 2012 2:21 pm

Thank you, Steve.

In respose to "access"; I am looking to pull the contents of a few fields in a child table as variables. and use them in the parent table's delegate class.

i need variables in a manner like this.
[parent table]
$locationString = $record->val('StreetNo').' '.$record->val('StreetName').' '.$record->val('City').' '.$record->val('State')
[child table]
$childLocationString = $record->val('StreetNo').' '.$record->val('StreetName').' '.$record->val('City').' '.$record->val('State')

The child table will contain about 5-10 records.
I not sure exactly how to handle the array.

The child table has similar field names as the parent table.

So my finished formula will be something like:

Code: Select all
...$locationString...$childLocationString1.$childLocationString2.$childLocationString3...


I hope that helps clarify. I really appreciate all the forum member's help.

Thanks!!
wisni1rr
 
Posts: 107
Joined: Mon Feb 13, 2012 9:03 pm

Re: Table delegate class

Postby shannah » Tue Feb 28, 2012 2:25 pm

Code: Select all
$children = $record->getRelatedRecordObjects('relationshipname');
foreach ($children as $child){

    echo $child->val('street').', '.$child->val('city').', '. etc....
}
shannah
 
Posts: 4457
Joined: Wed Dec 31, 1969 5:00 pm

Re: Table delegate class

Postby wisni1rr » Tue Feb 28, 2012 3:33 pm

Thanks Steve!

I'm really close to figuring it out. This is what I have currently.

Code: Select all
  $children = $record->getRelatedRecordObjects('Comps');
                foreach ($children as $child){

    $childLocationString = $child->val('StreetNo').' '.$child->val('StreetName').' '.$child->val('City').' '.$child->val('State');
}

          return array(
           'content' => "$childLocationString",
           'class' => 'main',
           'label' => 'Property Map via Google',
                'order' => -1
    );
}


This will return only one of my child records. I need it to return the fields of all child records as a continuous string.

Thanks again!

Rich
wisni1rr
 
Posts: 107
Joined: Mon Feb 13, 2012 9:03 pm

Re: Table delegate class

Postby shannah » Tue Feb 28, 2012 3:52 pm

That's because in each iteration of the loop you're overwriting the $childLocationString variable.

Try this:
Code: Select all
$childLocationString = '';
foreach ($children as $child){
    $childLocationString .= $child->val('StreetNo').' '.$child->val('StreetName').' '.$child->val('City').' '.$child->val('State');
}
.. etc...

(Notice I use ".=" instead of "=" when setting the string. This appends instead of replacing).
-Steve
shannah
 
Posts: 4457
Joined: Wed Dec 31, 1969 5:00 pm

Re: Table delegate class

Postby wisni1rr » Tue Feb 28, 2012 4:00 pm

Fantastic Steve!

Looks like this should work for what I'm looking to do.

I'll post an update to confirm it.

Thanks again,

Rich
wisni1rr
 
Posts: 107
Joined: Mon Feb 13, 2012 9:03 pm

Re: Table delegate class

Postby wisni1rr » Tue Feb 28, 2012 4:23 pm

Super duper close now.

Code: Select all
$children = $record->getRelatedRecordObjects('Comps');
                foreach ($children as $child){

    $childLocationString .= urlencode($child->val('StreetNo').' '.$child->val('StreetName').' '.$child->val('City').' '.$child->val('State').' ').'to:';
}


I'm not sure how to modify this to get my desire result.

The tail of the $childLocationString has the following:
Code: Select all
.' ').to:'


How can I modify the iteration so it will not include the tail on the final iteration?

As an example: This is what the first code-block will produce:

Code: Select all
1910+17TH+ST+SOMETOWN+MI+to:916+BONHOMME+COURT+SOMETOWN+MI+to:1026+BANCROFT+SOMETOWN+MI+to:


The last 4 characters need to be omitted somehow in the prior code block.

Thanks for all your help!
Last edited by wisni1rr on Tue Feb 28, 2012 4:38 pm, edited 1 time in total.
wisni1rr
 
Posts: 107
Joined: Mon Feb 13, 2012 9:03 pm

Re: Table delegate class

Postby shannah » Tue Feb 28, 2012 4:36 pm

There are 3 main ways to deal with this.
1. Call substr on your string after the loop is done to remove the end of the string.
2. Use an if statement inside the loop to test if it is the last iteration (not obvious if using a foreach loop).
3. Use an array, then join at the end.

Example of option 3.
Code: Select all
$locationString = array();
foreach ($children as $child){
    $locationString[] = urlencode($child->val('StreetNo').' '.$child->val('StreetName').' '.$child->val('City').' '.$child->val('State').' ');

}

$locationString = implode('to:', $locationString);


This way you are appending your strings to the array. Then the implode() function (http://ca3.php.net/manual/en/function.implode.php) joins all of the parts of the array with 'to:' in between each part.

-Steve
shannah
 
Posts: 4457
Joined: Wed Dec 31, 1969 5:00 pm

Re: Table delegate class

Postby ADobkin » Tue Feb 28, 2012 4:43 pm

Another simple method is to append the 'to:' at the beginning of the next iteration rather at the end of each, like this:

Code: Select all
$children = $record->getRelatedRecordObjects('Comps');
foreach ($children as $child){
    if ($childLocationString) $childLocationString .= .'to:';
    $childLocationString .= urlencode($child->val('StreetNo').' '.$child->val('StreetName').' '.$child->val('City').' '.$child->val('State').' ');
}
ADobkin
 
Posts: 195
Joined: Mon Oct 22, 2007 7:31 pm
Location: Atlanta, GA, USA

Re: Table delegate class

Postby wisni1rr » Tue Feb 28, 2012 4:47 pm

SUPER FANTASTIC!

The array worked perfectly!!!

Thanks 10 million times!

Rich
wisni1rr
 
Posts: 107
Joined: Mon Feb 13, 2012 9:03 pm

Re: Table delegate class

Postby wisni1rr » Tue Mar 06, 2012 12:34 pm

This relates so I decided to post it here.

In a similar manner as above, how can I reference fields in a grandchild (a child's child) record?
wisni1rr
 
Posts: 107
Joined: Mon Feb 13, 2012 9:03 pm

Re: Table delegate class

Postby shannah » Tue Mar 06, 2012 12:52 pm

The method getRelatedRecordsObjects() returns an array of Dataface_RelatedRecord objects, which are similar to Dataface_Record objects but not the same. For instance, they don't have a getRelatedRecordObjects() method. To get the related records of a related record, you must first convert the record to a Dataface_Record object using the Dataface_RelatedRecord::toRecord() method.

e.g.
Code: Select all
$children = $record->getRelatedRecordObjects('children');
foreach ($children as $child){
     $childObj = $child->toRecord();
     $grandchildren = $childObj->getRelatedRecordObjects('children');
     foreach ($grandchildren as $grandchild){
          ///
     }
}


Note, if there is more than one destination table involved in the relationship, you should specify the table name as the first parameter of the toRecord() method.

e.g.
Code: Select all
$child->toRecord('tablename');
shannah
 
Posts: 4457
Joined: Wed Dec 31, 1969 5:00 pm

Next

Return to Xataface Users

Who is online

Users browsing this forum: No registered users and 0 guests

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