How take field value from relayted entity OneToOne

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Lanket
    Member
    • Jul 2019
    • 51

    How take field value from relayted entity OneToOne

    Hello. Help please.

    I need take field value from relayted OneToOne entity.

    I try this :
    -------
    $var = $entity->get('originalLead.vkuserid');
    -------
    But this not work

    I have Lead converted to Opportunity, and in custom hook i want take field vale from Lead.
    Construction "originalLead.vkuserid" i take from formula and i phink what that mast work, but $var if empty. But in this time field value from Lead "vkuserid" is not empty.

    Help please.


    full code custom hook

    <?
    namespace Espo\Custom\Hooks\Opportunity;

    use Espo\ORM\Entity;

    class UnsubscribeSenler extends \Espo\Core\Hooks\Base
    {
    public function beforeSave(Entity $entity, array $options = array())
    {
    // originalLead
    if ($entity->get('stage') == 'Prepay') {
    //if (!is_null($entity->get('Lead.vkuserid'))) {
    $entity->set("name", $entity->get("name") . " - " . $entity->get('originalLead.vkuserid'));
    //}
    }
    else {
    $entity->set("name", $entity->get("name") . " + ");
    }
    }
    }
    Last edited by Lanket; 07-13-2019, 11:01 PM.
  • Lanket
    Member
    • Jul 2019
    • 51

    #2
    Hello againg.

    I try this way:


    PHP Code:
                    $lead = $this->getEntityManager()->getEntity('Lead');
                    $vkuserid = $lead->get('vkuserid');
                    $entity->set("description", $entity->get("description") . " | vkid - " . $vkuserid); 
    
    But var $vkuserid empy too


    info more:

    File : espocrmWwwRoot/custom/Espo/Custom/Hooks/Opportunity/vkId.php

    PHP Code:
    
    <?php
    namespace Espo\Custom\Hooks\Opportunity;
    
    use Espo\ORM\Entity;
    
    class UnsubscribeSenler extends \Espo\Core\Hooks\Base
    {    
        public function beforeSave(Entity $entity, array $options = array())
        {
            // originalLead
            if ($entity->get('stage') == 'Prepay') {
                    //$entity->set("description", $entity->get("description") . " | vkid - " . strval($entity->get('originalLead.name')));
    
                  //$entity->set("description", $entity->get("description") . " | vkid +- " . implode($options));
                  //  var $options empty too
    
                    $lead = $this->getEntityManager()->getEntity('Lead');
                    $vkuserid = $lead->get('vkuserid');
                    $entity->set("description", $entity->get("description") . " | vkid - " . $vkuserid);
    
            }
            else {
                $entity->set("description", $entity->get("description") . " XXXXXXX ");
            }
        }
    }
    How Get value vkuserid from linked Lead ????
    Last edited by Lanket; 07-13-2019, 11:00 PM.

    Comment


    • Lanket
      Lanket commented
      Editing a comment
      What do wrong?
      Why it don'n work?
      How do it right?
  • eymen-elkum
    Active Community Member
    • Nov 2014
    • 472

    #3
    you must use "vkuserId" instead of "vkuserid"
    CEO of Eblasoft
    EspoCRM Expert since 2014
    Full Stack Web Developer since 2008
    Creator of Numerous Successful Extensions & Projects​

    Comment

    • Lanket
      Member
      • Jul 2019
      • 51

      #4
      Originally posted by ayman.alkom
      you must use "vkuserId" instead of "vkuserid"
      OK. I try it. Thank you.

      Comment

      • Lanket
        Member
        • Jul 2019
        • 51

        #5
        Originally posted by ayman.alkom
        you must use "vkuserId" instead of "vkuserid"
        Not work it.
        For test i try field "name"

        PHP Code:
                        $lead = $this->getEntityManager()->getEntity('Lead');
                        $vkuserid = $lead->get('name');
                        $entity->set("description", $entity->get("description") . " | hasName = " . $lead->has('name') ." | vkid - " . $vkuserid); 
        
        And variable $vkuserid is empty too.

        What i do wrong?

        Comment


        • eymen-elkum
          eymen-elkum commented
          Editing a comment
          you need to pass the lead id to get a lead entity,

          $lead = $this->getEntityManager()->getEntity('Lead', $leadID);
      • Lanket
        Member
        • Jul 2019
        • 51

        #6
        Thank you good man. I will try it.

        Comment

        • Lanket
          Member
          • Jul 2019
          • 51

          #7
          Originally posted by ayman.alkom

          ...
          you need to pass the lead id to get a lead entity,

          $lead = $this->getEntityManager()->getEntity('Lead', $leadID);
          How take $leadId???

          I try this

          PHP Code:
                          $lead = $this->getEntityManager()->getEntity('Lead');
                          $vkuserid = $lead->get('name',$entity->get('originalLead'));
                          $entity->set("description", $entity->get("description") . " | originalLead = " . $entity->get('originalLead') . " | vkid - " . $vkuserid); 
          
          And this:

          PHP Code:
                          $lead = $this->getEntityManager()->getEntity('Lead');
                          $vkuserid = $lead->get('name',$entity->get('leadId'));
                          $entity->set("description", $entity->get("description") . " | originalLead = " . $entity->get('leadId') . " | vkid - " . $vkuserid); 
          
          But both ways don't work
          Var $vkuserid is empty.

          How i can take leadId?

          Comment

          • Lanket
            Member
            • Jul 2019
            • 51

            #8
            I still have not solved the problem, please help. /\ /\ /\ /\ /\ /\

            Comment

            • Lanket
              Member
              • Jul 2019
              • 51

              #9
              Please help. I don't undastand how to do this.

              I try this
              PHP Code:
                                     $vkuserid = count($this->getEntityManager()->getRepository('Lead')->where([
                                          'created_opportunity_id' => $entity->get('id')
                                      ])->findOne()); 
              
              But var $vkuserid = 0

              How take id of entity "Lead" related to entity "Opportunity"?

              Comment

              • Lanket
                Member
                • Jul 2019
                • 51

                #10
                I have taken an interest in contents of the var $options
                PHP Code:
                                $vkuserid = implode($options);
                                $entity->set("description", $entity->get("description") . " | originalLead = " . $entity->get('leadId') . " | vkid - " . $vkuserid); 
                
                and $var $options is empty too. Is it correct?

                Comment

                Working...