Announcement

Collapse
No announcement yet.

Insert data in text field as json object

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

  • Insert data in text field as json object

    Hello, I have text field in an entity that I want to update with data. Until now I tried to use the ORM to set the field like:
    $entity->set('fieldName', 'Test Account');
    but the problem is that when I set the field value it becomes set for all records although I fetch the entity like:

    $account = $entityManager->getRepository('Account')->get($accountId);
    Is there a way to set the field with a condition based on the record id?
    Or can I make an insert in database?

  • #2
    Hello
    https://github.com/espocrm/documenta...fetch-existing

    Code:
    $account = $entityManager->getEntity('Account', $accountId);
    $account->set('[fieldName]', 'Test Account');
    $entityManager->saveEntity($account);
    Last edited by tanya; 08-01-2017, 07:45 AM.

    Comment


    • #3
      Hello,
      Thanks it worked. The problem was in the fact that I saved in the ->getRepository ??

      Comment


      • #4
        I use this construction.
        Paste your code, please. From your text I see, that you use $account variable, but set the field in $entity variable

        Comment


        • #5
          $entityManager = $this->getEntityManager();
          $odi = $entityManager->getEntity('Obiectiv', $id);
          $fieldValue = $odi->get('val_solicitata_json');
          $in = array();
          if($fieldValue == null) {
          $in = $date;
          } else {
          $tem = json_decode($fieldValue);
          $in = $tem;
          foreach($date as $value) {
          array_push($in, $value);
          }
          }
          $final = json_encode($in);
          $odi->set('val_solicitata_json', $finall);
          $entityManager->saveEntity($odi);

          Comment


          • tanya
            tanya commented
            Editing a comment
            is this code work as you want?

        • #6
          yes, before correction, I had getRepository instead of getEntity ;

          Comment

          Working...
          X