Accessing EntityManager(getEntitity) from Custom PHP

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • info@jaguarsoft.com
    Member
    • Apr 2015
    • 35

    Accessing EntityManager(getEntitity) from Custom PHP

    Hi there',

    I try to get one record in my custom php, located on root but I think I miss something.
    Could you give me help on it; Regaridng ORM: How to manage entities in your code , I try follwing code but I couldn't manage to get it.
    <?php <?php

    // My TEST.PHP
    ini_set('display_startup_errors',1);
    ini_set('display_errors',1);
    error_reporting(E_ALL); //-1 idi
     
    include"bootstrap.php";
    use\Espo\Controllers;
    use\PDO;
    use\Espo\Core\Utils\Config;
    use\Espo\Core\ORM\EntityManager;
    $entname = $_GET['ent'];
    $entid = $_GET['enid'];

    //$app = new \Espo\Core\Application();
    //var_dump($app);
    $rec = new\Espo\Services\Record();
    //var_dump($rec);
    $HOS = $rec->getEntityManager()->getEntity($entname,$entid);
    var_dump($HOS);

    //// ***** I get Following ****

    Fatal error: Call to protected method Espo\Core\Services\Base::getEntityManager() .....




    Thanks in Advance....

    Selcuk





  • yuri
    Member
    • Mar 2014
    • 8440

    #2
    This approach is not good.

    Try

    $entityManager = $app->getContainer()->get('entityManager');
    If you find EspoCRM good, we would greatly appreciate if you could give the project a star on GitHub. We believe our work truly deserves more recognition. Thanks.

    Comment

    • info@jaguarsoft.com
      Member
      • Apr 2015
      • 35

      #3
      Thank you so much. I can get data .. So I think We ensure that all authentiaction stuff works by this way , right ?

      Comment

      • Paulina
        Member
        • Feb 2022
        • 41

        #4
        Hi, I have this code:
        PHP Code:
        <?php
        namespace Espo\Custom\Jobs;
        
        use Espo\Core\Job\Job;
        use Espo\Core\Job\Job\Data;
        use Espo\ORM\Entity;
        use Espo\Core\ORM\EntityManager;
        
        
        class MyJob implements Job
        {
        private $entityManager;
        
        public function __construct(EntityManager $entityManager)
        {
        $this->entityManager = $entityManager;
        }
        
        public function run(Data $data): void
        {
        
        $entityManager = $this->getEntityManager();
        $Scores = $entityManager->getEntity('Scores');
        $Scores->set([
        'player' => 'John',
        'scores' => '5',
        ]);
        $entityManager->saveEntity($Scores);
        
        }
        }
        I want to execute it but I have this error message:
        Error: Job 'MyJob' failed to execute. Call to undefined method Espo\Custom\Jobs\MyJob::getEntityManager()
        Can someone help me why I'mgetting this error?

        Comment

        • telecastg
          Active Community Member
          • Jun 2018
          • 907

          #5
          Hi,

          You don't need to redefine the entityManager object, you initialized it in the line:
          $this->entityManager = $entityManager

          So try:
          $Scores = $this->entityManager->getEntity('Scores');

          Instead of:
          $entityManager = $this->getEntityManager();
          $Scores = $entityManager->getEntity('Scores');

          Comment

          • smartinsar
            Member
            • Dec 2021
            • 33

            #6
            Hi,
            can you help me with access to entity manager RDBRepository;
            Here is my code
            Code:
            <?php
            
            namespace Espo\Custom\Services;
            
            use Espo\ORM\Entity;
            use Espo\ORM\EntityManager;
            
            class TransactionSync extends \Espo\Core\Templates\Services\Base
            {
            
            public function bankPairTransactions($workflowId, Entity $entity, $additionalParameters = null){
            
            
            $varSym = $entity->get('name'); // 
            
            $collection = $this->$entityManager->getRDBRepository('SalesOrder')->where(['number' => $varSym, ])->find();
            
            }
            }
            I receive the error on start function: ERROR: Process 62271279d2a812754 element j29gtou8b1: Call to a member function getRDBRepository() on null [] []



            What should I do wrong? I've follow this guide https://docs.espocrm.com/development/orm/#find

            Thank you

            Comment

            • alter
              Member
              • Apr 2018
              • 57

              #7
              Originally posted by smartinsar
              Hi,
              can you help me with access to entity manager RDBRepository;
              Here is my code
              Code:
              <?php
              
              namespace Espo\Custom\Services;
              
              use Espo\ORM\Entity;
              use Espo\ORM\EntityManager;
              
              class TransactionSync extends \Espo\Core\Templates\Services\Base
              {
              
              public function bankPairTransactions($workflowId, Entity $entity, $additionalParameters = null){
              
              
              $varSym = $entity->get('name'); //
              
              $collection = $this->$entityManager->getRDBRepository('SalesOrder')->where(['number' => $varSym, ])->find();
              
              }
              }
              I receive the error on start function: ERROR: Process 62271279d2a812754 element j29gtou8b1: Call to a member function getRDBRepository() on null [] []



              What should I do wrong? I've follow this guide https://docs.espocrm.com/development/orm/#find

              Thank you
              Hi, you are calling getRDBRepository() on null - because $this->$entityManager does not exist. Add a constructor and initiliaze entityManager there. So it would be something like this:

              PHP Code:
              class TransactionSync ....
              
              private $entityManager;
              
              public function __construct(EntityManager $entityManager) 
              {
              $this->entityManager = $entityManager;
              }
              
              $collection = $this->entityManager->getRDBRepository() 
              
              Beware of correct accessing to properties of the class - if you have "private $entityManager", the correct way to access it is "$this->entityManager", not "$this->$entityManager".

              Comment

              Working...