Search code examples
phpormredbean

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

Solution

  • 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.