Page 1 of 2

Table delegate class

PostPosted: Fri Feb 24, 2012 2:44 pm
by wisni1rr
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.

Re: Table delegate class

PostPosted: Sat Feb 25, 2012 2:01 am
by Jean
Please look at the grafted fields
http://xataface.com/wiki/Grafted_fields

Re: Table delegate class

PostPosted: Tue Feb 28, 2012 11:59 am
by wisni1rr
Thanks, Jean!

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

Thanks!!

Re: Table delegate class

PostPosted: Tue Feb 28, 2012 12:27 pm
by shannah
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

Re: Table delegate class

PostPosted: Tue Feb 28, 2012 2:21 pm
by wisni1rr
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!!

Re: Table delegate class

PostPosted: Tue Feb 28, 2012 2:25 pm
by shannah
Code: Select all
$children = $record->getRelatedRecordObjects('relationshipname');
foreach ($children as $child){

    echo $child->val('street').', '.$child->val('city').', '. etc....
}

Re: Table delegate class

PostPosted: Tue Feb 28, 2012 3:33 pm
by wisni1rr
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

Re: Table delegate class

PostPosted: Tue Feb 28, 2012 3:52 pm
by shannah
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

Re: Table delegate class

PostPosted: Tue Feb 28, 2012 4:00 pm
by wisni1rr
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

Re: Table delegate class

PostPosted: Tue Feb 28, 2012 4:23 pm
by wisni1rr
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!

Re: Table delegate class

PostPosted: Tue Feb 28, 2012 4:36 pm
by shannah
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

Re: Table delegate class

PostPosted: Tue Feb 28, 2012 4:43 pm
by ADobkin
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').' ');
}

Re: Table delegate class

PostPosted: Tue Feb 28, 2012 4:47 pm
by wisni1rr
SUPER FANTASTIC!

The array worked perfectly!!!

Thanks 10 million times!

Rich

Re: Table delegate class

PostPosted: Tue Mar 06, 2012 12:34 pm
by wisni1rr
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?

Re: Table delegate class

PostPosted: Tue Mar 06, 2012 12:52 pm
by shannah
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');