Announcement

Collapse
No announcement yet.

Relationship ORM help

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

  • Relationship ORM help

    How do i obtain relationship models (entities i guess called in espo crm)?

    coming from Laravel and asp.net core - there u can use the DB ORM to get models straight from models.
    Like from model Book - i can do `$book->author` and this way it would return me entire Author model that was assigned to that specific book model.

    In espo crm - there is a column `account_id` on Email entity. doing `$email->account` returns `null` even tho in database i see the ID...

    NOTE the project is not mine nor i have used espo crm ever. Just trying to understand espo, this project and expend with new integration.

    How do i obtain related model data? how does espo crm data flow?
    Espo crm documentation is so garbage (at least compared to any other i worked with like Laravel) that finding such info is close to impossible.

  • #2
    Code:
    $account = $entityManager->getRDBRepositoryByClass(\Espo\Entities\Email::class)
        ->getRelation($email, 'account')
        ->findOne();

    Comment


    • #3
      So I have to re-define each relationship each time i want to obtain data...
      Thats... annoying and time consuming

      Well thanks. I hoped it would be a proper ORM like in Laravel where you can just define a Model and it does it all but it seems it would have been too good.
      Thanks again, saved me a bunch of time

      Comment


      • #4
        What is proper ORM? Do you have an expertise to call things proper or not proper?

        You used to Laravel's magic that is considered as bad design by many (and for a reason) and now call quite a robust type safe solution not proper.

        > annoying and time consuming

        IDE will autocomplete needed code in a matter of a few seconds. You can move this part to a method and re-use.

        Comment


        • #5
          ORM - object relational mapping.
          it maps all the objects (relationships) for u.
          I shouldint need to re-define my relationships every time. Create a json file with defined relationships. than define again when needing to fetch data. again in other places. Oh, have 5 deep relationship requireemnts? time to define it 5 times! fun! that cna lead to user errors since u have to re-define it - what if u accidently defined the wrong one? bug!

          Laravel, symfony, asp.net core, pretty sure the Java one had it too just dont remember the name since it has been a long time i used it - all the popular ones have a proper mapping db objects. none have issues that u havent even described. none are 'magic'. It does as advertise - it maps. espo crm dosent.

          and espo crm does not work well with vsc. it does not auto complete half of the stuff. most of the stuff it dosent even find.
          php and vsc dont play well. but in laravel case - u have extentions. espo - dosent.

          the espo way aint even type safe since it just returns an object called `entity` - u can do whatever u wish with it. its not type safe.
          its not robust - u need to repeat same re-defined relationships every time.
          it does not map (M for mapping)
          and the bloody repos return entities that are just DB schemas (fields)!
          Instead of returning a proper object (what O in ORM stands for) - it returns a DB scheme... and u need to call `get('column')` method to obtain the actual value instead of simple `->column`. and that can create ever more user errors (miss-typed column name for example).
          on top of that - its time consuming compared to any other frameworks i mentioned.

          tl;dr; its a sloppy design.
          and this entire rant has nothing to do with what i posted so i have no idea why u change the subject without any valuable info or facts.
          i have talked to many people that have worked with both espo and other frameworks and all say the same - espo orm sucks and the lack of docs are annoying.
          every. single. response.

          so plz, dont flood the comments with negative, useless and most cases false facts.
          Thx for trying to help but as i assume - forums should be used as help not rant how u dislike efficiency, automation and newer technologies.
          thx, and have a good boomer day

          Comment


          • #6
            > the espo way aint even type safe since it just returns an object called `entity`

            Code:
            $account = $em->getRDBRepositoryByClass(Account::class)->getById($id);
            Will return an Account object, type hinted by IDE and type checked by the static analysis. No factories with global state.

            > and u need to call `get('column')`

            We can't have real class properties as by design fields can be custom (created from admin UI or added by other extensions, and not requiring any building from CLI). That's why we had to resort to a getter method 'get'. But a developer can define needed type safe getters and setters (by wrapping the 'get' method) and use them in their business logic code.

            Code:
            class MyEntity extends BaseEntity
            {
                
                public function getName(): ?string
                {
                    // You can assert type here for better safety.
                    return $this->get($name);
                }
            
                public function getAccount(): ?Account
                {
                    return $this->entityManager
                        ->getRDBRepository('MyEntity')
                        ->getRelation($this, 'account')
                        ->findOne();
                }
            }​
            I assume that your IDE extension type hints the magic property in Laravel. In fact, there is no property and without the extension you could mistype the name as well. So in Laravel you 'define' a relationship each time you access it as well. Check this article if you want more proofs.​

            It would take a few lines of code to replicate magic properties the way Laravel has in Espo.

            Code:
            class MyEntity extends BaseEntity
            {
                // Now Espo works the same as Laravel. You can write $entity->account.
                // Too bad we don't have a VCS plugin that will type hint this magic. Would you write it for us?
                public function __get($name): mixed
                {
                    return $this->get($name);    
                }
            }​
            ​​​
            Actually, $entity->get('account'), $entity->get('accounts') return an account entity and collection, but I discourage such usage as it's not type safe and in the future it's planned to be replaced with a better approach. It works the same way as in Laravel. The difference is only that Laravel utilizes magic properties upon that.

            > none are 'magic'.

            Here is the Laravel's magic method: https://github.com/illuminate/databa...odel.php#L2230. And see how it calls the 'getAttribute' method. It's the same 'get' method that we have. Every time you call $model->account, it just calls implicitly $model->getAttribute('account'). There's no account property. Static analysis won't catch a mistype in the property name unless you have customized static analysis specifically for Laravel.

            Other 'proper' ORMs do not support custom dynamic fields and relations, they are not designed for such a case. There you either define entity class members which will be used as a schema or a script generate an entity class file from a schema. Both are not an option for our case.

            > Instead of returning a proper object (what O in ORM stands for) - it returns a DB scheme...

            It's just a weird statement. A nonsense I would say. How the Entity is not a proper object? An Entity contains entity definitions for internal purposes, what is the problem with that? The Repository returns a proper entity object (e.g. Account).

            > and espo crm does not work well with vsc. it does not auto complete half of the stuff. most of the stuff it dosent even find.
            php and vsc dont play well. but in laravel case - u have extentions. espo - dosent.​

            PhpStorm autocompletes well both backend and frontend. There are paid VSC extensions for PHP support.

            > not rant how u dislike efficiency, automation and newer technologies

            Such a sarcasm is not welcomed here. So the wording you used in your last posts. If come for help from developers, be polite and respectful. You could have abstained from using words like 'garbage' and others.
            Last edited by yuri; 06-09-2024, 03:01 PM.

            Comment

            Working...
            X