public function getTypeId($type, $createIfMissing = true)
    {
        if (null === $this->types) {
            $this->loadCache();
        }

        if (false === ($key = array_search($type, $this->types, true))) {

            if (!$createIfMissing) {
                return null;
            }

            try {
                $this
                    ->backend
                    ->getConnection()
                    ->insert('apb_msg_type')
                    ->fields(array('type' => $type))
                    ->execute()
                ;

            } catch (\PDOException $e) {
                // Another thread went doing this at the same time and created
                // the same type, ignore error and continue
            }

            $this->loadCache();

            return array_search($type, $this->types, true);

        } else {
            return $key;
        }
    }