Flextrine 0.7.3 released

The Flextrine releases are flying out hard and fast!  This release gives another batch of bug fixes, enhanced functionality in various areas, better error reporting and lots more API documentation.  Flash on the Beach is just around the corner, and I’m hoping that my presentation will bring a new batch of users into the Flextrine camp.

Pick it up at www.flextrine.com

SPOILER ALERT (only if you are coming to Flash on the Beach) – here is a sneak preview of my slides for the presentation http://prezi.com/6vrft-2h4sjy/flextrine/

Flextrine 0.7 released – now with ZendAMF!

After much soul searching I eventually decided to put in the time to convert Flextrine from using AMFPHP to ZendAMF.  There were a few reasons I decided to make this move, but eventually it boiled down to the fact that quite a few people had asked me to do so.  In retrospect it was certainly the right thing to do – ZendAMF has the green light from Adobe, its licence is LGPL instead of GPL and the cleaner code base means that the changes Flextrine requires can be implemented as overridden classes instead of having to modify the ZendAMF core.  This particularly is great news because it means that you can use Flextrine directly with the standard Zend framework installation, whereas previously you could only use the version of AMFPHP bundled with Flextrine.

Functionally speaking things remain exactly the same, and we continue to approach our stable release alongside Doctrine 2.

The new version of Flextrine can be downloaded from www.flextrine.com.  Note that if you are upgrading from a previous version of Flextrine you will need to regenerate your AS3 entities.

Flextrine Tutorial – CRUD in a simple Flex 4 address book: Creating the database schema

Introduction
Setting up the server
Creating the entities
Creating the database schema
Loading the entities
Creating new entities
Deleting entities
Updating entities
Conclusion

Once we have defined the entities in the entities directory of the Flextrine server component it is very simple to create the appropriate database schema.

Creating the database schema

Browse to http://localhost/flextrinemanager (or whatever URL you pointed to the manager directory).  You should see a screen like this:

Flextrine Manager 1

