klox7 VIP
Total posts: 914
02 Jan 2015 13:47

Hi,

I have 2 types. In 1st type I have some fields which I would like to display in list and full view of 2nd type. Those 2 have relation, 1st is parent and 2nd is child.

How can I determine parent record or how can I get field values from parent record in child record?

Regards

Hidden text

Last Modified: 03 Feb 2015

Tags Developer

Comments have been disabled for this article and works only in read only mode.

pepperstreet VIP
Total posts: 3,837
02 Jan 2015 23:53

Programatically...?

Or by configuration:

Did you see and configure both relation field parameters? In Child- and Parent field there are according parameters. Look for parameter set "Show Parent" and "Show Children". Both allow to set a special template for full and list view. In template parameters include/exclude your fields.


klox7 VIP
Total posts: 914
03 Jan 2015 21:13

pepperstreet Programatically...?>

Yes, I'm already using those templates in field for something else. This I need for "record info" which will include standard info, subscriptions info, parent info etc.


pepperstreet VIP
Total posts: 3,837
05 Jan 2015 03:11

I remember that someone else has asked a similar question... sorry, can't recall the username, nor the exact topic. Maybe "jimie", or "Xtreame" ...?!

Found only the following topic. Hence Sergey's first comment:


Sergey
Total posts: 13,748
06 Jan 2015 06:26

First get your field value

$ids = $item->fields_by_id[12]->values;

Check if it is array or single value. If it is array you can get access to ID by $ids[0]. So now use records API to get list of articles and pass $ids as last parameter. At the end you have list of articles with field values.


klox7 VIP
Total posts: 914
06 Jan 2015 08:03

Sergey First get your field value

But how will I get field value of related record? So if I'm using parent-child relation how will I get fields value from parent in child record list?


Sergey
Total posts: 13,748
06 Jan 2015 15:10

you use records API.

$ids = $item->fields_by_id[12]->values;
$items = $api=>records(........, $ids);

$item = $items['list'][0];

$item->field_by_id[10]->result.

Or if you only need a field falue use RenderField API.

$record_id = $item->fields_by_id[12]->values[0];
echo CobaltApi::renderField($record_id, 10, 'full')

Where 10 is ID of the related article field.


klox7 VIP
Total posts: 914
06 Jan 2015 19:44

Thank you. I thought I have to do some custom condition to get those fields.


klox7 VIP
Total posts: 914
09 Jan 2015 14:10

How can I exclude parameters here (for ex. I don't need $limit):

$data = $api->records(
    $section_id, 
    $view_what, 
    $orderby, 
    $type_ids,
    $user_id,
    $category_id, 
    $limit, 
    $template,
    $client,
    $client_id,
    $lang,
    $ids
);

And also how can I check if field of related record is set?


Sergey
Total posts: 13,748
12 Jan 2015 07:40

Us limit to 5 for example.

klox7 And also how can I check if field of related record is set?

You do not need to set any relations because you already have IDs of the records to get passed with $ids parameter.


klox7 VIP
Total posts: 914
12 Jan 2015 07:57

So in $data = $api->records I have to set up every parameter?


Sergey
Total posts: 13,748
13 Jan 2015 12:23

Yes. view_what should be all and others that you do not know use null.


Sergey
Total posts: 13,748
22 Jan 2015 03:15

Where you add this code? In Joomla template or Cobalt template or module? Which one in particular. Anyway try to make sure this file is included before you call it.

require_once JPATH_ROOT . '/components/com_cobalt/api.php';

klox7 VIP
Total posts: 914
22 Jan 2015 07:39

Yes I tried based on documentation. In full record template. Same thing happens as above.


Sergey
Total posts: 13,748
26 Jan 2015 03:51

Looks like you pass template name in to records() function with it cannot find on the disk. Just use '' as template for test.


klox7 VIP
Total posts: 914
31 Jan 2015 20:43

OK, this is for render article list and not field value of related record.

Sergey $ids = $item->fields_by_id[12]->values; $items = $api=>records(........, $ids);

$item = $items['list'][0];

$item->field_by_id[10]->result.


Sergey $record_id = $item->fields_by_id[12]->values[0]; echo CobaltApi::renderField($record_id, 10, 'full')

Is this the only way for displaying related record field value. Or is it possible that I get label and result?


And how to remove this notice when using code bellow?

Notice: Trying to get property of non-object in components/com_cobalt/api.php on line 175

<?php
$related_record_id = $item->fields_by_id[232]->value[0];
$field=CobaltApi::renderField($related_record_id, 315, 'full');
if(!empty($field)):
    echo '<b>Full membership - Sale ID:</b>'. $field .'<br />';
endif;
?>

Sergey
Total posts: 13,748
03 Feb 2015 11:14

Into renderField you have to put record object.

<?php
$related_record_id = $item->fields_by_id[232]->value[0];
if($related_record_id) {
    $record = ItemsStore::getRecord($related_record_id);
    $field=CobaltApi::renderField($record, 315, 'full');
    if(!empty($field)) {
        echo '<b>Full membership - Sale ID:</b>'. $field .'<br />';
    }
}
?>

But I make changes in API and you will have to be able to do this


$field=CobaltApi::renderField($item->fields_by_id[232]->value[0], 315, 'full'); if(!empty($field)) { echo '<b>Full membership - Sale ID:</b>'. $field .'<br />'; }

You have an errod because $related_record_id is not an object.

This is not only way. You can also use fields array.

<?php
$related_record_id = $item->fields_by_id[232]->value[0];
if($related_record_id) {
    $record = ItemsStore::getRecord($related_record_id);
    var_dump($record->fields[315]);
}
?>

If ->fields is a string convert it to to array from JSON first.

$record->fields = json_decode($record->fields, TRUE);
Powered by Cobalt