I am looking for a way to initial our application quickly with current database schema which is defined and seed data to the database in zendframework 2. This has been done quite well in the modern web framework like rails, play!, you name it but I don't know if it's existed in zf2. I don't find any official documentation about this :)
1 Answer
Use Phing for that task. Initializing (installing) your app, should not be done by the app itself but by an external agent like Phing.
Here is an example phing target of how you can install your database and populate it with initial data:
<target name="recreate-db">
<echo message="[Data] Drop and recreate database..." />
<pdosqlexec
url="mysql:host=${db.project.host};dbname=${db.project.dbname}"
userid="${db.project.username}"
password="${db.project.password}"
onerror="abort">
DROP DATABASE IF EXISTS `${db.project.dbname}`;
CREATE DATABASE `${db.project.dbname}`;
</pdosqlexec>
<echo message="[Data] Populate database with schema and start-data ..." />
<pdosqlexec
url="mysql:host=${db.project.host};dbname=${db.project.dbname}"
userid="${db.project.username}"
password="${db.project.password}"
onerror="abort">
<transaction src="${basePath}/db-versioning/schema.sql" />
<transaction src="${basePath}/db-versioning/start-data.sql" />
</pdosqlexec>
</target>