Skip to content
Advertisement

ORM model for foreign database in typo3

I am writing a typo3 plugin that needs to access a database table of a pre-defined structure on a configurable MySQL / MariaDB database server. This is the model I wrote:

<?php

namespace HomeinfoSysMon2DomainModel;

use TYPO3CMSExtbaseDomainObjectAbstractEntity;

class CheckResults extends AbstractEntity
{
    /**
     * @var int $id
     */
    public $id;

    /**
     * @var DateTime $timestamp
     */
    public $timestamp;

    /**
     * @var int $system
     */
    public $system;

    /**
     * @var bool $icmp_request 
     */
    public $icmp_request;

    /**
     * @var string $ssh_login  
     */
    public $ssh_login ;

    /**
     * @var string $http_request 
     */
    public $http_request;

    /**
     * @var string $application_state 
     */
    public $application_state;

    /**
     * @var string $smart_check 
     */
    public $smart_check;

    /**
     * @var string $baytrail_freeze 
     */
    public $baytrail_freeze;

    /**
     * @var string $fsck_repair 
     */
    public $fsck_repair;

    /**
     * @var bool $application_version 
     */
    public $application_version;

    /**
     * @var int $ram_total 
     */
    public $ram_total;

    /**
     * @var int $ram_free 
     */
    public $ram_free;

    /**
     * @var int $ram_available 
     */
    public $ram_available;

    /**
     * @var string $efi_mount_ok 
     */
    public $efi_mount_ok;

    /**
     * @var int $download 
     */
    public $download;

    /**
     * @var int $upload 
     */
    public $upload;

    /**
     * @var string $root_not_ro 
     */
    public $root_not_ro;

    /**
     * @var string $sensors 
     */
    public $sensors;

    /**
     * @var bool $in_sync 
     */
    public $in_sync;

    /**
     * @var int $recent_touch_events 
     */
    public $recent_touch_events;

    /**
     * @var DateTime $offline_since 
     */
    public $offline_since;

    /**
     * @var DateTime $blackscreen_since 
     */
    public $blackscreen_since;
}

Of course this does not work as it stands, since Typo3 assumes the model to represent a table as defined by ext_tables.{php,sql} within the local typo3 database. But I want typo3 to use this model from another, configurable database server, identified by

  • host name
  • port
  • user name
  • password

How would I go about that? I want to use as much high-level abstraction as possible and do not want to write bare SQL queries using mysqli or the like.

Advertisement

Answer

You can have multiple databases attached to TYPO3 and establish a mapping for which tables this special new database should be used, refer to https://docs.typo3.org/m/typo3/reference-coreapi/main/en-us/Configuration/Typo3ConfVars/DB.html#tablemapping for your specific version but the example mentioned there

'Connections' => [
  'Default' => [
    // ...
  ],
  'Syslog' => [
    'charset' => 'utf8mb4',
    'driver' => 'mysqli',
    'dbname' => 'syslog_dbname',
    'host' => 'syslog_host',
    'password' => '***',
    'port' => 3306,
    'user' => 'syslog_user',
  ],
],
'TableMapping' => [
  'sys_log' => 'Syslog',
]

should give you a good glimpse. Depending on your use case it could be helpful to

  1. have read-only SQL access to that database so that a destructive database compare does not invalidate the foreign system.
  2. or have some additional TYPO3-friendly fields (e.g. uid, pid) in it which help achieving what you want.
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement