Announcement

Collapse
No announcement yet.

Metadada change only one value

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Metadada change only one value

    Hi,
    i have a entity with a auto-increment and prefix. ()
    where use this kind of field, like invoiceNumber.
    prefix = 2 letter of year
    number = 0001 (with padding)
    wonderfull.

    But what i don't understand, prefix is in entityDefs.json and the next number is on database (entity = NextNumber).
    My question is why Prefix is not in Database ?
    So second question, is possible to change just the prefix ? with metadata, fileManager ? or we need to read all content of json ?

    I this sample, it's a job who run annually on 1/1 .. so prefix will be 2 letter of year. so next prefix on 1/1 is 24

    PHP Code:

    "number": {
                
    "type""number",
                
    "len"36,
                
    "notNull"false,
                
    "unique"false,
                
    "nextNumber"1,
                
    "padLength"4,
                
    "prefix""23",
                
    "inlineEditDisabled"true,
                
    "isCustom"true
            
    }​ 

    PHP Code:

            $nextNumber 
    $this->em->getRDBRepository('NextNumber')
                ->
    where([
                    
    'entityType' => 'Facture',
                    
    'fieldName' => 'number'
                
    ])
                ->
    findOne();
            
    $nextNumber->set([
                
    'value' => 1,
                ]);
             
    $this->em->saveEntity($nextNumber);
            
            
    $fieldNumber $this->metadata->getObjects(['entityDefs''Facture''fields''number'], null);
            
    $fieldNumber->prefix '34';

            
    $this->log->errorjson_encode($fieldNumber ) );
         
    ​ 

  • #2
    No body have response ? Vadym victor
    I dont like to ask personnaly but i think this "question" is respectable

    Comment


    • #3
      Hi item ,

      I don't know if you can edit metadata like that.

      My understanding is that Metadata.php is a file created by the rebuild process to consolidate all metadata json files in the application and the information there is stored as an associative array not as object or array of objects.

      I would try this:
      PHP Code:

          $fieldNumber 
      $this->metadata->get(['entityDefs','Facture','fields','number']);
          
      $fieldNumber['prefix'] = '34';
          
      $this->metadata->set(['entityDefs','Facture','fields','number'],$fieldNumber); 
      Last edited by telecastg; 06-08-2023, 03:13 PM.

      Comment


      • #4
        Hi telecastg
        i will try tomorow but i think don't work.
        PHP Code:
         $this->metadata->set(['entityDefs','Facture','fields'],'number',$fieldNumber);
        // so now i need something like this:
        $this->saveEntity($metadata); 
        i think, my question is more "why nextNumber value" is in "database" .. and why not "prefix".

        i think, entityDefs must be "protected" a maximum.

        And in the real world... "prefix" is always related to year or .. so in a basic cron job, we can change this without touch entityDefs.

        it's just my idea
        Attached Files
        Last edited by item; 06-05-2023, 08:41 PM.

        Comment


        • #5
          Hi @item,

          The statement $this->metadata->set() is used by the Entity Manager to persist changes made to metadata in the Admin GUI, so I don't think that you will need the saveEntity statement, but please let me know if it doesn't work

          Another option is to retrieve the actual file content, edit and save it. This is a more complicated process, but if $this->metadata->set() does not work I will be happy to share an example of it.

          i think, my question is more "why nextNumber value" is in "database" .. and why not "prefix".

          i think, entityDefs must be "protected" a maximum.

          And in the real world... "prefix" is always related to year or .. so in a basic cron job, we can change this without touch entityDefs.
          I think those questions will be better answered by someone from the development team

          Comment


          • item
            item commented
            Editing a comment
            Hi telecastg,
            yes question is more for team.. but no response and Yuri is busy so no disturb. It's so then.

            Yes share a sample
            i don't understand some function of metadata.
            he read from cache ?
            so how he know is a custom field ? because there are 2 function
            $this->metadata->getCustom
            $this->metadata->saveCustom

            the $this->metadata->set(). if for out-of-box Metadata i think

            Regards

        • #6
          No problem,

          First, this is what I understand about metadata, if my information is not correct I will be happy to be corrected

          The application includes many metadata json files, which contain all the information that the system needs to build the Espo front-end, database structure and settings. Think of it as a "blueprint" / "recipe" for a complex project.

          Most of the metadata files come "off the shelf" with the Espo download, but Users can edit or create additional files, manually, or by using the Admin GUI, to modify the system.

          Upon rebuilding, the system reads and consolidates all metadata json files to build a single php script metadata.php which is stored in cache and serves as data source for the application at runtime. application/data/cache/application/metadata.php

          This very clever approach makes it possible to perform a lot of operations by the front-end while minimizing calls to the server which results in Espo being lighting fast and super flexible as the application can be reconfigured simply by modifying metadata settings.

          The rebuild process essentially orders all metadata instructions and resolves conflicts, giving more weight to competing scripts (for example different versions of Contact.json entityDefs) in accordance to their "weight" determined by the namespace where the file is located.

          Customizations done through the Admin GUI always use the "Custom" namespace which make them take priority over any conflicting json file, that is why a user is able to "modify" the application through the GUI and "revert" those changes if necessary.

          In the case of modules, the module definition includes an "order" value, which determines the relative weight of each module (the higher the value the higher the weight).

          For detailed information about the rebuild process and the metadata methods, please see these files:

          https://github.com/espocrm/espocrm/b...ataManager.php

          EspoCRM – Open Source CRM Application. Contribute to espocrm/espocrm development by creating an account on GitHub.



          Next, for an example of fetching a metadata file from the server, modifying it and storing it back:
          PHP Code:
          use Espo\Core\Utils\File\Manager as FileManager;
          use 
          Espo\Core\Utils\Json;

          class 
          MyMetadataEditClass
          {
              protected 
          $fileManager;
              
              public function 
          __construct(FileManager $fileManager)
              {
                  
          $this->fileManager $fileManager;
              }

              public function 
          editMetadata() : void
              
          {
                  
          $filePath 'custom/Espo/Custom/Resources/metadata/entityDefs/Facture.json';
          ​        $metadataArray = [];

                  if(
          file_exists($filePath) {

                      
          // retrieve the existing file data
                      
          $rawFileData $this->fileManager->getContents($originalFilePath);

                      
          // remove unwanted 'line break' characters
                      
          $fileData str_replace('\n'''$rawFileData);

                      
          // remove unwanted white space
                      
          $jsonData Json::encode(Json::decode($fileData));

                      
          // convert to associative array
                      
          $metadataArray Json::decode($jsonData,true);

                      
          // change the value of option 'prefix' in the field 'number'
                      
          $metadataArray['fields']['number']['prefix'] = '34';

                      
          // prepare the associative array to be stored as metadata structure
                      
          $entityDefsFileData Json::encode($metadataArrayJSON_PRETTY_PRINT JSON_UNESCAPED_UNICODE JSON_UNESCAPED_SLASHES);

                      
          // create new entityDefs file
                      
          $this->fileManager->putContents($filePath$entityDefsFileData);

                  }
              }

          Last edited by telecastg; 06-08-2023, 03:17 PM.

          Comment


        • #7
          PHP Code:
          $metadata->set('entityDefs'$entityType, [
              
          'fields' => [
                   
          'myField' => [
                        
          'myOnlyParameter' => $value,
                   ]
              ]
          ]);

          $metadata->save(); 

          This will merge the current metadata with passed metadata and store it to the custom. There a quite a few usage examples in the codebase.

          Comment

          Working...
          X