propelorm / Propel2

Propel2 is an open-source high-performance Object-Relational Mapping (ORM) for modern PHP
http://propelorm.org/
MIT License
1.26k stars 393 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 10 months ago

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