1. NOTICE: If you are using Fabrik and update to Joomla 3.10, you will need to update to Fabrik 3.10. And, if you are using Fabrik, do not upgrade to Joomla 4, we do not have a supported version ready for release. More information on a release date coming soon. Also, please note that Fabrik 3.10 will not install on any Joomla sites less than 3.8.
    Dismiss Notice

Use data from query in field

Discussion in 'Community' started by chris92, Jul 23, 2021.

  1. chris92

    chris92 New Member

    Level: Community
    Hi,

    Before load form I use a php to make a query...

    ...
    $formateur = $myDb->loadObject();
    return $formateur;

    and I just want to use the value, for example, username in an element like

    return $formateur->username;

    I try also:

    $f = 'user_pro___username';
    return $f;

    Thanks
    Chris
     
    Last edited: Jul 23, 2021
  2. juuser

    juuser Well-Known Member

    Level: Community
    So what is the actual problem? I assume you have this in calc element or ...?

    And how does your full code look like?
     
  3. chris92

    chris92 New Member

    Level: Community
    Thanks for your reply juuser,

    in the form
    $myDb = JFactory::getDbo();
    $user = JFactory::getUser();
    $id= $user->id;
    $myQuery = $myDb->getQuery(true)
    ->select('*')
    ->from($myDb->quoteName('user_pro'))
    ->where($myDb->quoteName('user_id') . ' = ' . $id);
    $myDb->setQuery($myQuery);
    $formateur = $myDb->loadObject();

    return $formateur;

    in element:

    return $formateur->username;

    username is a column in the table user_pro (no prefix)
     
  4. lousyfool

    lousyfool Active Member

    Level: Community
    I'm assuming
    - the field user_id in table user_pro is identical with the field id in the Joomla #__users table,
    - but for some reason the "username" is not,
    - you cannot or do not want to join your "main" list (which your form relates to) with a list based on the user_pro table,
    - and you only need to have the the logged-in user's username from user_pro.

    Then you don't need any "form PHP", but you should be good with simply using this in your (calc) element:
    Code (Text):

    $myDb = JFactory::getDbo();
    $myQuery = $myDb->getQuery(true)
        ->select($myDb->quoteName('username'))
        ->from($myDb->quoteName('user_pro'))
        ->where($myDb->quoteName('user_id') . ' = \'{$my->id}\'');
    $myDb->setQuery($myQuery);
    return $myDb->loadResult();
     
    http://fabrikar.com/forums/index.php?wiki/placeholders/
     
    Last edited: Jul 23, 2021
  5. chris92

    chris92 New Member

    Level: Community
    Yes I understant for 1 field but I would want to make only one query before the form load.
    And use the object with all the columns in each field.
    do you understand, lousyfool?
    thanks a lot for your help.
     
    Last edited: Jul 23, 2021
  6. juuser

    juuser Well-Known Member

    Level: Community
    Actually I'm not 100% sure I get your setup and actual goal, but like @lousyfool said, you should just have this code in calc element and not in form plugins. Didn't make any syntax test, but your code seems fine with a quick look. If you need only username, then lousyfools snippet above seems even more optimal.
     
  7. chris92

    chris92 New Member

    Level: Community
    I have several fields to populate from the query.

    I'm affraid by that but in pure php. You can use only one query to load all data from the db. And you can use it in a list or a form by a simple call like $obj->field1, $obj->field2...etc...

    In your sample, I have to make one query by field. It is not very optimized.

    Do you understand?

    best regards
     
  8. juuser

    juuser Well-Known Member

    Level: Community
    If you set "Only calc on save" to "Yes" in calc element settings, the query is only run once when you save a particular record. So no performance issues there. Please note that if you had previously "Only calc on save" set to "No" (that means calculating on the fly), you should open and resave each record to get the field value written to database. If you have a lot of records, it's obviously more efficient to make the "one time update" of the fields in PhpMyAdmin or similar.
     
  9. chris92

    chris92 New Member

    Level: Community
    We deviate completely from the subject. Please read my messages. It's easier than that. And sorry for my english
     
  10. juuser

    juuser Well-Known Member

    Level: Community
    Unsatisfying answers usually start from a poorly modified questions or description of the current situation -> goal. I'm lost here about what you really want to achieve. "Have one optimal query somewhere" is not a clear description (at least not for me).
     
  11. chris92

    chris92 New Member

    Level: Community
    Sorry I know you do the best for me and a very big thank for that. I'm not very clear in my ask...
    Best regards
    Chris
     
  12. troester

    troester Well-Known Member Staff Member

    Level: Community
    This is not the way Fabrik is working.
    Usually you have a Fabrik list linked to a database table, each element is linked to one of the columns and Fabrik is doing the query.
    So I think we don't understand what you want/need to do at all.

    Surely you can bypass/extend Fabrik's standards with php plugins, custom templates, calc elements etc but first you should know how the Fabrik basics are working.
     
  13. chris92

    chris92 New Member

    Level: Community
    Ok troester,
    Bye at all
     

Share This Page