XF 2.0 Update/Insert to DB Table

Cupara

Well-known member
So I have learned to create tables using the Setup.php file but have found nada on doing updates and inserts to said table.

PHP:
$sm = \XF::db()->getSchemaManager();
$sm->createTable('xf_some_table', function(\XF\Db\Schema\Create $table)
{
    $table->addColumn('some_id', 'int')->autoIncrement();
    $table->addColumn('some_name', 'varchar', 50);
});

That is from the documentation for XF2. So how does one process updating a table or insert to that table.

Thanks
 
You'd do them the same way you would do them from anywhere else.

There are insert and update methods on the db() object:
PHP:
/**
* @param string $table
* @param array  $rawValues
* @param bool   $replaceInto
* @param bool   $onDupe
* @param string $modifier
*
* @return int The number of affected rows.
*/
public function insert($table, array $rawValues, $replaceInto = false, $onDupe = false, $modifier = '');

/**
* @param string $table
* @param array  $cols
* @param string $where
* @param string $modifier
* @param string $order
* @param int    $limit
*
* @return int The number of affected rows.
*/
public function update($table, array $cols, $where, $params = [], $modifier = '', $order = '', $limit = 0);

Usage:
PHP:
\XF::db()->insert($table, $rawValues [, $replaceInto, $onDupe, $modifier]);
\XF::db()->update($table, $cols, $where [, $params , $modifier, $order, $limit]);
 
Last edited:
You'd do them the same way you would do them from anywhere else.

There are insert and update methods on the db() object:
PHP:
/**
* @param string $table
* @param array  $rawValues
* @param bool   $replaceInto
* @param bool   $onDupe
* @param string $modifier
*
* @return int The number of affected rows.
*/
public function insert($table, array $rawValues, $replaceInto = false, $onDupe = false, $modifier = '');

/**
* @param string $table
* @param array  $cols
* @param string $where
* @param string $modifier
* @param string $order
* @param int    $limit
*
* @return int The number of affected rows.
*/
public function update($table, array $cols, $where, $params = [], $modifier = '', $order = '', $limit = 0);

Usage:
PHP:
\XF::db()->insert($table, $rawValues [, $replaceInto, $onDupe, $modifier]);
\XF::db()->update($table, $cols, $where [, $params , $modifier, $order, $limit]);

So this would be correct then:
PHP:
$db = \XF::db()->getSchemaManager();
$db->insert('xwow_realms', realm_stat [, $new['realm_stat'], $onDupe, $modifier]);
 
[,... denotes that the arguments that follow are optional.

To insert a new row if you're in a setup class:
PHP:
$db = $this->db();
$db->insert('xwow_realms', [
    'column'  => 'value',
    'column2' => 'value2'
]);

The schema manager is for managing the schema (tables/columns) only. If you mean how can you add/update existing table schema:
PHP:
$sm = $this->schemaManager();
$sm->alterTable('xwow_realms', function (\XF\Db\Schema\Alter $table) {
    $table->addColumn('realm_stat', 'int');
});
 
The first one you gave is correct. I take it I use the update method in the same manner as the insert method?
 
Pretty much, just pass in a where clause (as a string) as the third argument. It's probably best to use parameters for it too:
PHP:
$db->update(
    'xwow_realms' 
    [
        'column'  => 'newValue',
        'column2' => 'newValue2'
    ]
    'column =  ?',
    ['oldColumnValue']
);
 
Top Bottom