limoncello-php / app

Quick start JSON API application
MIT License
83 stars 7 forks source link

Usage about the getRawAttributes() in ModelInterface #56

Open dreamsbond opened 5 years ago

dreamsbond commented 5 years ago

Curious if the getRawAttributes() can be used for storing spatial data type.

neomerx commented 5 years ago

I think you can. Any type that Doctrine can work with can be used. On their web site, I've found how to make spatial types in Doctrine.

The type I use \Limoncello\Flute\Types\DateTimeType is another example of how to make custom Doctrine type.

dreamsbond commented 5 years ago

could you give some examples of using GetRawAttributes()?

neomerx commented 5 years ago

The spatial types example should be used as a normal type (e.g. Type::INTEGER, Type::STRING or DateTimeType::NAME).

Row Attributes are simply added to SELECT when data are read from a database without any changes. I use them to call various functions and add columns with their results. For example, if I have a column that links to some user, I can add an extra column that would have user's name


    /**
     * @inheritdoc
     */
    public static function getRawAttributes(): array
    {
        return [

                [self::class, 'getRawAttributeFullName'],

            ] + parent::getRawAttributes();
    }

    /**
     * Called to add an extra column in SELECT statements.
     *
     * @param ModelQueryBuilder $builder
     *
     * @return string
     */
    public static function getRawAttributeFullName(ModelQueryBuilder $builder): string
    {
        $function   = UserFullNameFunctionMigration::FUNCTION_NAME;
        $userId     = $builder->getQuotedMainAliasColumn(self::FIELD_ID_USER);
        $columnName = self::V_FIELD_AUTHOR_FULL_NAME;

        return "$function($userId) AS $columnName";
    }