Real Estate. afterSave for Properties

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Galaxy Express
    Junior Member
    • Feb 2016
    • 28

    Real Estate. afterSave for Properties

    I'm trying to customize Real Estate extension for my needs (and to understand how extensions are working and how to make 'em).
    There is file application/Espo/Modules/RealEstate/Repositories/RealEstateRequest.php in which function afterSave is using to form name of request.

    PHP Code:
        public function afterSave(Entity $entity, array $options)
        {
            $result = parent::afterSave($entity, $options);
            $this->handleAfterSaveContacts($entity, $options);
    
            if ($entity->isNew() && !$entity->get('name')) {
    
                $e = $this->get($entity->id);
                $name = strval($e->get('number'));
                $name = str_pad($name, 6, '0', STR_PAD_LEFT);
                $name = 'R ' . $name;
    
                $e->set('name', $name);
                $this->save($e);
                $entity->set('name', $name);
                $entity->set('number', $e->get('number'));
            }
    
            return $result;
        } 
    
    I want to make something similar for Properties. So I created entity "propertyName" (Varchar) for RealEstateProperty (in Entity manager) and placed it to Layouts.
    I also copied those public function afterSave(Entity $entity, array $options) from application/Espo/Modules/RealEstate/Repositories/RealEstateRequest.php
    to application/Espo/Modules/RealEstate/Repositories/RealEstateProperty.php and edited it, so it's now looks like:

    PHP Code:
    <?php
    
    namespace Espo\Modules\RealEstate\Repositories;
    
    class RealEstateProperty extends \Espo\Core\Templates\Repositories\Base
    {
        public function beforeSave(Entity $entity, array $options)
        {
            $propertyType = $entity->get('type');
    
            switch ($propertyType) {
                case 'Apartment':
                    $entity->set('floorCount', null);
                    break;
                case 'Separate House':
                    $entity->set('floor', null);
                    break;
                case 'Office':
                    $entity->set('bedroomCount', null);
                    $entity->set('bathroomCount', null);
                    $entity->set('floorCount', null);
                case 'Warehouse':
                case 'Retail':
                case 'Farm':
                    $entity->set('floor', null);
                    $entity->set('bedroomCount', null);
                    $entity->set('bathroomCount', null);
                    $entity->set('floorCount', null);
                    break;
            }
    
            return parent::beforeSave($entity, $options);
        }
    
        public function afterSave(Entity $entity, array $options)
        {
            $result = parent::afterSave($entity, $options);
            $this->handleAfterSaveContacts($entity, $options);
    
            if ($entity->isNew() && !$entity->get('propertyName')) {
    
                $e = $this->get($entity->id);
                $propertyName = strval($e->get('number'));
                $propertyName = str_pad($propertyName, 6, '0', STR_PAD_LEFT);
                $propertyName = 'P ' . $propertyName;
    
                $e->set('propertyName', $propertyName);
                $this->save($e);
                $entity->set('propertyName', $propertyName);
                $entity->set('number', $e->get('number'));
            }
    
            return $result;
        }
    
    
    }
    So, if I creating new Property, I enter name to propertyName field (for example Qwerty)
    What I want to get in list view later - something like "Qwerty P 000003"
    What I get with it - only "Qwerty" and pack of errors in log:

    Code:
    [2016-03-02 09:38:06] Espo.ERROR: E_RECOVERABLE_ERROR: Argument 1 passed to Espo\Modules\RealEstate\Repositories\RealEstateProperty::beforeSave() must be an instance of Espo\Modules\RealEstate\Repositories\Entity, instance of Espo\Modules\RealEstate\Entities\RealEstateProperty given, called in /var/www/espocrm/application/Espo/ORM/Repositories/RDB.php on line 141 and defined {"file":"/var/www/espocrm/application/Espo/Modules/RealEstate/Repositories/RealEstateProperty.php","line":7} []
    [2016-03-02 09:38:06] Espo.ERROR: E_RECOVERABLE_ERROR: Argument 1 passed to Espo\Modules\RealEstate\Repositories\RealEstateProperty::afterSave() must be an instance of Espo\Modules\RealEstate\Repositories\Entity, instance of Espo\Modules\RealEstate\Entities\RealEstateProperty given, called in /var/www/espocrm/application/Espo/ORM/Repositories/RDB.php on line 149 and defined {"file":"/var/www/espocrm/application/Espo/Modules/RealEstate/Repositories/RealEstateProperty.php","line":35} []
    What I'm doing wrong and what I'm missing?
    (versions: EspoCRM 4.0.0 and Real Estate 1.0.2)
    Last edited by Galaxy Express; 03-02-2016, 09:44 AM.
  • yuri
    Member
    • Mar 2014
    • 8478

    #2
    $entity->set('name', ...);
    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

    • Galaxy Express
      Junior Member
      • Feb 2016
      • 28

      #3
      So it will be something like this? :
      PHP Code:
          public function afterSave(Entity $entity, array $options)
          {
              $result = parent::afterSave($entity, $options);
      
              if ($entity->isNew() && !$entity->get('name')) {
      
                  $e = $this->get($entity->id);
                  $ep = $entity->get('propertyName');
                  $name = strval($e->get('number'));
                  $name = str_pad($name, 6, '0', STR_PAD_LEFT);
                  $name = $ep . ' P ' . $name;
                  
                  $e->set('name', $name);
                  $this->save($e);
                  $entity->set('name', $name);
                  $entity->set('number', $e->get('number'));
              }
      
              return $result;
          } 
      
      At least I get "Qwerty" in "propertyName" and "Qwerty P 000003" in "name" with it...

      And those E_RECOVERABLE_ERROR's - are they very deadly or not very?

      Comment

      • yuri
        Member
        • Mar 2014
        • 8478

        #4
        That error doesn't affect anything..
        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

        • Galaxy Express
          Junior Member
          • Feb 2016
          • 28

          #5
          Do I understand right that this function run only if record is new ( $entity->isNew() ) and there is nothing in 'name' ( !$entity->get('name') ) ?
          How to make it also run when record is not new and I change that 'propertyName' value? For example I have already created Property with 'name' Qwerty P 000003 and want to edit it to change 'propertyName' from Qwerty to Toster and to get 'name' Toster P 000003.
          If I remove "if" construction or copy code to "else" - I get long loop (which is logical). Googling do not give me suitable results for my php madskillz.

          PHP Code:
              public function afterSave(Entity $entity, array $options)
              {
                  $result = parent::afterSave($entity, $options);
          
                  if ($entity->isNew() && !$entity->get('name')) {
          
                      $e = $this->get($entity->id);
                      $name = strval($e->get('number'));
                      $name = str_pad($name, 6, '0', STR_PAD_LEFT);
                      $name = $entity->get('propertyName') . ' P ' . $name;
                      
                      $e->set('name', $name);
                      $this->save($e);
                      $entity->set('name', $name);
                      $entity->set('number', $e->get('number'));
                      
                  }
          
              } 
          

          Comment

          • yuri
            Member
            • Mar 2014
            • 8478

            #6
            Right. You can remove $entity->isNew() check.
            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

            • Galaxy Express
              Junior Member
              • Feb 2016
              • 28

              #7
              If to remove $entity->isNew() check only - it will not give anything.
              If to also remove !$entity->get('name') - it will be very long loop.

              Comment

              • yuri
                Member
                • Mar 2014
                • 8478

                #8
                Try to move it to beforeSave method. And don't call save() there.
                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

                • Galaxy Express
                  Junior Member
                  • Feb 2016
                  • 28

                  #9
                  But will it be possible to get 'number' (autoincrementing id) if to run it from beforeSave?
                  And what to use instead of save() ?

                  Comment

                  • yuri
                    Member
                    • Mar 2014
                    • 8478

                    #10
                    Unfortunately no. You can use afterSave but pass some token in $options to preven looping.
                    $this->save($entity, array('noLoop => true));

                    and then check !empty($options['noLoop'])
                    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

                    • Galaxy Express
                      Junior Member
                      • Feb 2016
                      • 28

                      #11
                      I do not understand. Really.
                      Something like this?
                      PHP Code:
                          public function afterSave(Entity $entity, array $options)
                          {
                      
                              $result = parent::afterSave($entity, $options);
                      
                              if ($entity->isNew() && !$entity->get('name')) {
                      
                                  $e = $this->get($entity->id);
                                  $name = strval($e->get('number'));
                                  $name = str_pad($name, 6, '0', STR_PAD_LEFT);
                                  $name = $entity->get('propertyName') . ' P ' . $name;
                                  
                                  $e->set('name', $name);
                                  $this->save($e);
                                  $entity->set('name', $name);
                                  $entity->set('number', $e->get('number'));
                                  
                              } else
                      
                                  if (!empty($options['noLoop'])) {
                      
                                      $this->save($entity, array('noLoop' => true));
                      
                                      $e = $this->get($entity->id);
                                      $name = strval($e->get('number'));
                                      $name = str_pad($name, 6, '0', STR_PAD_LEFT);
                                      $name = $entity->get('propertyName') . ' P ' . $name;
                                  
                                      $e->set('name', $name);
                                      $this->save($e);
                                      $entity->set('name', $name);
                                      $entity->set('number', $e->get('number'));
                                      
                                  }
                      
                          } 
                      
                      Last edited by Galaxy Express; 03-03-2016, 11:38 AM.

                      Comment

                      • yuri
                        Member
                        • Mar 2014
                        • 8478

                        #12
                        PHP Code:
                            public function afterSave(Entity $entity, array $options)
                            {
                        
                                $result = parent::afterSave($entity, $options);
                        
                                if (empty($options['noLoop'])) {
                        
                                    ....
                                    $name = ...;
                                    ...
                                    
                                    $e->set('name', $name);
                                    $this->save($e, array('noLoop' => true));
                        
                                    
                                }
                            } 
                        
                        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

                        • Galaxy Express
                          Junior Member
                          • Feb 2016
                          • 28

                          #13
                          Will it work only once (for one such renaming because token 'noLoop' become stored populated after it)?
                          Also will it need additional column 'noLoop' in database? EspoCRM error 500 me if I insert this code into it.

                          Comment

                          • yuri
                            Member
                            • Mar 2014
                            • 8478

                            #14
                            noLoop is a temporary token. It will work each time the record is saved.
                            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

                            Working...