Skip to main content
  1. All Posts/

AkrabatZF

Tools PHP

Akrabat ZF library

See Akrabat_Db_Schema_Manager: Zend Framework database migrations for full details. This code is licensed under the New-BSD license.
Akrabat_Db_Schema_Manager is a tool for use with ZF1. If you need a standalone tool, then have a look at South for the Winter.

ZF1:

To get the Zend_Tool provider working:

  1. Place a copy of ZF1 in /usr/local/include/zf1/, so that the Zend folder is in zf1/library/Zend
  2. Update your ~/.bash_profile to set up an alias to the zf.sh script

     alias zf="export ZF_CONFIG_FILE=~/.zf.ini; /usr/local/include/zf1/bin/zf.sh"
    
    <p>
      Restart your terminal or <code>source ~/.bash_profile</code> </li> 
      
      <li>
        If you haven&#8217;t already done so, setup the storage directory and config file:</p> <pre class="notranslate"><code> zf --setup storage-directory
    

    zf –setup config-file

  3.   <li>
        Place a copy of the Akrabat Tools in <code>/usr/local/include/Akrabat</code>:</p> <pre class="notranslate"><code> cd /usr/local/include
    

    git clone http://github.com/akrabat/Akrabat.git

      <li>
        Edit the created <code>~/.zf.ini</code>. Change path so that it includes ZF1 and Akrabat/library, allow for auotoloading Akrabat and set up the provider:</p> <pre class="notranslate"><code> php.include_path = "/usr/local/include/zf1/library:/usr/local/include/Akrabat/library/"
    

    autoloadernamespaces.0 = “Akrabat_” basicloader.classes.0 = “Akrabat_Tool_DatabaseSchemaProvider”

      <li>
        <code>zf</code> should provide a help screen with the <code>DatabaseSchema</code> provider at the bottom.
      </li></ol> 
      
      <h3 dir="auto">
        <a rel="nofollow noopener" target="_blank" id="user-content-akrabat_db_schema_manager" class="anchor" aria-hidden="true" href="#akrabat_db_schema_manager"></a>Akrabat_Db_Schema_Manager
      </h3>
      
      <ol dir="auto">
        <li>
          Create scripts/migrations folder in your ZF application
        </li>
        <li>
          Create migration files within migrations with the file name format of nnn-Xxxx.php. e.g. 001-Users.php<br /> where:<br /> nnn => any number. The lower numbered files are executed first<br /> Xxx => any name. This is the class name within the file.<br /> In up(), database changes are applied, in down(), changes are reverted. If changes cannot be reverted, then down() should throw a Akrabat_Db_Schema_Exception.
        </li>
        <li>
          Create a class in your migrations file. Example for 001-Users.php:</p> <pre class="notranslate"><code> class Users extends Akrabat_Db_Schema_AbstractChange 
    

    { function up() { $tableName = $this->_tablePrefix . ‘users’; $sql = " CREATE TABLE IF NOT EXISTS $tableName ( id int(11) NOT NULL AUTO_INCREMENT, username varchar(50) NOT NULL, password varchar(75) NOT NULL, roles varchar(200) NOT NULL DEFAULT ‘user’, PRIMARY KEY (id) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;"; $this->_db->query($sql);

         $data = array();
         $data['username'] = 'admin';
         $data['password'] = sha1('password');
         $data['roles'] = 'user,admin';
         $this-&gt;_db-&gt;insert($tableName, $data);
     }
     
     function down()
     {
         $tableName = $this-&gt;_tablePrefix . 'users';
         $sql= "DROP TABLE IF EXISTS $tableName";
         $this-&gt;_db-&gt;query($sql);
     }
    

    }

        <li>
          If you want a table prefix, add this to your <code>application.ini</code>:</p> <pre class="notranslate"><code> resources.db.table_prefix = "prefix"
    

  <h2 dir="auto">
    <a rel="nofollow noopener" target="_blank" id="user-content-phing-task" class="anchor" aria-hidden="true" href="#phing-task"></a>Phing Task
  </h2>
  
  <ol dir="auto">
    <li>
      Define the phing task in the <code>build.xml</code></p> <pre class="notranslate"><code> &lt;taskdef name="dbmigration" classname="phing.tasks.PhingAkrabatDbSchemaManager" /&gt;

    <li>
      Setup a phing target with the database options in the <code>build.xml</code></p> <pre class="notranslate"><code> &lt;target name="database-migration"&gt;
&lt;dbmigration adapter="mysqli" host="${db.host}" dbname="${db.name}" username="${db.user}" password="${db.pass}" /&gt;

</target>

    <li>
      Run phing using the command line, by <code>phing database-migration</code>
    </li>
  </ol>