kikeda1104's blog

備忘録・技術に関することを書いています。(webエンジニア)

MySql 外部参照制約

こんにちは。kikedaです。

スキーマを作成して、外部参照制約をつける機会があったので、備忘録です。Googleで検索することはあるんですが、やはり自分のブログを検索して、作業する機会が多いので「他人」より「自分」に向けての、記事を多くなりそうです。

MySqlの外部参照制約

MySqlで外部キーを持つテーブルで、関係を記述することにより、外部キーの元となるマスタテーブルの値もしくは、テーブルそのもの削除と更新を防止することができます。挙動については、指定するパラメータにより細かく変えることができますが、今回の目的は、マスタテーブルのレコードを、外部キーが参照しているレコードが存在する間は、削除と更新を防止させます。 親のレコードが消されて、つながりが示されなくなるのを防ぎます。

外部参照制約をつける。

> alter table table_name add constraint 制約名 foreign key (外部キーの列名) references テーブル名(列名) on delete cascade on update cascade;

MySql Workbenchを利用することで、データベースやテーブルを定義をして、Sqlを作成することができますので、そちらから利用することもオススメです。

以上です。

codeigniterのヘルパー

こんにちは。RubyRailsが触りたいですが、日々を過ごしているkikedaです。

「あんな技術のないやつらに・。」とか空耳を聞くくらいなので、記事書いたら休みたいです。

今日は、codeigniterを触っていて、ヘルパーを覚える機会があったので、そちらを記述していきます。

viewで使われるヘルパー

form_input

まずは、input type="text"をcodeigniterのヘルパーで記述します。

<?php echo form_input('name',  set_value('name', $data ? $data->id : ''), 'class="wrapper"' ) ?>

set_valueは、formのaction先でvalidation_ruleをセットして、validation->runをを走らせた際に、codeigniterが保持してくれる値です。validation->runで値の検証がエラーになった際に、検証した値を名前を指定して保存することができます。もちろんリクエスト毎ですよ。

form_checkbox

<?php echo form_checkbox('name', '1', $value ? $value == '1' : '', 'class="chk"') ?>

ひとまず、2つだけご紹介でした。 ちなみに、公式のサイトに各種ヘルパーの利用サンプルと解説が載っていますので、詳細はそちらを参考にしてください。

以上。

codeigniter2.2 エラー対応(Only variable references should be returned by reference)

こんにちは。kikedaです。

codeigniter 2.2で開発したプロジェクトにブラウザからアクセスした際に、エラーが発生したので対応しました。備忘録です。

エラーメッセージ

Severity: Notice

Message: Only variable references should be returned by reference

Filename: core/Common.php

Line Number: 257

system/core/Common.php

# 変更前
return $_config[0] =& $config;

# 変更後
$_config[0] =& $config;
return $_config[0]; 

phpのバージョンに依存するのか。MAMPから起動してもらった際にも確認しましたが、 CentOSで確認した際にも同様に起こりましたね。

環境

  • Cent OS 6.5
  • PHP 5.3.3
  • mysql 5.1.73
  • CodeIgniter 2.2

参考URL

https://ellislab.com/forums/viewthread/244510/#1069047

codeigniter2.2の環境設定

こんにちは。rubyが好きですが、業務でPHPを書くことになり、まずは業務をこなせることを見せようと思っているkikeda1104です。このタイトルで書くのもあれですが、Rubyのコミニティに貢献できるようになりたいですねぇ。

で、codeigniter2.2というフレームワークをプロジェクトで利用していますが、フレームワークそのものにバグがあったり、日本語のドキュメントの更新がとまっていたり、仕様がややおかしいなと思えるフレームワークです。(あんまり好ましく思っていないです。)

