echo (int) ((date('Ymd') - 19710608) / 10000);
// 43
参考サイト(というかそのまま)生年月日から年齢を簡単に求める方法
新潟市で個人でコンピュータ相手にシステム開発をしています。確実、安く、素早くをモットーにやっています。
システム開発の時に調べた技術を惜しみなく公開していきます。あまりよろしくない業者についての苦言も為に吐きます。
コンピュータにベッタリというのもどうかと思いますが、やはりこれからは、コンピュータのことを知らないよりは知っていたほうが良いと思いますよ。
echo (int) ((date('Ymd') - 19710608) / 10000);
// 43
参考サイト(というかそのまま) $this->Table1->begin();
$this->Table1->save($data);
$this->Table1->commit();
$this->Table1->begin();
$this->Table1->save($data1);
$this->Table2->save($data2); // ←トランザクション対象外に見える
$this->Table1->commit();
class TransactionComponent extends Component {
/**
* トランザクション開始
*
* @param array $models トランザクション処理するモデルを全て指定する
*/
public function begin($models) {
$this->models = $models;
foreach ($models as $model) {
$model->begin();
}
}
/**
* ロールバック
*/
public function rollback() {
foreach ($this->models as $model) {
$model->rollback();
}
}
/**
* コミット
*/
public function commit() {
foreach ($this->models as $model) {
$model->commit();
}
}
}
AppModel.phpclass AppModel extends Component {
App::uses('Model', 'Model');
class AppModel extends Model {
/**
* トランザクション開始
*/
public function begin() {
$db = $this->getDataSource($this->useDbConfig);
$db->begin();
}
/*
* トランザクションコミット
*/
public function commit() {
$db = $this->getDataSource($this->useDbConfig);
$db->commit();
}
/*
* トランザクションロールバック
*/
public function rollback() {
$db = $this->getDataSource($this->useDbConfig);
$db->rollback();
}
}
HogeController.phpApp::uses('Model', 'Model');
class HogeController extends AppController {
public $uses = array("Table1", "Table2");
public $components = array("Transaction");
private function register($data) {
try {
$this->Transaction->begin(array("Table1", "Table2"));
if(!$this->Table1->save($data)) {
$this->rollback();
return false;
)
if(!$this->Table2->save($data)) {
$this->rollback();
return false;
)
$this->commit();
return true;
}catch(Exception $e) {
$this->rollback();
return false;
}
}
}
これは、CakePHPを使い始めてDBのトランザクションはどうやって扱ったら良いかというのを調査していたらあるサイト(今もあると思いますが忘れました)で一つの解として紹介されていたもものです。
実際の所、ルールなので、複数テーブルにトランザクション処理をするときは、代表のテーブルに対してbegin、commit、rollbackをするということにすればいちいちこんなめんどくさいことをしなくても良いのですけどね。