User Created Image
User Created Image

Reputation: 1182

Doctrine mongoDB ODM field with multiple types

I need to store integer and string values in one field. What type should I use in mapping? When I use string, I get integer values as string in my mongo documents.

Upvotes: 1

Views: 1628

Answers (2)

MAXakaWIZARD
MAXakaWIZARD

Reputation: 71

Just use @ODM/Field(type="raw") annotation : https://www.doctrine-project.org/projects/doctrine-mongodb-odm/en/latest/reference/basic-mapping.html#doctrine-mapping-types

/**
 * @ODM/Field(type="raw")
 */
protected $value;

Upvotes: 2

Jamie Sutherland
Jamie Sutherland

Reputation: 2790

Use the string mapping. Then in your getter for the field if you need to return different types. Use something like this

public function getTransgenderField() {
    if (is_numeric($this->transgenderField)) {
        return (int) $this->transgenderField;
    }
    return $this->transgenderField;
}

Upvotes: 0

Related Questions