propelorm / Propel2

Propel2 is an open-source high-performance Object-Relational Mapping (ORM) for modern PHP
http://propelorm.org/
MIT License
1.26k stars 399 forks source link

FRW-2182 extend TimestampableBehavior with is_timestamp parameter to allow configuration of datetime type for created_at and updated_at #1971

Open maritechpro opened 1 year ago

maritechpro commented 1 year ago

FRW-2182 extend TimestampableBehavior with is_timestamp parameter to allow configuration of datetime type for created_at and updated_at

Behavior should be extendable on 2.0.0-beta2 (2.0.1-beta2) and 2.0.0-beta3 (2.0.1-beta3) versions. Thank you.

maritechpro commented 1 year ago
  1. Adjust field TimestampableBehavior::parameters

    /**
     * @var array<string, mixed>
     */
    protected $parameters = [
        'create_column' => 'created_at',
        'update_column' => 'updated_at',
        'disable_created_at' => 'false',
        'disable_updated_at' => 'false',
        'is_timestamp' => 'true',
    ];
  2. Add method TimestampableBehavior::isTimestamp() :

    
    /**
     * @return bool
     */
    protected function isTimestamp(): bool
    {
        return $this->booleanValue($this->getParameter('is_timestamp'));
    }
  3. Adjust method TimestampableBehavior::modifyTable() :

    
    /**
     * Add the create_column and update_columns to the current table
     *
     * @return void
     */
    public function modifyTable(): void
    {
        $table = $this->getTable();
    
        if ($this->withCreatedAt() && !$table->hasColumn($this->getParameter('create_column'))) {
            $table->addColumn([
                'name' => $this->getParameter('create_column'),
                'type' => $this->isTimestamp() ? 'TIMESTAMP' : 'DATETIME',
            ]);
        }
        if ($this->withUpdatedAt() && !$table->hasColumn($this->getParameter('update_column'))) {
            $table->addColumn([
                'name' => $this->getParameter('update_column'),
                'type' => $this->isTimestamp() ? 'TIMESTAMP' : 'DATETIME',
            ]);
        }
    }
wollanup commented 1 year ago

Hey, I'm opening a PR with your code @maritechpro and will try to add some tests.

SilverSnake83 commented 4 months ago

I would greatly appreciate this kind of changes. The propel diff are now nearly impossible to use due to thousands of detected diff that must be manually deleted from the result of the diff everytime.
For instance :


ALTER TABLE `table`

  CHANGE `created_at` `created_at` TIMESTAMP NULL,

  CHANGE `updated_at` `updated_at` TIMESTAMP NULL;