4

I am using Magento 2.2.6 version. Am just trying to add an extra column updated_at in setup/upgradeschema.php. But on saving data, it is not updating the timestamp automatically. It is null always. Here is my code,

$installer->getConnection()->addColumn(
                'updated_at',
                \Magento\Framework\DB\Ddl\Table::TYPE_TIMESTAMP,
                null,
                ['nullable' => false, 'default' => \Magento\Framework\DB\Ddl\Table::TIMESTAMP_INIT_UPDATE],
                'Updated At'
            );

Adding the above code in

setup/InstallSchema.php

or

in upgradeschema[But only on creating the whole new table]

works fine. Can someone help?

5 Answers 5

5

We have to empty the field updated_at after every changes in beforeSave function

public function beforeSave()
{
    if ($this->hasDataChanges()) {
        $this->setUpdateTime(null);
    }

    return parent::beforeSave();
}
1

First of all the file name should be UpgradeSchema.php and in that file you need have a check like

if (version_compare($context->getVersion(), '<your_module_version>') < 0) { //your code }

Secondly you need to increment the module version in etc/module.xml and then run the bin/magento setup:upgrade
Now you can see your column is updated in the database.
If so, then you can see updated time in the column.

0

I think because your default key using Uppercase, try change your code to be like this :

...
->addColumn(
        'updated_at',
        \Magento\Framework\DB\Ddl\Table::TYPE_TIMESTAMP,
        null,
        ['nullable' => false, 'default' => \Magento\Framework\DB\Ddl\Table::TIMESTAMP_INIT_UPDATE],
        'Updated At'
...

if it does not work, you can see this link for more reference :

How do auto add datetime in InstallSchema magento2?

2
  • I tried giving 'DEFAULT' in lowercase too but doesn't work. I will edit my question to change 'DEFAULT' in lowercase.
    – Octopus
    Commented Sep 21, 2018 at 6:29
  • try to run bin/magento cache:flush in command line , if it still does not work I think you can try to follow this way magento.stackexchange.com/questions/84255/…
    – mrfizh
    Commented Sep 21, 2018 at 6:42
0

Try this

->addColumn(
            'update_datetime',
            \Magento\Framework\DB\Ddl\Table::TYPE_TIMESTAMP,
            null,
            [],
            'Modification Date Time'
        )
0

Follow below steps to update the created_at and update_at columns on saving the data in the table.

  1. Create created_at and update_at columns in the database table.

................. <column xsi:type="timestamp" name="created_at" on_update="false" nullable="false" default="CURRENT_TIMESTAMP" comment="Creation Time"/> <column xsi:type="timestamp" name="updated_at" on_update="true" nullable="false" default="CURRENT_TIMESTAMP" comment="Update Time"/> .................

  1. Update the Model/CustomTable.php file to add beforeSave() function.
use Magento\Framework\Api\DataObjectHelper;
class CustomTable extends \Magento\Framework\Model\AbstractModel
{
    /**
     * @var \Magento\Framework\Stdlib\DateTime
     */
    private $dateTime;

    public function __construct(
        \Magento\Framework\Model\Context $context,
        \Magento\Framework\Registry $registry,
        \Magento\Framework\Stdlib\DateTime $dateTime,
        DataObjectHelper $dataObjectHelper,
        array $data = []
    ) {
        $this->dateTime = $dateTime;
        $this->dataObjectHelper = $dataObjectHelper;
        parent::__construct(....);
    }

    /**
     * Prepare data to be saved to database.
     *
     * @return $this
     */
    public function beforeSave()
    {
        parent::beforeSave();
        if ($this->isObjectNew() && !$this->getCreatedAt()) {
            $this->setCreatedAt($this->dateTime->formatDate(true));
        }
        $this->setUpdatedAt($this->dateTime->formatDate(true));

        return $this;
    }
}

Hope this works for you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.