我怎样才能更改 redbeanphp 中的列类型?

how could I change the type of column in redbeanphp?

我正在使用RedBeanPHP,我有一串数字,是否可以像字符串一样存储它,而不是双精度类型?接下来是我的示例,但它不起作用:

    $participant = R::dispense('participants'); 
    $participant->setMeta("participants.number","string");
    $participant->number = $number;
    R::store($participant);

Redbean 检查给定的属性值来为列设置正确的类型,我建议您每次都关闭冻结选项,但是,当您需要更改某些内容时,只需将其打开即可。我的意思是,当你真的需要对你执行一些更改时,只需打开冻结 table,例如:

// the false param will disable db changes
R::setup('dns', 'user', 'pass', false);changes (freeze option)

//...
//... let's imagine you have some code here
//...

R::freeze(false);

$participant = R::dispense('participants');
$participant->number = 'intert any string'; // need to set field to string type
R::store($participant);

R::freeze(true);

$participant->number = '99.99';
R::store($participant);

我知道这不是最好的东西,但是当你需要改变数据库结构的时候你只需要打开它。本质上,在生产环境中,您应该始终将其关闭

RedBean 将自动尝试为您提供的数据猜测正确的列类型。但是,它永远不会缩小列(例如从 TEXTINTEGER),只会加宽(例如从 INTEGERTEXT)。

如果在开发过程中数据库列是 TEXT 对您很重要,那么您可以插入 string 并再次将其删除到 "trick" RedBean 中,使列类型成为 TEXT

例如,将此代码片段放入某种类型的初始化脚本中:

$participant = R::dispense('participants'); 
$participant->number = 'not a number';
R::store($participant);
R::trash($participant);

// Column 'participants.number' is now of type TEXT

正如我之前提到的,RedBean 永远不会将列缩小到 INTEGER,即使您不再插入数字字符串以外的任何内容。

另一方面,如果在开发过程中它对您来说并不重要,您可以在部署到生产环境之前冻结数据库并在数据库管理器中手动将列类型更改为 TEXT