Announcement

Collapse
No announcement yet.

Accessing EntityManager(getEntitity) from Custom PHP

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

  • alter
    replied
    Originally posted by smartinsar View Post
    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".

    Leave a comment:


  • smartinsar
    replied
    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

    Leave a comment:


  • telecastg
    replied
    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');

    Leave a comment:


  • Paulina
    replied
    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?

    Leave a comment:


  • info@jaguarsoft.com
    replied
    Thank you so much. I can get data .. So I think We ensure that all authentiaction stuff works by this way , right ?

    Leave a comment:


  • yuri
    replied
    This approach is not good.

    Try

    $entityManager = $app->getContainer()->get('entityManager');

    Leave a comment:


  • 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





Working...
X