Fetched Row Not Working on Relate Field Logic Hook

Hi Everyone,

Is it possible to use the fetched_row function on relate field (not relationship)?  

Currently we have the code below written in a before save logic hook, but the current account field is not pulling back a value using fetched_row.

$currentAccount = $bean->fetched_row['account_user_c'];
$GLOBALS['log']->fatal('Current Account: ' . $currentAccount);
$newAccount = $bean->account_user_c;
$GLOBALS['log']->fatal('New Account:: ' . $newAccount);

Any suggestions? Thanks!

Parents
  • Dan,

    Although I don't recall using it in the fetched_row array I believe the field should be available - I see no reason why not.

    However, it doesn't look as if you have the correct field name and usage in your code. When you create a Relate type field in Studio it will actually create a pair of fields, a pseudo one to hold the text of (generally) the "name" field of the related record and a real database one to hold the id value. Studio will normally auto-create the id one using the name you choose. The plain "name" field may not hold a bean value though and I don't think you can create a separate instance using it, you need to collect a bean using the id field.

    In your case, it looks to me like you might have created a field in Studio and given the field a name of "account_user" and Studio has auto-added the "_c" suffix. If that is the case then there will be a second field in both the fields_meta_data database table and in the Vardefs called "account_user_id_c" and you then need to use that field to grab the related bean using a BeanFactory call.

    So, second guessing your field names, I'd say you need to put this:

    $currentAccount = BeanFactory::retrieveBean('Accounts', $bean->fetched_row['account_user_id_c']);

    (assuming here of course that account_user_c holds a related Account record - if it is a related User then you need to adjust the BeanFactory call accordingly)

    Hopefully, that should pull in the bean record of the fetched row value for your related Account.

    [EDIT] In your original code I would say that your line: "$newAccount = $bean->account_user_c;" is not actually pulling back the related Account record but is simply storing the name of the related Account. It is likely that this field is not filled in on the fetched_row array either as the relate will likely be made as part of the Save event. So you might be seeing that the $currentAccount and $newAccount variables hold the same value during the before_save hook or that the $currentAccount is empty as it has not been filled yet. However, the $bean->fetched_row['account_user_id_c'] should hold the previous value for that field when changed (I hope).

    Thanks,

    JH.

Reply
  • Dan,

    Although I don't recall using it in the fetched_row array I believe the field should be available - I see no reason why not.

    However, it doesn't look as if you have the correct field name and usage in your code. When you create a Relate type field in Studio it will actually create a pair of fields, a pseudo one to hold the text of (generally) the "name" field of the related record and a real database one to hold the id value. Studio will normally auto-create the id one using the name you choose. The plain "name" field may not hold a bean value though and I don't think you can create a separate instance using it, you need to collect a bean using the id field.

    In your case, it looks to me like you might have created a field in Studio and given the field a name of "account_user" and Studio has auto-added the "_c" suffix. If that is the case then there will be a second field in both the fields_meta_data database table and in the Vardefs called "account_user_id_c" and you then need to use that field to grab the related bean using a BeanFactory call.

    So, second guessing your field names, I'd say you need to put this:

    $currentAccount = BeanFactory::retrieveBean('Accounts', $bean->fetched_row['account_user_id_c']);

    (assuming here of course that account_user_c holds a related Account record - if it is a related User then you need to adjust the BeanFactory call accordingly)

    Hopefully, that should pull in the bean record of the fetched row value for your related Account.

    [EDIT] In your original code I would say that your line: "$newAccount = $bean->account_user_c;" is not actually pulling back the related Account record but is simply storing the name of the related Account. It is likely that this field is not filled in on the fetched_row array either as the relate will likely be made as part of the Save event. So you might be seeing that the $currentAccount and $newAccount variables hold the same value during the before_save hook or that the $currentAccount is empty as it has not been filled yet. However, the $bean->fetched_row['account_user_id_c'] should hold the previous value for that field when changed (I hope).

    Thanks,

    JH.

Children