後ほど説明しますが、学習コストが低いことが利点としてあげられていますが、学習コストが0になるわけではなく、それなりにはかかるので、プロジェクトの立ち上げ時にはフレームワークの知識の習得や慣れも見積もりに入れるべきだと思います。(参加プロジェクトでも、当初から伝えていますし、遅れていても、自分を責めるつもりはありません。(何)

とはいえ、プロジェクトに参加される方に迷惑をかけたくはないので、できる限り時間を投資していきたい。自社サービスの際にはチームになりますしね。まぁ、余談です。

それで本題です。

デザイナーの方に(HTML+CSSをコーディングしていただける方)に環境設定のお手伝いをする機会があったので、 備忘録として書いておきます。

codeigniterとは

PHPで書かれているwebアプリケーションフレームワークです。特徴としてあげているのが、学習コストが低い。つまり、構成やDSLが単純であることが売りのようです。2011年で日本語のサイトの更新が止まっていますが、当時世界的にもシェアが高かったようです。

公式ホームページ

  • 日本語公式ページ

トップ :: 日本CodeIgniterユーザ会

  • 日本語のユーザドキュメント

CodeIgniterへようこそ : CodeIgniter ユーザガイド 日本語版

日本語のドキュメントは古いので、海外の公式を参考にするようにしましょう。

CodeIgniter Web Framework

環境構築

環境構築は簡単で、codeigniterをダウンロードすることで利用できます。

http://www.codeigniter.com/downloadからダウンロードして解凍します。

$ wget https://github.com/bcit-ci/CodeIgniter/archive/2.2-stable.zip
$ unzip 2.2-stable.zip

ディレクトリ構成は以下のようですね。

$ CodeIgniter-2.2-stable
.
├── application
│   ├── cache
│   │   └── index.html
│   ├── config
│   │   ├── autoload.php
│   │   ├── config.php
│   │   ├── constants.php
│   │   ├── database.php
│   │   ├── doctypes.php
│   │   ├── foreign_chars.php
│   │   ├── hooks.php
│   │   ├── index.html
│   │   ├── migration.php
│   │   ├── mimes.php
│   │   ├── profiler.php
│   │   ├── routes.php
│   │   ├── smileys.php
│   │   └── user_agents.php
│   ├── controllers
│   │   ├── index.html
│   │   └── welcome.php
│   ├── core
│   │   └── index.html
│   ├── errors
│   │   ├── error_404.php
│   │   ├── error_db.php
│   │   ├── error_general.php
│   │   ├── error_php.php
│   │   └── index.html
│   ├── helpers
│   │   └── index.html
│   ├── hooks
│   │   └── index.html
│   ├── index.html
│   ├── language
│   │   ├── english
│   │   │   └── index.html
│   │   └── index.html
│   ├── libraries
│   │   └── index.html
│   ├── logs
│   │   └── index.html
│   ├── models
│   │   └── index.html
│   ├── third_party
│   │   └── index.html
│   └── views
│       ├── index.html
│       └── welcome_message.php
├── index.php
├── license.txt
├── system
│   ├── core
│   │   ├── Benchmark.php
│   │   ├── CodeIgniter.php
│   │   ├── Common.php
│   │   ├── Config.php
│   │   ├── Controller.php
│   │   ├── Exceptions.php
│   │   ├── Hooks.php
│   │   ├── index.html
│   │   ├── Input.php
│   │   ├── Lang.php
│   │   ├── Loader.php
│   │   ├── Model.php
│   │   ├── Output.php
│   │   ├── Router.php
│   │   ├── Security.php
│   │   ├── URI.php
│   │   └── Utf8.php
│   ├── database
│   │   ├── DB_active_rec.php
│   │   ├── DB_cache.php
│   │   ├── DB_driver.php
│   │   ├── DB_forge.php
│   │   ├── DB.php
│   │   ├── DB_result.php
│   │   ├── DB_utility.php
│   │   ├── drivers
│   │   │   ├── cubrid
│   │   │   │   ├── cubrid_driver.php
│   │   │   │   ├── cubrid_forge.php
│   │   │   │   ├── cubrid_result.php
│   │   │   │   ├── cubrid_utility.php
│   │   │   │   └── index.html
│   │   │   ├── index.html
│   │   │   ├── mssql
│   │   │   │   ├── index.html
│   │   │   │   ├── mssql_driver.php
│   │   │   │   ├── mssql_forge.php
│   │   │   │   ├── mssql_result.php
│   │   │   │   └── mssql_utility.php
│   │   │   ├── mysql
│   │   │   │   ├── index.html
│   │   │   │   ├── mysql_driver.php
│   │   │   │   ├── mysql_forge.php
│   │   │   │   ├── mysql_result.php
│   │   │   │   └── mysql_utility.php
│   │   │   ├── mysqli
│   │   │   │   ├── index.html
│   │   │   │   ├── mysqli_driver.php
│   │   │   │   ├── mysqli_forge.php
│   │   │   │   ├── mysqli_result.php
│   │   │   │   └── mysqli_utility.php
│   │   │   ├── oci8
│   │   │   │   ├── index.html
│   │   │   │   ├── oci8_driver.php
│   │   │   │   ├── oci8_forge.php
│   │   │   │   ├── oci8_result.php
│   │   │   │   └── oci8_utility.php
│   │   │   ├── odbc
│   │   │   │   ├── index.html
│   │   │   │   ├── odbc_driver.php
│   │   │   │   ├── odbc_forge.php
│   │   │   │   ├── odbc_result.php
│   │   │   │   └── odbc_utility.php
│   │   │   ├── pdo
│   │   │   │   ├── index.html
│   │   │   │   ├── pdo_driver.php
│   │   │   │   ├── pdo_forge.php
│   │   │   │   ├── pdo_result.php
│   │   │   │   └── pdo_utility.php
│   │   │   ├── postgre
│   │   │   │   ├── index.html
│   │   │   │   ├── postgre_driver.php
│   │   │   │   ├── postgre_forge.php
│   │   │   │   ├── postgre_result.php
│   │   │   │   └── postgre_utility.php
│   │   │   ├── sqlite
│   │   │   │   ├── index.html
│   │   │   │   ├── sqlite_driver.php
│   │   │   │   ├── sqlite_forge.php
│   │   │   │   ├── sqlite_result.php
│   │   │   │   └── sqlite_utility.php
│   │   │   └── sqlsrv
│   │   │       ├── index.html
│   │   │       ├── sqlsrv_driver.php
│   │   │       ├── sqlsrv_forge.php
│   │   │       ├── sqlsrv_result.php
│   │   │       └── sqlsrv_utility.php
│   │   └── index.html
│   ├── fonts
│   │   ├── index.html
│   │   └── texb.ttf
│   ├── helpers
│   │   ├── array_helper.php
│   │   ├── captcha_helper.php
│   │   ├── cookie_helper.php
│   │   ├── date_helper.php
│   │   ├── directory_helper.php
│   │   ├── download_helper.php
│   │   ├── email_helper.php
│   │   ├── file_helper.php
│   │   ├── form_helper.php
│   │   ├── html_helper.php
│   │   ├── index.html
│   │   ├── inflector_helper.php
│   │   ├── language_helper.php
│   │   ├── number_helper.php
│   │   ├── path_helper.php
│   │   ├── security_helper.php
│   │   ├── smiley_helper.php
│   │   ├── string_helper.php
│   │   ├── text_helper.php
│   │   ├── typography_helper.php
│   │   ├── url_helper.php
│   │   └── xml_helper.php
│   ├── index.html
│   ├── language
│   │   ├── english
│   │   │   ├── calendar_lang.php
│   │   │   ├── date_lang.php
│   │   │   ├── db_lang.php
│   │   │   ├── email_lang.php
│   │   │   ├── form_validation_lang.php
│   │   │   ├── ftp_lang.php
│   │   │   ├── imglib_lang.php
│   │   │   ├── index.html
│   │   │   ├── migration_lang.php
│   │   │   ├── number_lang.php
│   │   │   ├── profiler_lang.php
│   │   │   ├── unit_test_lang.php
│   │   │   └── upload_lang.php
│   │   └── index.html
│   └── libraries
│       ├── Cache
│       │   ├── Cache.php
│       │   ├── drivers
│       │   │   ├── Cache_apc.php
│       │   │   ├── Cache_dummy.php
│       │   │   ├── Cache_file.php
│       │   │   ├── Cache_memcached.php
│       │   │   └── index.html
│       │   └── index.html
│       ├── Calendar.php
│       ├── Cart.php
│       ├── Driver.php
│       ├── Email.php
│       ├── Encrypt.php
│       ├── Form_validation.php
│       ├── Ftp.php
│       ├── Image_lib.php
│       ├── index.html
│       ├── javascript
│       │   ├── index.html
│       │   └── Jquery.php
│       ├── Javascript.php
│       ├── Log.php
│       ├── Migration.php
│       ├── Pagination.php
│       ├── Parser.php
│       ├── Profiler.php
│       ├── Session.php
│       ├── Sha1.php
│       ├── Table.php
│       ├── Trackback.php
│       ├── Typography.php
│       ├── Unit_test.php
│       ├── Upload.php
│       ├── User_agent.php
│       ├── Xmlrpc.php
│       ├── Xmlrpcs.php
│       └── Zip.php
└── user_guide
    ├── changelog.html
    ├── database
    │   ├── active_record.html
    │   ├── caching.html
    │   ├── call_function.html
    │   ├── configuration.html
    │   ├── connecting.html
    │   ├── examples.html
    │   ├── fields.html
    │   ├── forge.html
    │   ├── helpers.html
    │   ├── index.html
    │   ├── queries.html
    │   ├── results.html
    │   ├── table_data.html
    │   ├── transactions.html
    │   └── utilities.html
    ├── doc_style
    │   ├── index.html
    │   └── template.html
    ├── general
    │   ├── alternative_php.html
    │   ├── ancillary_classes.html
    │   ├── autoloader.html
    │   ├── caching.html
    │   ├── cli.html
    │   ├── common_functions.html
    │   ├── controllers.html
    │   ├── core_classes.html
    │   ├── creating_drivers.html
    │   ├── creating_libraries.html
    │   ├── credits.html
    │   ├── drivers.html
    │   ├── environments.html
    │   ├── errors.html
    │   ├── helpers.html
    │   ├── hooks.html
    │   ├── libraries.html
    │   ├── managing_apps.html
    │   ├── models.html
    │   ├── profiling.html
    │   ├── quick_reference.html
    │   ├── requirements.html
    │   ├── reserved_names.html
    │   ├── routing.html
    │   ├── security.html
    │   ├── styleguide.html
    │   ├── urls.html
    │   └── views.html
    ├── helpers
    │   ├── array_helper.html
    │   ├── captcha_helper.html
    │   ├── cookie_helper.html
    │   ├── date_helper.html
    │   ├── directory_helper.html
    │   ├── download_helper.html
    │   ├── email_helper.html
    │   ├── file_helper.html
    │   ├── form_helper.html
    │   ├── html_helper.html
    │   ├── inflector_helper.html
    │   ├── language_helper.html
    │   ├── number_helper.html
    │   ├── path_helper.html
    │   ├── security_helper.html
    │   ├── smiley_helper.html
    │   ├── string_helper.html
    │   ├── text_helper.html
    │   ├── typography_helper.html
    │   ├── url_helper.html
    │   └── xml_helper.html
    ├── images
    │   ├── appflowchart.gif
    │   ├── arrow.gif
    │   ├── ci_logo_flame.jpg
    │   ├── ci_logo.jpg
    │   ├── ci_quick_ref.png
    │   ├── codeigniter_1.7.1_helper_reference.pdf
    │   ├── codeigniter_1.7.1_helper_reference.png
    │   ├── codeigniter_1.7.1_library_reference.pdf
    │   ├── codeigniter_1.7.1_library_reference.png
    │   ├── file.gif
    │   ├── folder.gif
    │   ├── nav_bg_darker.jpg
    │   ├── nav_separator_darker.jpg
    │   ├── nav_toggle_darker.jpg
    │   ├── reactor-bullet.png
    │   ├── smile.gif
    │   └── transparent.gif
    ├── index.html
    ├── installation
    │   ├── downloads.html
    │   ├── index.html
    │   ├── troubleshooting.html
    │   ├── upgrade_120.html
    │   ├── upgrade_130.html
    │   ├── upgrade_131.html
    │   ├── upgrade_132.html
    │   ├── upgrade_133.html
    │   ├── upgrade_140.html
    │   ├── upgrade_141.html
    │   ├── upgrade_150.html
    │   ├── upgrade_152.html
    │   ├── upgrade_153.html
    │   ├── upgrade_154.html
    │   ├── upgrade_160.html
    │   ├── upgrade_161.html
    │   ├── upgrade_162.html
    │   ├── upgrade_163.html
    │   ├── upgrade_170.html
    │   ├── upgrade_171.html
    │   ├── upgrade_172.html
    │   ├── upgrade_200.html
    │   ├── upgrade_201.html
    │   ├── upgrade_202.html
    │   ├── upgrade_203.html
    │   ├── upgrade_210.html
    │   ├── upgrade_211.html
    │   ├── upgrade_212.html
    │   ├── upgrade_213.html
    │   ├── upgrade_214.html
    │   ├── upgrade_220.html
    │   ├── upgrade_b11.html
    │   └── upgrading.html
    ├── libraries
    │   ├── benchmark.html
    │   ├── caching.html
    │   ├── calendar.html
    │   ├── cart.html
    │   ├── config.html
    │   ├── email.html
    │   ├── encryption.html
    │   ├── file_uploading.html
    │   ├── form_validation.html
    │   ├── ftp.html
    │   ├── image_lib.html
    │   ├── input.html
    │   ├── javascript.html
    │   ├── language.html
    │   ├── loader.html
    │   ├── migration.html
    │   ├── output.html
    │   ├── pagination.html
    │   ├── parser.html
    │   ├── security.html
    │   ├── sessions.html
    │   ├── table.html
    │   ├── trackback.html
    │   ├── typography.html
    │   ├── unit_testing.html
    │   ├── uri.html
    │   ├── user_agent.html
    │   ├── xmlrpc.html
    │   └── zip.html
    ├── license.html
    ├── nav
    │   ├── hacks.txt
    │   ├── moo.fx.js
    │   ├── nav.js
    │   ├── prototype.lite.js
    │   └── user_guide_menu.js
    ├── overview
    │   ├── appflow.html
    │   ├── at_a_glance.html
    │   ├── cheatsheets.html
    │   ├── features.html
    │   ├── getting_started.html
    │   ├── goals.html
    │   ├── index.html
    │   └── mvc.html
    ├── toc.html
    ├── tutorial
    │   ├── conclusion.html
    │   ├── create_news_items.html
    │   ├── hard_coded_pages.html
    │   ├── index.html
    │   ├── news_section.html
    │   └── static_pages.html
    └── userguide.css

48 directories, 355 files

config.php

$ vi config/config.php
# base_urlを入力する
$ config['base_url'] = 'http://localhost:3000';

apacheの設定

$ vi /etc/httpd/conf/httpd.conf

<VirtualHost *:80> 
    ServerAdmin gankai1104@gmail.com 
    DocumentRoot /var/www/test_code 
    ServerName test-code 
    ErrorLog logs/dummy-host.example.com-error_log 
    CustomLog logs/dummy-host.example.com-access_log common 
</VirtualHost>

シンボリックリンクの作成

$ cd /var/www
$ ln -s ~/Codeigniter/ test-code

以上です。

Vagrantで構築した仮想マシンにDBのdumpファイルを転送する

こんにちはkikeda1104です。
仮想マシン側からdumpファイルを作成して、ローカルにdumpファイルを転送、復元するようにします。

前提

DBからdumpファイルをダウンロード

$ vagrant ssh
$ cd
$ mysql_dump -u root -h 192.168.1.2 -p --port=3306 my_datadabase  >> ~/mydb.sql
$ gzip mydb.sql
=> mydb.sql.gz

vagrantで起動した仮想マシンからファイルをコピーする

$ vagrant ssh-config --host app_server >> ~/.ssh/config
$ scp vagrant@app_server:~/mydb.sql.gz ~/mydb.sql.gz
$ ungzip mydb.sql.gz

データベースの作成

$ mysql -u root -p
$ CREATE DATABASE my_database DEFAULT CHARACTER SET utf8;
$ exit

mysql_dumpファイルをローカルのmysqlに復元する

$ mysql -u root -p my_database << mydb.sql

以上です。

Vagrantのディレクトリを削除後にVagrantのprocessを削除した(Cent OS6.5)

掲題の件を調べる機会があったので備忘録として置きます。

Vagrantを起動

$ cd ~/vagrant_dir
$ vagrant up
$ rm -rf ~/vagrant_dir

Vagrantのゾンビプロセスを削除

$ vagrant global-status --prune

以上

日報(2014/10/31)

12:00 - 14:00

14:00 - 15:00

  • KVMが導入できるか調べる
    • x86でしか対応できないので、ひとまず見送る

16:00 - 18:00

  • サービスに画像アップロード用のgemと機能を追加
    • ついでに記事を書く

18:00 - 23:30

  • ローカルでNginxのインストールと設定
  • unicorn.rbの記述を修正
  • vagrantによる開発環境の構築しようかと検討

感想&課題

  • アウトプットは、常に意識しておきたい(コードの書く、記事を書く,つぶやくなど)
  • 書籍がたりない。DNSの設定とNginxの設定が、ネットで調べるだけだと体系的に頭に入れられないので辛い。
  • 借りた本も読み進めたいなぁ。

Nginxの設定

こんにちは。kikeda1104です。 前回の記事で、Nginxのディレクティブの一覧を作りました。それを参考にしながら公開する目的で、 Nginxの設定を進めていきたいと思います。

システムの前提

Nginx

server {
    listen 80 default deferred;
    client_max_body_size 4G;
    server_name example.com;
    keepalive_timeout 5;

    upstream app_server {
      server unix:/tmp/unicorn.sock fail_timeout=0;
    }

    location / {
      root /myapp/public;
    }

   error_page  404              /404.html;
   location = /404.html {
       root   /usr/share/nginx/html;
   }
}

参考:
unicorn/nginx.conf at master · defunkt/unicorn · GitHub

https://github.com/defunkt/unicorn/blob/master/examples/nginx.conf

nginx

以上。

rmagickのインストール(Cent OS 6.5)

こんにちは。kikeda1104です。 rmagickのインストールで、必要なライブラリが足りずにコンパイルエラーになっていたので、 調べて対応しました。

環境

  • Cent OS(6.5)

必要なlibのインストール

$ yum -y install libjpeg-devel libpng-devel
$ yum -y install ImageMagick ImageMagick-devel

rmagickのインストール

$ gem install rmagick

以上です。

Capybara-webkitの導入(Cent OS 6.5)

こんにちは。kikeda1104です。 スクレイピングで、JSがrenderするDOMを触る必要があったので、CapybaraとCapybara-webkitをインストールしました。

環境

  • Cent OS 6.5

まず、インストールする上でqtが必要になるので、これをインストールします。

Qtのダウンロード&インストール

最新版は、現在(2014/10/30)5.3系ですが、Capybara-webkitWikiを確認すると4.8を使っています。

$ cd ~/Downloads
$ wget http://download.qt-project.org/official_releases/qt/4.8/4.8.6/qt-everywhere-opensource-src-4.8.6.tar.gz 
$ tar xzvf qt-everywhere-opensource-src-4.8.6.tar.gz
$ cd qt-everywhere-opensource-src-4.8.6
$ ./configure
$ gmake
$ sudo gmake install
$ sudo ln -s /usr/local/Trolltech/Qt-4.8.6/bin/qmake /usr/bin/qmake

capybara-webkitをインストール

$ gem install capybara-webkit
Fetching: capybara-webkit-1.3.1.gem (100%)
Building native extensions.  This could take a while...
Successfully installed capybara-webkit-1.3.1
Parsing documentation for capybara-webkit-1.3.1
Installing ri documentation for capybara-webkit-1.3.1
Done installing documentation for capybara-webkit after 0 seconds
1 gem installed

参考

https://github.com/thoughtbot/capybara-webkit/wiki/Installing-Qt-and-compiling-capybara-webkit