DEV Community

Pavel Olnyov
Pavel Olnyov

Posted on

3 1

Can you create db table with Bitrix ORM?

If you use bitrix CMS and cannot create a database table in migration using the built-in ORM (bitrix core d7), then here is my working php-code:

$connection = Application::getConnection();

        try{
            $connection->createTable(
                'table_name',
                [
                    'id' => new Entity\IntegerField(
                        'id',
                        [
                            'column_name' => 'id'
                        ]
                    ),
                    'user_id' => new Entity\IntegerField(
                        'user_id',
                        [
                            'column_name' => 'user_id'
                        ]
                    ),
                    'type' => new Entity\StringField(
                        'type',
                        [
                            'column_name' => 'type'
                        ]
                    ),
                    'create_date' => new Entity\DatetimeField(
                        'create_date',
                        [
                            'column_name' => 'create_date'
                        ]
                    ),
                    'lots_count' => new Entity\IntegerField(
                        'lots_count',
                        [
                            'column_name' => 'lots_count'
                        ]
                    )
                ],
                ['id'],
                ['id']
            );
        }
        catch(\Exception $exception){
            echo $exception->getMessage();
        }

This is very bad. Why specify duplicate field names so many times?
Becouse, the method createTable() is far from perfect:

public function createTable($tableName, $fields, $primary = array(), $autoincrement = array())
    {
        $sql = 'CREATE TABLE '.$this->getSqlHelper()->quote($tableName).' (';
        $sqlFields = array();

        foreach ($fields as $columnName => $field)
        {
            if (!($field instanceof ScalarField))
            {
                throw new ArgumentException(sprintf(
                    'Field `%s` should be an Entity\ScalarField instance', $columnName
                ));
            }

            $realColumnName = $field->getColumnName();

            $sqlFields[] = $this->getSqlHelper()->quote($realColumnName)
                . ' ' . $this->getSqlHelper()->getColumnTypeByField($field)
                . ' NOT NULL' // null for oracle if is not primary
                . (in_array($columnName, $autoincrement, true) ? ' AUTO_INCREMENT' : '')
            ;
        }

        $sql .= join(', ', $sqlFields);

        if (!empty($primary))
        {
            foreach ($primary as &$primaryColumn)
            {
                $realColumnName = $fields[$primaryColumn]->getColumnName();
                $primaryColumn = $this->getSqlHelper()->quote($realColumnName);
            }

            $sql .= ', PRIMARY KEY('.join(', ', $primary).')';
        }

        $sql .= ')';

        if ($this->engine)
        {
            $sql .= ' Engine='.$this->engine;
        }

        $this->query($sql);
    }

Postmark Image

Speedy emails, satisfied customers

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more