简体   繁体   中英

how could I change the type of column in redbeanphp?

I am using the RedBeanPHP, I have a string of numbers, is it possible to store it like the string, not a double type? My example is next, and it doesn't work:

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

Redbean check attribute given value to set the right type for the column, I would recommend you to work with freeze option off every time, but, when you need to change something, you just turn it on. I mean, just turn freeze on when you really need to perform some change on you table, for ex:

// 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);

I know it is not the best thing ever, but you just have to turn it on when you need to change something on DB structure. Essentially, in production environment you should always turn it off

RedBean will automatically try to guess the right column type for the data you provide. However, it will never shrink a column, (for example from TEXT to INTEGER ), only widen (for example from INTEGER to TEXT ).

If it's important for you that the database column is TEXT during development, you could therefore insert a string and delete it again to "trick" RedBean into making the column type TEXT .

For example, put this code snippet into some type of initialization script:

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

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

As I mentioned earlier, RedBean will never shrink the column to INTEGER even if you never insert anything else than number strings again.

On the other hand, if it's not critical to you during development, you could just freeze the database before deploying to production and manually change the column type to TEXT in your database manager.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM