Not a bug Command xf-dev:generate-schema-entity don't correctly generate default value of column

inzanty

Active member
Affected version
2.2.9
For example, we have column in entity, which haves default value with \XF::$time constant:
PHP:
'creation_date'  => ['type' => self::UINT, 'default' => \XF::$time]

But in output we have column with default value 0:
Bash:
// php cmd.php xf-dev:generate-schema-entity BCORE\eCommerce:ShopItemKey

$this->createTable('xf_bcore_ecommerce_shop_item_key', function (\XF\Db\Schema\Create $table)
{
        $table->addColumn('shop_item_key_id', 'int')->autoIncrement();
        $table->addColumn('shop_item_id', 'int');
        $table->addColumn('shop_item_key', 'text');
        $table->addColumn('user_id', 'int');
        $table->addColumn('creation_date', 'int')->setDefault(0);
});

Full entity:
PHP:
public static function getStructure(Structure $structure): Structure
    {
        $structure = parent::getStructure($structure);

        $structure->table .= 'shop_item_key';
        $structure->shortName .= 'ShopItemKey';
        $structure->primaryKey = 'shop_item_key_id';
        $structure->columns = [
            'shop_item_key_id' => ['type' => self::UINT, 'autoIncrement' => true, 'nullable' => true],
            'shop_item_id'     => ['type' => self::UINT, 'required' => true],
            'shop_item_key'    => ['type' => self::STR, 'required' => true],
            'user_id'          => ['type' => self::UINT, 'required' => true],
            'creation_date'    => ['type' => self::UINT, 'default' => XF::$time]
        ];

        $structure->relations = [
            'Item' => [
                'entity'     => 'BCORE\eCommerce:ShopItem',
                'type'       => self::TO_ONE,
                'conditions' => 'shop_item_id',
                'primary'    => true
            ]
        ];

        return $structure;
    }

I mean that \XF::$time in _preSave() of entity, but code will work without ->setDefault(0) in setup, can command see that?
 
Last edited:
\XF::$time is a dynamic PHP variable, so it would be impossible to add its value to the MySQL schema. The only value that makes any sense here is 0.
 


Write your reply...
Back
Top Bottom