More Related Content
Similar to Drupal8 DBに保存して読み込む (8)
More from iPride Co., Ltd. (20)
Drupal8 DBに保存して読み込む
- 13. フォーム画面を作る
〜vim src/Form/InputForm.php〜
• 完成図
vim src/Form/InputForm.php
<?php
namespace DrupalanimalbookForm;
use DrupalCoreFormFormBase;
use DrupalCoreFormFormStateInterface;
use DrupalCoreDatabaseDatabase;
class InputForm extends FormBase {
public function getFormId() {
return 'default_form';
}
public function buildForm(array $form,
FormStateInterface $form_state) {
$typelist = array(
$typelist = array(
'MAMMALS' => t('哺乳類'),
'BIRDS' => t('鳥類'),
'REPTILES' => t('爬虫類'),
'AMPHIBIANS' => t('両生類'),
'FISH' => t('魚類'),
);
public function validateForm(array &$form,
FormStateInterface $form_state) {
parent::validateForm($form, $form_state);
}
public function submitForm(array &$form,
FormStateInterface $form_state) {
// Display result.
foreach ($form_state->getValues() as $key => $value) {
drupal_set_message($key . ': ' . $value);
}
// Save to Database.
$conn = Database::getConnection();
$conn->insert('module_animalbook_data')->fields(
array(
'name' => $form_state->getValue('name'),
'type' => $form_state->getValue('type'),
)
)->execute();
}
}
$form['name'] = array(
'#type' => 'textfield',
'#title' => $this->t('Name'),
'#required' => TRUE,
);
$form['type1'] = array(
'#type' => 'select',
'#title' => 'タイプ',
'#options' => $typelist,
'#required' => TRUE,
);
$form['submit'] = [
'#type' => 'submit',
'#value' => $this->t('Save'),
];
return $form;
}
- 14. フォーム画面を作る
〜vim src/Form/InputForm.php〜
• 完成図
vim src/Form/InputForm.php
<?php
namespace DrupalanimalbookForm;
use DrupalCoreFormFormBase;
use DrupalCoreFormFormStateInterface;
use DrupalCoreDatabaseDatabase;
class InputForm extends FormBase {
public function getFormId() {
return 'default_form';
}
public function buildForm(array $form,
FormStateInterface $form_state) {
$typelist = array(
$typelist = array(
'MAMMALS' => t('哺乳類'),
'BIRDS' => t('鳥類'),
'REPTILES' => t('爬虫類'),
'AMPHIBIANS' => t('両生類'),
'FISH' => t('魚類'),
);
public function validateForm(array &$form,
FormStateInterface $form_state) {
parent::validateForm($form, $form_state);
}
public function submitForm(array &$form,
FormStateInterface $form_state) {
// Display result.
foreach ($form_state->getValues() as $key => $value) {
drupal_set_message($key . ': ' . $value);
}
// Save to Database.
$conn = Database::getConnection();
$conn->insert('module_animalbook_data')->fields(
array(
'name' => $form_state->getValue('name'),
'type' => $form_state->getValue('type'),
)
)->execute();
}
}
$form['name'] = array(
'#type' => 'textfield',
'#title' => $this->t('Name'),
'#required' => TRUE,
);
$form['type1'] = array(
'#type' => 'select',
'#title' => 'タイプ',
'#options' => $typelist,
'#required' => TRUE,
);
$form['submit'] = [
'#type' => 'submit',
'#value' => $this->t('Save'),
];
return $form;
}
// Save to Database.
$conn = Database::getConnection();
$conn->insert('module_animalbook_data')->fields(
array(
'name' => $form_state->getValue('name'),
'type' => $form_state->getValue('type'),
)
)->execute();
- 15. フォーム画面を作る
〜vim src/Form/InputForm.php〜
• 完成図
vim src/Form/InputForm.php
<?php
namespace DrupalanimalbookForm;
use DrupalCoreFormFormBase;
use DrupalCoreFormFormStateInterface;
use DrupalCoreDatabaseDatabase;
class InputForm extends FormBase {
public function getFormId() {
return 'default_form';
}
public function buildForm(array $form,
FormStateInterface $form_state) {
$typelist = array(
$typelist = array(
'MAMMALS' => t('哺乳類'),
'BIRDS' => t('鳥類'),
'REPTILES' => t('爬虫類'),
'AMPHIBIANS' => t('両生類'),
'FISH' => t('魚類'),
);
public function validateForm(array &$form,
FormStateInterface $form_state) {
parent::validateForm($form, $form_state);
}
public function submitForm(array &$form,
FormStateInterface $form_state) {
// Display result.
foreach ($form_state->getValues() as $key => $value) {
drupal_set_message($key . ': ' . $value);
}
// Save to Database.
$conn = Database::getConnection();
$conn->insert('module_animalbook_data')->fields(
array(
'name' => $form_state->getValue('name'),
'type' => $form_state->getValue('type'),
)
)->execute();
}
}
$form['name'] = array(
'#type' => 'textfield',
'#title' => $this->t('Name'),
'#required' => TRUE,
);
$form['type1'] = array(
'#type' => 'select',
'#title' => 'タイプ',
'#options' => $typelist,
'#required' => TRUE,
);
$form['submit'] = [
'#type' => 'submit',
'#value' => $this->t('Save'),
];
return $form;
}
// Save to Database.
$conn = Database::getConnection();
$conn->insert('module_animalbook_data')->fields(
array(
'name' => $form_state->getValue('name'),
'type' => $form_state->getValue('type'),
)
)->execute();
DBに接続
- 16. フォーム画面を作る
〜vim src/Form/InputForm.php〜
• 完成図
vim src/Form/InputForm.php
<?php
namespace DrupalanimalbookForm;
use DrupalCoreFormFormBase;
use DrupalCoreFormFormStateInterface;
use DrupalCoreDatabaseDatabase;
class InputForm extends FormBase {
public function getFormId() {
return 'default_form';
}
public function buildForm(array $form,
FormStateInterface $form_state) {
$typelist = array(
$typelist = array(
'MAMMALS' => t('哺乳類'),
'BIRDS' => t('鳥類'),
'REPTILES' => t('爬虫類'),
'AMPHIBIANS' => t('両生類'),
'FISH' => t('魚類'),
);
public function validateForm(array &$form,
FormStateInterface $form_state) {
parent::validateForm($form, $form_state);
}
public function submitForm(array &$form,
FormStateInterface $form_state) {
// Display result.
foreach ($form_state->getValues() as $key => $value) {
drupal_set_message($key . ': ' . $value);
}
// Save to Database.
$conn = Database::getConnection();
$conn->insert('module_animalbook_data')->fields(
array(
'name' => $form_state->getValue('name'),
'type' => $form_state->getValue('type'),
)
)->execute();
}
}
$form['name'] = array(
'#type' => 'textfield',
'#title' => $this->t('Name'),
'#required' => TRUE,
);
$form['type1'] = array(
'#type' => 'select',
'#title' => 'タイプ',
'#options' => $typelist,
'#required' => TRUE,
);
$form['submit'] = [
'#type' => 'submit',
'#value' => $this->t('Save'),
];
return $form;
}
// Save to Database.
$conn = Database::getConnection();
$conn->insert('module_animalbook_data')->fields(
array(
'name' => $form_state->getValue('name'),
'type' => $form_state->getValue('type'),
)
)->execute();
nameとtypeのフィールドの値を
insert:挿入
- 17. フォーム画面を作る
〜vim src/Form/InputForm.php〜
• 完成図
vim src/Form/InputForm.php
<?php
namespace DrupalanimalbookForm;
use DrupalCoreFormFormBase;
use DrupalCoreFormFormStateInterface;
use DrupalCoreDatabaseDatabase;
class InputForm extends FormBase {
public function getFormId() {
return 'default_form';
}
public function buildForm(array $form,
FormStateInterface $form_state) {
$typelist = array(
$typelist = array(
'MAMMALS' => t('哺乳類'),
'BIRDS' => t('鳥類'),
'REPTILES' => t('爬虫類'),
'AMPHIBIANS' => t('両生類'),
'FISH' => t('魚類'),
);
public function validateForm(array &$form,
FormStateInterface $form_state) {
parent::validateForm($form, $form_state);
}
public function submitForm(array &$form,
FormStateInterface $form_state) {
// Display result.
foreach ($form_state->getValues() as $key => $value) {
drupal_set_message($key . ': ' . $value);
}
// Save to Database.
$conn = Database::getConnection();
$conn->insert('module_animalbook_data')->fields(
array(
'name' => $form_state->getValue('name'),
'type' => $form_state->getValue('type'),
)
)->execute();
}
}
$form['name'] = array(
'#type' => 'textfield',
'#title' => $this->t('Name'),
'#required' => TRUE,
);
$form['type1'] = array(
'#type' => 'select',
'#title' => 'タイプ',
'#options' => $typelist,
'#required' => TRUE,
);
$form['submit'] = [
'#type' => 'submit',
'#value' => $this->t('Save'),
];
return $form;
}
// Save to Database.
$conn = Database::getConnection();
$conn->insert('module_animalbook_data')->fields(
array(
'name' => $form_state->getValue('name'),
'type' => $form_state->getValue('type'),
)
)->execute();
実行
- 19. フォーム画面を作る
~DBのテーブル定義作成~
vim animalbook.install
<?php
function animalbook_schema() {
$schema['module_animalbook_data'] = [
'description' => 'Module Animalbook data.',
'fields' => array(
'id' => array(
'description' => 'id',
'type' => 'serial',
'not null' => TRUE,
),
'name' => array(
'description' => 'Character Name',
'type' => 'varchar',
'length' => 256,
'not null' => TRUE,
),
'type' => array(
'description' => 'Type',
'type' => 'varchar',
'length' => 256,
'not null' => TRUE,
),
),
'primary key' => array('id'),
];
return $schema;
}
DBのテーブル名
- 28. • 完成図
表の表示画面を作る
~表を表示するForm作成~
vim src/Form/ListForm.php
<?php
namespace DrupalanimalbookForm;
use DrupalCoreFormFormBase;
use DrupalCoreFormFormStateInterface;
use DrupalCoreDatabaseDatabase;
class ListForm extends FormBase {
function __construct() {
}
public function getFormId() {
return 'default_form';
}
// Initialize an empty array
$output = array();
// Next, loop through the $results array
foreach ($results as $result) {
$output[$result->id] = [
'id' => $result->id,
'name' => $result->name,
'type' => $result->type,
];
}
$form['table'] = [
'#type' => 'tableselect',
'#header' => $header,
'#options' => $output,
'#empty' => t('No users found'),
];
return $form;
}
public function buildForm(array $form,
FormStateInterface $form_state) {
//データベースからデータを取得する
$conn = Database::getConnection();
$query = $conn
->select('module_animalbook_data', 'x')
->fields('x', array('id', 'name', 'type'));
$data = $query->execute();
//条件に合致するものを配列にして返す
#$result = $data->fetchCol();
$results = $data->fetchAll();
//var_dump($results);
//ヘッダー作成
$header = [
'id' => t('ID'),
'name' => t('Name'),
'type' => t('Type'),
];
- 29. • 完成図
<?php
namespace DrupalanimalbookForm;
use DrupalCoreFormFormBase;
use DrupalCoreFormFormStateInterface;
use DrupalCoreDatabaseDatabase;
class ListForm extends FormBase {
function __construct() {
}
public function getFormId() {
return 'default_form';
}
// Initialize an empty array
$output = array();
// Next, loop through the $results array
foreach ($results as $result) {
$output[$result->id] = [
'id' => $result->id,
'name' => $result->name,
'type' => $result->type,
];
}
$form['table'] = [
'#type' => 'tableselect',
'#header' => $header,
'#options' => $output,
'#empty' => t('No users found'),
];
return $form;
}
public function buildForm(array $form,
FormStateInterface $form_state) {
//データベースからデータを取得する
$conn = Database::getConnection();
$query = $conn
->select('module_animalbook_data', 'x')
->fields('x', array('id', 'name', 'type'));
$data = $query->execute();
//条件に合致するものを配列にして返す
#$result = $data->fetchCol();
$results = $data->fetchAll();
//var_dump($results);
//ヘッダー作成
$header = [
'id' => t('ID'),
'name' => t('Name'),
'type' => t('Type'),
];
表の表示画面を作る
~表を表示するForm作成~
vim src/Form/ListForm.php
//データベースからデータを取得する
$conn = Database::getConnection();
$query = $conn
->select('module_animalbook_data', 'x')
->fields('x', array('id', 'name', 'type'));
$data = $query->execute();
- 30. • 完成図
<?php
namespace DrupalanimalbookForm;
use DrupalCoreFormFormBase;
use DrupalCoreFormFormStateInterface;
use DrupalCoreDatabaseDatabase;
class ListForm extends FormBase {
function __construct() {
}
public function getFormId() {
return 'default_form';
}
// Initialize an empty array
$output = array();
// Next, loop through the $results array
foreach ($results as $result) {
$output[$result->id] = [
'id' => $result->id,
'name' => $result->name,
'type' => $result->type,
];
}
$form['table'] = [
'#type' => 'tableselect',
'#header' => $header,
'#options' => $output,
'#empty' => t('No users found'),
];
return $form;
}
public function buildForm(array $form,
FormStateInterface $form_state) {
//データベースからデータを取得する
$conn = Database::getConnection();
$query = $conn
->select('module_animalbook_data', 'x')
->fields('x', array('id', 'name', 'type'));
$data = $query->execute();
//条件に合致するものを配列にして返す
#$result = $data->fetchCol();
$results = $data->fetchAll();
//var_dump($results);
//ヘッダー作成
$header = [
'id' => t('ID'),
'name' => t('Name'),
'type' => t('Type'),
];
表の表示画面を作る
~表を表示するForm作成~
vim src/Form/ListForm.php
//データベースからデータを取得する
$conn = Database::getConnection();
$query = $conn
->select('module_animalbook_data', 'x')
->fields('x', array('id', 'name', 'type'));
$data = $query->execute();
DBに接続
- 31. • 完成図
<?php
namespace DrupalanimalbookForm;
use DrupalCoreFormFormBase;
use DrupalCoreFormFormStateInterface;
use DrupalCoreDatabaseDatabase;
class ListForm extends FormBase {
function __construct() {
}
public function getFormId() {
return 'default_form';
}
// Initialize an empty array
$output = array();
// Next, loop through the $results array
foreach ($results as $result) {
$output[$result->id] = [
'id' => $result->id,
'name' => $result->name,
'type' => $result->type,
];
}
$form['table'] = [
'#type' => 'tableselect',
'#header' => $header,
'#options' => $output,
'#empty' => t('No users found'),
];
return $form;
}
public function buildForm(array $form,
FormStateInterface $form_state) {
//データベースからデータを取得する
$conn = Database::getConnection();
$query = $conn
->select('module_animalbook_data', 'x')
->fields('x', array('id', 'name', 'type'));
$data = $query->execute();
//条件に合致するものを配列にして返す
#$result = $data->fetchCol();
$results = $data->fetchAll();
//var_dump($results);
//ヘッダー作成
$header = [
'id' => t('ID'),
'name' => t('Name'),
'type' => t('Type'),
];
表の表示画面を作る
~表を表示するForm作成~
vim src/Form/ListForm.php
//データベースからデータを取得する
$conn = Database::getConnection();
$query = $conn
->select('module_animalbook_data', 'x')
->fields('x', array('id', 'name', 'type'));
$data = $query->execute();
SQLのクエリを作成
select:検索
- 32. • 完成図
<?php
namespace DrupalanimalbookForm;
use DrupalCoreFormFormBase;
use DrupalCoreFormFormStateInterface;
use DrupalCoreDatabaseDatabase;
class ListForm extends FormBase {
function __construct() {
}
public function getFormId() {
return 'default_form';
}
// Initialize an empty array
$output = array();
// Next, loop through the $results array
foreach ($results as $result) {
$output[$result->id] = [
'id' => $result->id,
'name' => $result->name,
'type' => $result->type,
];
}
$form['table'] = [
'#type' => 'tableselect',
'#header' => $header,
'#options' => $output,
'#empty' => t('No users found'),
];
return $form;
}
public function buildForm(array $form,
FormStateInterface $form_state) {
//データベースからデータを取得する
$conn = Database::getConnection();
$query = $conn
->select('module_animalbook_data', 'x')
->fields('x', array('id', 'name', 'type'));
$data = $query->execute();
//条件に合致するものを配列にして返す
#$result = $data->fetchCol();
$results = $data->fetchAll();
//var_dump($results);
//ヘッダー作成
$header = [
'id' => t('ID'),
'name' => t('Name'),
'type' => t('Type'),
];
表の表示画面を作る
~表を表示するForm作成~
vim src/Form/ListForm.php
//データベースからデータを取得する
$conn = Database::getConnection();
$query = $conn
->select('module_animalbook_data', 'x')
->fields('x', array('id', 'name', 'type'));
$data = $query->execute();
DBへの問い合わせを実行