Enter the URL of the Flextrine server side component (http://localhost/contacts) in the FLEXTRINE PROJECT URL input and Click to refresh.  The Flextrine manager will examine your project and update the manager to show the entities and database.  Now click on Create schema to automatically generate the appropriate tables in the database.  The manager should now look like this:

Flextrine Manager 2

Under the hood Doctrine 2 has parsed the annotations in ContactGroup and Contact and has determined the appropriate tables and columns that will be required to persist the objects.  Although you will never need to edit the schema manually, the following SQL is what was actually executed when you clicked CREATE SCHEMA.  Notice that Doctrine enforces referential integrity through MySQL constraints so that, for example, you cannot delete a ContactGroup which has any Contacts referring to it.

   1: CREATE TABLE  `contact`.`contactgroup` (

   2:   `id` int(11) NOT NULL AUTO_INCREMENT,

   3:   `name` varchar(100) NOT NULL,

   4:   PRIMARY KEY (`id`)

   5: );

   6:

   7: CREATE TABLE  `contact`.`contact` (

   8:   `id` int(11) NOT NULL AUTO_INCREMENT,

   9:   `name` varchar(80) NOT NULL,

  10:   `telephoneNumber` varchar(50) DEFAULT NULL,

  11:   `birthday` date DEFAULT NULL,

  12:   `contactGroup_id` int(11) DEFAULT NULL,

  13:   PRIMARY KEY (`id`),

  14:   KEY `contactGroup_id` (`contactGroup_id`),

  15:   CONSTRAINT `contact_ibfk_1` FOREIGN KEY (`contactGroup_id`) REFERENCES `contactgroup` (`id`)

  16: );

Now that the schema has been created you can use UPDATE SCHEMA whenever you add/remove entities or attributes within entities to update the schema without losing any data that is already in the database.  This gives you a very efficient workflow as your entities expand over the lifetime of a project.

We now have everything we need in the Flextrine server side component and in the database.  Its time to start writing some Flex code!

Flextrine Tutorial – CRUD in a simple Flex 4 address book: Creating the entities

Introduction
Setting up the server
Creating the entities
Creating the database schema
Loading the entities
Creating new entities
Deleting entities
Updating entities
Conclusion

Now that Flextrine has been configured we need to create our entities.  Entities are the objects that we want to enable for reading and writing to the database, and they are defined in PHP in the entities directory of the Flextrine server side component.

The requirements for our simple address book are that we need to have groups containing contacts.  Unfortunately Group is a reserved word in SQL so we’ll call it ContactGroup.  ContactGroups have a name, and many Contacts, and Contacts have a name, telephone number, birthday and a contactGroup.  We are going to place all our entities in the vo package.  This gives us the class diagram below:

Flextrine entities

Create a folder called vo in the entities directory of the Flextrine server side component and create ContactGroup.php and Contact.php with the contents given below.  More information on writing entities can be found in the Doctrine documentation for Basic Mapping and Association Mapping.  The main thing to remember when creating entities is that:

  • All mapped attributes must be public

ContactGroup.php

<?php


namespace vo;


use DoctrineCommonCollectionsArrayCollection;


/**

 * @Entity

 */


class ContactGroup {


    /** @Id @Column(type="integer") @GeneratedValue(strategy="IDENTITY") */

    public $id;


    /** @Column(length=100, type="string") */

    public $name;


    /**

     * @OneToMany(targetEntity="Contact", mappedBy="contactGroup")

     */


    public $contacts;


    public function __construct() {

        $this->contacts = new ArrayCollection();

    }


}


?>

Contact.php

<?php

namespace vo;


use DoctrineCommonCollectionsArrayCollection;


/**

 * @Entity

 */


class Contact {


    /** @Id @Column(type="integer") @GeneratedValue(strategy="IDENTITY") */

    public $id;


    /** @Column(length=80, type="string") */

    public $name;


    /** @Column(length=50, type="string", nullable="true") */

    public $telephoneNumber;


    /** @Column(type="date", nullable="true") */

    public $birthday;


    /**

     * @ManyToOne(targetEntity="ContactGroup", inversedBy="contacts")

     */

    public $contactGroup;


    public function __construct() {


    }


}


?>

Now that we have created our entity definitions we need to create the matching database schema.

Flextrine Tutorial – CRUD in a simple Flex 4 address book: Introduction

Introduction
Setting up the server
Creating the entities
Creating the database schema
Loading the entities
Creating new entities
Deleting entities
Updating entities
Conclusion

UPDATED FOR FLEXTRINE 0.6.5!

After numerous requests I have finally sat myself down to write a basic tutorial for using Flextrine in a practical and simple Flex application.

What is Flextrine?

In a sentence, Flextrine is an open-source solution for reading and writing AS3 objects to and from a remote database.  Flextrine doesn’t really do anything that you couldn’t do by hand using PHP and AS3 code, but it removes most of the grunt work and standardises client-server database interactions.  All being well, database aware Flex applications coded using Flextrine are shorter, more readable and more maintainable.

Documentation for Flextrine is available in its Google Code wiki and its definitely worth having a skim through before beginning the tutorial.

What shall we build?

In this tutorial we are going to demonstrate some of Flextrine’s functionality by building a simple online contacts manager.  The application will allow us to create, edit and delete contacts, each of which simply have names and telephone numbers.  In order to demonstrate some of Flextrine’s associations we will also allow the user to put contacts into groups.

Note that the purpose of this tutorial is to demonstrate Flextrine rather than Flex, so we won’t be using any frameworks (apart from Flex itself) and where there is a choice between an elegant programming solution and just keeping it simple we’ll be going for the simpler option 🙂

Requirements

To run through this tutorial you will need the following:

  • Flex SDK 4 (free from Adobe)
  • A nice Flex IDE – I personally use the wonderful FlashDevelop, but Flash Builder 4 will do just as well
  • A web server running PHP 5.3+ and MySQL (WAMP for Windows and MAMP for OSX are good choices)
  • Download the complete source code for the tutorial and save yourself tedious typing!  This does not include the Flextrine AS3 code so you will need to add the source code or SWC to your classpath manually before compiling.

Lets begin by installing Flextrine and its dependencies.