Menu

how to display the db_key_field value

2023-04-11
2023-05-17
  • Abdullah Noman

    Abdullah Noman - 2023-04-11

    Hello,

    I want to use the attribute db_key_field that we declare at the beginning of class when objects are displayed in a list, so that a user will be able to see their unique id number. But unfortunately whenever I call the attribute 'id' in reconciliation tag ,it shows the id doesn't exist. Any help will be appreciated.

     
  • Abdullah Noman

    Abdullah Noman - 2023-04-13

    Anybody can help please?

     
  • Vincent @ Combodo

    No, it's not possible to display this id. You have to copy it in a php method AfterInsert in another field which can be displayed.

     
  • Abdullah Noman

    Abdullah Noman - 2023-04-13

    Hi Vincent, Thank you so much for your response. I would appreciate if you can nudge me towards the right direction here. Here is my code:
    <field id="counter" xsi:type="AttributeInteger">
    <values>
    <value><![CDATA[return $this->GetKey();]]></value>
    </values>
    <sql>counter</sql>
    <is_null_allowed>true</is_null_allowed>
    <always_load_in_tables>true</always_load_in_tables>
    </field>

          but this didnt work. Would you please tell me what changes can I make here?
    
     
  • Abdullah Noman

    Abdullah Noman - 2023-04-13

    @cisou I have tried this code which only replaces the value with -1.
    <method id="OnInsert">
    <static>false</static>
    <access>protected</access>
    <type>Overload-DBObject</type>
    <![CDATA[
    protected function OnInsert()
    {
    </method>

          $iNextId = $this->GetKey();
          $this->Set('counter', $iNextId);
          parent::OnInsert();
    
      }
        ]]></code>
        </method>
    

    I assume since its onInsert and the key field was not created yet, its returning -1. but I tried the same code but using the AfterInsert() method which should have worked but when I use the AfterInsert method the value of counter doesnt get updated at all.

     
  • Abdullah Noman

    Abdullah Noman - 2023-04-13
     

    Last edit: Abdullah Noman 2023-04-13
  • Jeffrey Bostoen

    Jeffrey Bostoen - 2023-04-14

    I would indeed also just use AfterInsert(), then perhaps

    $this->Set('counter', $this->GetKey());
    $this->DBUpdate();
    
     
  • Abdullah Noman

    Abdullah Noman - 2023-04-14

    Thank you so much Jeff. It worked. Really appreciate your help.

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.