zf2 の tableGateway を使用していますが、それがもたらす設計がわかりません。
zf2 の tableGateway を使用して挿入を行う方法の標準的な例を次に示します (これは docs からのものです)。
public function saveAlbum(Album $album)
{
$data = array(
'artist' => $album->artist,
'title' => $album->title,
);
$id = (int)$album->id;
if ($id == 0) {
$this->tableGateway->insert($data);
} else {
if ($this->getAlbum($id)) {
$this->tableGateway->update($data, array('id' => $id));
} else {
throw new \Exception('Form id does not exist');
}
}
}
しかし、次のような Album クラスが既にあるため、 $data 配列を定義するのは冗長に思えます。
class Album
{
public $id;
public $artist;
public $title;
public function exchangeArray($data)
{
$this->id = (isset($data['id'])) ? $data['id'] : null;
$this->artist = (isset($data['artist'])) ? $data['artist'] : null;
$this->title = (isset($data['title'])) ? $data['title'] : null;
}
}
私自身のプロジェクトでは、約 25 のプロパティ (25 列のテーブル) を持つモデルがあります。25 個のプロパティを持つクラスを定義し、それらのプロパティごとに要素を持つ tableGateway を実装するクラスのメソッド内に $data 配列を記述する必要があるのは冗長に思えます。何か不足していますか?