Announcement

Collapse
No announcement yet.

Migrating ORM select queries from 5.x to 7.x

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

  • Migrating ORM select queries from 5.x to 7.x

    I went from 5.x to 7.x, so I am completely lost when it comes to using the ORM to find records at this point. I spent years writing queries like this:
    PHP Code:
    $this->getEntityManager()->getRepository('Project')->where(array('value' => 10))->find(array('name','value')); 
    I know I need to use dependency injection and the query builder. However, nothing I've tried has worked. I initially became confused when findOne wasn't working the way I expected, which led me to realize there are massive differences in the ORM compared to what I am used to. If someone could please help me build a simple query, maybe that will be enough to get me going in the right direction.

    This is what I've been trying to do:

    PHP Code:
    52 $expense $this->entityManager->getRDBRepository('Expenses')->where(array(
    53   'date' => $pDate->format('Y-m-d'),
    54   'amount' => $amount,
    55   'description' => $description,
    56 ))->findOne(['name','amount','date']]); 
    I think I need to do something like this:

    PHP Code:
    $selectQuery $this->entityManager->getQueryBuilder()->where(array(
      
    'date' => $pDate->format('Y-m-d'),
      
    'amount' => $amount,
      
    'description' => $description,
    ))->
    select(['name','amount','date'])->build(); 
    I have no idea if that is correct because I can't get it to work. Also, I don't know what to do with it even if that is the right syntax. The examples show many more statements; this used to be a one-liner, so I must be missing something.
    Last edited by bandtank; 03-27-2022, 01:06 AM.

  • #2
    I feel your pain dealing with the massive refactoring in Espo 7+, although I am sure we will learn to appreciate its advantages, it has forced me to spend a lot more time that I wanted, re-learning to perform what used to be easy tasks

    Try this (I copied the syntaxis from https://github.com/espocrm/espocrm/b...tors/Email.php)
    PHP Code:
    $expense $this->entityManager
    ->getRDBRepository('Expenses')
    ->
    where(['date' => $pDate->format('Y-m-d'), 'amount' => $amount'description' => $description])
    ->
    select(['name','amount','date'])
    ->
    findOne(); 
    I didn't test the example but I copied the syntax from an actual implementation so it should work.

    Comment


    • #3
      Hello,
      look this file :
      Espo\Core\Authentication\TwoFactor\Sms
      you can see "Condition"

      Comment


      • #4
        Thanks - both of the responses are helpful. I am slowly starting to get it, but it's not as easy as it used to be. Hopefully the extra difficulty will result in better control over the data once I get the hang of it.

        Comment

        Working...
        X