PHPのフレームワークで人気だったZend Framework。
Zend Framework2あたりから日本語情報がめっきり少なくなって、利用者も減ってきました。
Laravel隆盛の昨今ですが、どうもLaravelは性に合わない…
Zend Framework3の勉強してたら、どうも後継はLaminas(ラミナス)というものになるとのこと。
このサイトはLaminasに関するメモ書きです。
viewに変数を送る
return new ViewModel([‘item’ => $item]);
zendframework1の時
$targetUrl = $this->_getParam(‘targetUrl’);
laminas
$targetUrl = $this->getRequest()->getQuery(‘referer’)
laminas dbの例(fetchRow的)
日本語記事が意外と見つからないサンプル
zendframeworkのZend_Db的なもの。
fetchRow的なもの。
select文(クエリーベタ書き)
use Laminas\Db\Adapter\Adapter;
$db = new Adapter([
'driver' => 'Pdo_Pgsql', //postgresを使う場合
'database' => 'データベース名',
'username' => 'ユーザー名',
'password' => 'ユーザーパスワード',
]);
$statement = $db->createStatement("SELECT * FROM テーブル名 WHERE tel = ? AND address = ?", [$tel, $address]);
$results = $statement->execute();
$row = $results->current();
$name = $row['name'];
Zend_Dbのように、もっとコード少なくて済む方法をご存じの方は教えてくださいm(__)m
Application/src/Module.php getConfig →
Application/config/module.config.php →
Application/src/Module.php getServiceonfig → Application/src/Module.php getControllerConfig →
Application/src/Controller/IndexController.php __construct
Laminasを使ってみる
データベースとモデル
データベース
Now that we have the Album module set up with controller action methods and view scripts, it is time to look at the model section of our application. Remember that the model is the part that deals with the application’s core purpose (the so-called “business rules”) and, in our case, deals with the database. We will make use of laminas-db’s Laminas\Db\TableGateway\TableGateway to find, insert, update, and delete rows from a database table.
We are going to use Sqlite, via PHP’s PDO driver. Create a text file data/schema.sql with the following contents:
CREATE TABLE album (id INTEGER PRIMARY KEY AUTOINCREMENT, artist varchar(100) NOT NULL, title varchar(100) NOT NULL);
INSERT INTO album (artist, title) VALUES ('The Military Wives', 'In My Dreams');
INSERT INTO album (artist, title) VALUES ('Adele', '21');
INSERT INTO album (artist, title) VALUES ('Bruce Springsteen', 'Wrecking Ball (Deluxe)');
INSERT INTO album (artist, title) VALUES ('Lana Del Rey', 'Born To Die');
INSERT INTO album (artist, title) VALUES ('Gotye', 'Making Mirrors');
(The test data chosen happens to be the Bestsellers on Amazon UK at the time of writing!)
Now create the database using the following:
module/Album/src/ModelにAlbumTable.phpを作る。
namespace Album\Model;
class Album
{
public $id;
public $artist;
public $title;
public function exchangeArray(array $data)
{
$this->id = !empty($data['id']) ? $data['id'] : null;
$this->artist = !empty($data['artist']) ? $data['artist'] : null;
$this->title = !empty($data['title']) ? $data['title'] : null;
}
}
Laminas\Db\Adapter\AdapterInterfaceサービスはlaminas-dbコンポーネントによって登録されます。以前、config/modules.config.phpに次のエントリが含まれていることに気づいたかもしれません。:
return [
'Laminas\Form',
'Laminas\Db',
'Laminas\Router',
'Laminas\Validator',
/* ... */
],
All Laminas components that provide laminas-servicemanager configuration are also exposed as modules themselves; the prompts as to where to register the components during our initial installation occurred to ensure that the above entries are created for you.
The end result is that we can already rely on having a factory for the Laminas\Db\Adapter\AdapterInterface service; now we need to provide configuration so it can create an adapter for us.
LaminasのModuleManagerは、各モジュールのmodule.config.phpファイルからすべてのconfigurationをマージしてからconfig/autoload/ (first *.global.php files, and then *.local.php files)にマージします。データベースconfiguration情報をバージョン管理システムにコミットすべきglobal.phpに追加します。必要に応じてlocal.php (バージョン管理システム外) にデータベースの資格情報を保存できます。 次のコードを使用してconfig/autoload/global.php(アルバムモジュール内ではなくプロジェクトルート内)を変更します。 :
return [
'db' => [
'driver' => 'Pdo',
'dsn' => sprintf('sqlite:%s/data/laminastutorial.db', realpath(getcwd())),
],
];
https://docs.laminas.dev/tutorials/getting-started/database-and-models/
laminasのmvcスケルトンをインストール
composer create-project -s dev laminas/laminas-mvc-skeleton path/to/install
tableGateway関連説明サイト その2
zendframework3だと思われるtableGatewayが記述されたチュートリアル(公式)
https://docs.zendframework.com/tutorials/getting-started/database-and-models/
と、それを翻訳?したサイト
http://rakulack.net/archives/50
tableGateway関連説明サイト
zendframework2だがzf3でも使えるtableGatewayを使ったzend-dbを使う方法?
http://blog.livedoor.jp/kazylla/archives/28530709.html