Location: PHPKode > scripts > MySQL Ajax Database Access Layer - MADAL > ClassGenerator.php
<?php
require_once 'Constants.php';
require_once ('ClassGeneratorJavascript.php');
require_once 'Config.php';

/**
 * Class for generating the code for the MySQL classes
 *
 */
class ClassGenerator {

	/**
	 * The name of the class to generate
	 *
	 * @var string
	 */
	public $class_name;

	/**
	 * The table to draw this class from
	 *
	 * @var string
	 */
	protected $table_name;

	/**
	 * The table from which to generate a class
	 *
	 * @var array
	 */
	protected $fields;

	/**
	 * A list of tables for which my primary key is their foreign key
	 *
	 * @var array
	 */
	protected $foreign_tables;

	/**
	 * The name of the database this class accesses
	 *
	 * @var string
	 */
	protected $database;

	/**
	 * Constructs the class using the given table
	 *
	 * @param string $database
	 * @param string $table_name The name of the class to generate
	 * @param array $fields The table structure from which to generate a class
	 * @param array $foreign_tables The set of tables for which this class's primary key is a foreign key
	 */
	function __construct($database, $table_name, $fields, $foreign_tables) {
		$this->database = $database;
		$this->class_name = $this->formatClassName ( $table_name );
		$this->table_name = $table_name;
		$this->fields = $fields;
		$this->foreign_tables = $foreign_tables;
	}

	/**
	 * Formats the table_name into CamelCase as TableName and removes s's/ ies's
	 * @param string $table_name
	 * @return string The formatted class name
	 */
	protected function formatClassName($table_name) {
		//replace underscores with spaces
		$className = str_ireplace ( "_", " ", $table_name );
		//upper case the first letter of each word
		$className = ucwords ( $className );
		//get rid of the spaces
		$className = str_ireplace ( " ", "", $className );
		if (EndsWith ( "ies", $className )) {
			//replace the ies with y
			$className = substr ( $className, 0, strlen ( $className ) - 3 );
			$className .= "y";
		} //				else //			//replace the ies with y
		//			$className = str_ireplace ( "Lists", "", $className );
		//		}
		if (EndsWith ( "s", $className )) {
			//remove the s
			$className = substr ( $className, 0, strlen ( $className ) - 1 );
		}
		return $className;
	}

	/**
	 * Generates and returns the code for the class
	 *
	 * @return string The code for the PHP class
	 */
	public function generateClassCode() {
		$requirements = "//foreign key class requirements
";
		if (isset ( $this->foreign_tables['pointToMe'] )) {
			foreach ( $this->foreign_tables['pointToMe'] as $foreign_table ) {
				$className = $this->formatClassName ( $foreign_table );
				$requirements .= "
//check to make sure $className has not already been included
if(!class_exists('$className')){
	//check to make sure $className.php has been generated
	if(file_exists(dirname(__FILE__).'/$className.php')){
		//require $className
		require_once dirname(__FILE__).'/$className.php';
	}
}
";
			}
		}
		if(count($this->foreign_tables) > 1){
			$requirements .= "//Single foreign table requirements";
		}
		if (isset ( $this->foreign_tables )) {
			foreach ( $this->foreign_tables as $field => $foreign_table ) {
				if($field != 'pointToMe'){
					$className = $this->formatClassName ( $foreign_table );
					$requirements .= "
//check to make sure $className has not already been included
if(!class_exists('$className')){
	//check to make sure $className.php has been generated
	if(file_exists(dirname(__FILE__).'/$className.php')){
		//require $className
		require_once dirname(__FILE__).'/$className.php';
	}
}
";
				}
			}
		}

		$public_fields = "";
		$insert_new_comments = "";
		$insert_new_field_names = "";

		//loop through each of the fields
		foreach ( $this->fields as $field_name => $field_properties ) {
			$field_properties [FIELD_NAME_FIELD] = $field_name;

			//get the php data type
			$field_properties [DATA_TYPE_FIELD] = ConvertMySQLTypeNameToPHPTypeName ( $field_properties [DATA_TYPE_FIELD] );

			//check if this is the primary field
			$is_primary_field = false;
			if ($field_properties [INDEX_FIELD] == PRIMARY_FIELD) {
				$primary_key_field = $field_name;
				$primary_key_type = $field_properties [DATA_TYPE_FIELD];
				$is_primary_field = true;
			}
			else{
				$insert_new_comments .= "* @param ".$field_properties [DATA_TYPE_FIELD]." $field_name
";
				$insert_new_field_names .= "\$$field_name, ";
			}
			//create the code for this field
			$public_fields .= $this->generateFieldCode ( $field_properties, $is_primary_field );

		}
		$constructor = "";
		$foreign_class_getters = "";
		if (isset ( $primary_key_field )) {
			$constructor = $this->generateConstructor ( $primary_key_field, $primary_key_type );

			$foreign_class_getters = $this->generateForeignClassGetters ( $primary_key_field );
		}

		$insert_new_field_names = substr ( $insert_new_field_names, 0, - 2 );
		$insert_new_field_names_no_cash = str_ireplace("$", "", $insert_new_field_names);
		$globalInsertFunction = "
/**
* Inserts a new $this->class_name
$insert_new_comments*/
function insertNew$this->class_name($insert_new_field_names){
	\$sql = \"INSERT INTO $this->table_name ($insert_new_field_names_no_cash) VALUES ($insert_new_field_names);\";
	mysql_query ( \$sql, \$this->connection ) or die(mysql_error());
}
";

		$header = "<?php
/**
* Generated by Brent Rossen's MySQL Ajax Database Access Layer
*/
require_once dirname(__FILE__).'/../Connection.php';
		$requirements
if(!class_exists('DBElement')){
	require_once dirname(__FILE__).'/Includes/DBElement.php';
}
if(!class_exists('FieldProperties')){
	require_once dirname(__FILE__).'/../Controller/Includes/FieldProperties.php';
}
if(!function_exists('getClassProperties')){
	require_once dirname(__FILE__).'/../Controller/Functions.php';
}

$globalInsertFunction

/**
* Class of type $this->class_name
*/
class $this->class_name extends DBElement {
";

		$staticClassGetter = "/**
	* Gets an array of all of the $this->class_name from the database
	* @param string \$OrderBy Field(s) to order by. If multiple fields, separate by a comma.
	* @param string \$direction ASC (ascending) or DESC (descending)
	* @return array of $this->class_name
	*/
	public static function get" . $this->class_name . "s(\$OrderBy = NULL, \$direction = 'DESC'){
		global \$" . $this->database . "_connection;
		\$" . $this->class_name . "s = array();
		if(\$OrderBy != NULL){
			\$sql = \"SELECT * FROM $this->table_name ORDER BY \$OrderBy \$direction\";
		}
		else{
			\$sql = \"SELECT * FROM $this->table_name\";
		}
		
		\$result = mysql_query(\$sql, \$" . $this->database . "_connection);
		while(\$row = mysql_fetch_array(\$result)){
			array_push(\$" . $this->class_name . "s, $this->class_name::constructFrom_" . $this->table_name . "_row(\$row));
		}
		return \$" . $this->class_name . "s;
	}
";
		$staticJavascriptObjectsInstantiator = "
	/**
	* Instantiates the javascript objects by
	* printing out javascript that generates
	* a javascript object for each of the given PHP objects.
	* You must call this before you can use the print_delete_button
	*
	* @param array " . $this->class_name . "s The array of " . $this->class_name . " objects to instantiate as Javascript objects
	*/
	public static function instantiate_javascript_objects($" . $this->class_name . "s){
		?><script type=\"text/javascript\">
		<?php
		foreach ( $" . $this->class_name . "s as $" . $this->class_name . " ) {
			?>
		var " . $this->class_name . "<?php echo $" . $this->class_name . "->primary_key_value; ?> = new " . $this->class_name . "(<?php
			echo json_encode ( $" . $this->class_name . " );
			?>);
		<?php
		}
		?>
		</script><?php
	}
";

		$dynamicJavascriptObjectInstantiator = "
	/**
	* Instantiates this PHP object as a javascript object by
	* printing out javascript that generates
	* a javascript object for this PHP object.
	* You must intantiate the javascript object before you can use the print_delete_button
	*
	*/
	public function instantiate_javascript_object(){
		?><script type=\"text/javascript\">
		var " . $this->class_name . "<?php echo \$this->primary_key_value; ?> = new " . $this->class_name . "(<?php
			echo json_encode ( \$this );
			?>);
		</script><?php
	}
";
		$print_delete_button = "
/**
* Prints a delete button for this object.
* Requires that you have instantiated the javascript for this object.
* @param bool \$require_confirmation Should we ask the user to confirm their choice of deleting an object? Prevents accidental button presses.
* @param string \$on_complete The function to call when the delete is completed
*/
public function print_delete_button(\$require_confirmation, \$on_complete){
?>
<button class='deleteButton ".$this->class_name."DeleteButton' title=\"Delete this $this->class_name\" onclick=\"
		<?php
		if(\$require_confirmation){
		?>	
		var answer = confirm('Are you sure you want to delete this $this->class_name?');
		if (!answer) {
			return false;
		}
		<?php } ?>
		$this->class_name<?php echo \$this->primary_key_value; ?>.deleteMeFromDatabase(<?php echo \$on_complete ?>);
		return false; //prevents full page refresh on error
		\">Delete</button>
<?php
}
	";

		$staticClassDropdown = "
	/**
	* Takes an array of $this->class_name and constructs a dropdown list
	* Calls the javascript callback function
	* @param Array of $this->class_name \$" . $this->class_name . "s The list of " . $this->class_name . "s to display in the dropdown
	* @param string \$display_field The field of the table to display
	* @param string \$selected_value The value of the currently selected option in the dropdown
	* @param string callback The javascript function to call on change
	*/
	public static function print_" . $this->class_name . "_dropdown(\$" . $this->class_name . "s, \$display_field, \$selected_value, \$javascript_callback){
		?>
		<select onchange='<?php echo \$javascript_callback ?>(this.value)' class='" . $this->class_name . "_dropdown' title='Select a $this->class_name'>
		<option>Select $this->class_name</option>
		<?php
		/* @var \$this->class_name $this->class_name */ 
		foreach (\$" . $this->class_name . "s as \$$this->class_name){
			?><option value='<?php echo \$" . $this->class_name . "->primary_key_value; ?>' <?php if(\$selected_value == \$" . $this->class_name . "->primary_key_value){ ?>selected='selected'<?php } ?>><?php echo \$" . $this->class_name . "->\$display_field; ?></option><?php 
		}
		?>
		</select>
		<?php
	}
";

		$staticClassAdder = "
	/**
	* Generates an interface to add to the table $this->class_name
	* There should only be one of these used per class type per page
	* Calls the javascript callback function
	* @param string callback The javascript function to call on insert, returns the id of the new object
	*/
	public static function print_" . $this->class_name . "_add(\$javascript_callback){
		
		\$class_properties = getClassProperties('$this->class_name');
		\$class_prop_array = explode(', ', \$class_properties);
		?>
<table>
	<tr>				
	<?php		
		foreach(\$class_prop_array as \$prop){
			if(\$prop != \"\"){
			?><th><?php echo formatFieldNameToTitle(\$prop); ?></th><?php
			}
		} ?>
		</tr>
		<tr><?php	
		\$var_retriever_code = '';
		foreach(\$class_prop_array as \$prop){
			\$var_retriever_code .= \"$('#\$prop').val(),\";
			if(\$prop != \"\"){
				?><td><input type='text' class='" . $this->class_name . "_add_input_<?php echo \$prop ?>' id='<?php echo \$prop ?>' title='Enter value for the <?php echo \$prop ?> here' /></td><?php 
			}
		} ?> 
		<td>
		<input type='button' class='" . $this->class_name . "_add_button' title='Add this $this->class_name' value='Add New $this->class_name' onclick=\"insertNew$this->class_name(<?php echo \$var_retriever_code ?><?php echo \$javascript_callback ?>);
		return false;
		\" />
		</td>
	</tr>
</table>

	
		<?php
	}
";

		$footer = "
}
?>";
		return $header . $public_fields . $constructor . $staticClassGetter . $staticClassAdder . $staticClassDropdown . $staticJavascriptObjectsInstantiator . $dynamicJavascriptObjectInstantiator . $print_delete_button . $foreign_class_getters . $footer;
	}

	/**
	 * Takes the foreign_keys array and generates foreign class getters using that array
	 */
	protected function generateForeignClassGetters($primary_key_field) {
		$foreignClassGettersCode = "";
		$subObjectGettersCode = "";
		foreach ( $this->foreign_tables['pointToMe'] as $foreign_table ) {
			$className = $this->formatClassName ( $foreign_table );
			$subObjectGettersCode .= "
		foreach(\$this->get" . $className . "s() as \$$className){
			\$" . $className . "->instantiateSubObjects();
		}";
			$foreignClassGettersCode .= "
	/**
	* A private array of " . $className . "s for caching values retreived from the db
	* This array should not be accessed or edited directly. It is protected for serialization purposes
	* @var array
	*/
	public \$" . $className . "s = array();
	/**
	* @param string \$OrderBy Field(s) to order by. If multiple fields, separate by a comma.
	* @param string \$direction ASC (ascending) or DESC (descending)
	* @return array An array of " . $className . "s associated with this $this->class_name
	*/
	public function get" . $className . "s(\$OrderBy = NULL, \$direction = 'DESC'){
		//Check if " . $className . "s have been instantiated
		if(count(\$this->" . $className . "s) == 0){
			//retrieve all the " . $className . "s from the database
			if(\$OrderBy != NULL){
				\$sql = \"SELECT * FROM " . $foreign_table . " WHERE $primary_key_field='\$this->primary_key_value' ORDER BY \$OrderBy \$direction;\";
			}
			else{
				\$sql = \"SELECT * FROM " . $foreign_table . " WHERE $primary_key_field='\$this->primary_key_value';\";
			}
			
			\$result = mysql_query ( \$sql,  \$this->connection);

			while(\$row = mysql_fetch_array ( \$result )){
				array_push(\$this->" . $className . "s, $className::constructFrom_" . $foreign_table . "_row(\$row));
			}
		}
		return \$this->" . $className . "s;
	}

	/**
	* A fully instantiated private array of " . $className . "s for caching values retreived from the db
	* This array should not be accessed or edited directly. It is protected for serialization purposes
	* @var array
	*/
	public \$fullyInstantiated" . $className . "s = array();
	/**
	* @param string \$OrderBy Field(s) to order by. If multiple fields, separate by a comma.
	* @param string \$direction ASC (ascending) or DESC (descending)
	* @return array An array of fully instantiated " . $className . "s associated with this $this->class_name, fully instantiated means that all linked subobjects will also be retrieved
	*/
	public function getFullyInstantiated" . $className . "s(\$OrderBy = NULL, \$direction = 'DESC'){
		//Check if " . $className . "s have been instantiated
		if(count(\$this->fullyInstantiated" . $className . "s) == 0){
			//retrieve all the " . $className . "s from the database
			if(\$OrderBy != NULL){
				\$sql = \"SELECT * FROM " . $foreign_table . " WHERE $primary_key_field='\$this->primary_key_value' ORDER BY \$OrderBy \$direction;\";
			}
			else{
				\$sql = \"SELECT * FROM " . $foreign_table . " WHERE $primary_key_field='\$this->primary_key_value';\";
			}
			\$result = mysql_query ( \$sql,  \$this->connection);

			while(\$row = mysql_fetch_array ( \$result )){
				\$obj = $className::constructFrom_" . $foreign_table . "_row(\$row);
				\$obj->instantiateSubObjects();
				
				array_push(\$this->fullyInstantiated" . $className . "s, \$obj);
			}
		}
		return \$this->fullyInstantiated" . $className . "s;
	}
	
	/**
	* Inserts a new $className into this element as a foreign key object
	*
	* @uses To use insertNew$className: Instantiate an object of $className. Pass it the appropriate values using the PUBLIC FIELDS directly. 
	* DO NOT use the setters. Setters will attempt to access the database for an object that does not exist. 
	* Then pass the instantiated object to insertNew$className. The object is passed by reference, the primary key id will be set
	* by insertNew$className. Once you've inserted, you can use your instantiated object to access the database.
	*
	* @param $className \$new$className
	* @return int The new id of the inserted object
	*/
	public function insertNew$className(&\$new$className){
		if(\$new$className == NULL)
		{
			\$new$className = new $className();
		}
		return \$this->insert(\$new$className); //return the id of the new row
	}
	
	/**
	* Gets the parameters for the insertNew$className constructor
	*/
	public function get_insertNew" . $className . "_parameters()
	{
		return '$className';
	}
		
	/**
	* Instantiates and inserts a new $className into this element as a foreign key object
	*
	* @uses Pass the parameters of the new class to the function. For parameters you don't wish to instantiate, set them to NULL.
	* Don't be fooled by seeing no arguments for the function. This function uses a dynamic arguments list, 
	* it will read out whatever arguments you pass to it. 
	*
	* @return int The new id of the inserted object
	*/
	public function generateNew$className(){
		\$arg_list = func_get_args();
		\$arg_names = explode(',', \$this->get_generateNew" . $className . "_parameters());
		\$new$className = new $className();
		foreach(\$arg_list as \$key=>\$val){
			\$new$className->\$arg_names[\$key] = \$val;
		}

		return \$this->insert(\$new$className); //return the id of the new row
	}
	
	/**
	* Gets the parameters for the generateNew$className constructor
	*/
	public function get_generateNew" . $className . "_parameters()
	{
			//array of vars to skip
			\$skipArray = array(\"updated\");//updated is automatically inserted
			
			//don't want the DBElement vars either
			foreach (get_class_vars(\"DBElement\") as \$key => \$var)
			{
				array_push(\$skipArray, \$key);
			}
			//get the class' vars
			\$classVars = get_class_vars('$className');
			\$varsList = '';
			foreach(\$classVars as \$key => \$var)
			{
				//if it's not an arrary, and it's not in the skip array
				//and it's not the primary key field for the class
				//and it's not the primary key field for this class
				if(!is_array(\$var) && !in_array(\$key, \$skipArray) && \$key != \$classVars['primary_key_field'] && \$key != \$this->primary_key_field){
					\$varsList .= \$key . \", \";
				}
			}
			\$varsList = substr(\$varsList,0,-2);
		return \$varsList;
	}
";
		}
		if (isset ( $primary_key_field )) {
			$fullyInstantiatedConstructor = "
	/**
	* Constructs a fully instantiated " . $this->class_name . ". This means it cascades construction to all of it's sub objects
	* Warning: Instantiating classes this way can be very slow. It requires cascading sql calls.
	* @return a pointer to the class
	*/
	public static function constructFullyInstantiatedFrom_$primary_key_field(\$$primary_key_field) {
		\$me = $this->class_name::constructFrom_$primary_key_field(\$$primary_key_field);
		//call each of the getters
		\$me->instantiateSubObjects();
		return \$me;
	}
		
	/**
	* Gets the parameters for the constructFullyInstantiatedFrom_" . $primary_key_field . " constructor
	*/
	public function get_constructFullyInstantiatedFrom_" . $primary_key_field . "_parameters(){
		return '$primary_key_field';
	}
	
	/**
	* Instantiates all of this class's sub objects
	*/
	public function instantiateSubObjects(){
	$subObjectGettersCode
	}
";
		}

		//generates the single object foreign key getters
		foreach ( $this->foreign_tables as $field => $foreign_table ) {
			if($field != "pointToMe" && $foreign_table != ""){
				$className = $this->formatClassName ( $foreign_table );
				$foreignClassGettersCode .= "
	/**
	* A private " . $className . " for caching the object retreived from the db
	* This object should not be accessed or edited directly. It is protected for serialization purposes
	*/
	private \$" . $className . ";
				
	/**
	* @return $className A " . $className . " associated with this $this->class_name
	*/
	public function get" . $className . "(){
		//Check if " . $className . " has been instantiated
		if(!isset(\$this->" . $className . ")){
			//retrieve the " . $className . " from the database
			\$sql = \"SELECT * FROM " . $foreign_table . " WHERE $field='\$this->$field';\";
			\$result = mysql_query ( \$sql,  \$this->connection);
			if(mysql_num_rows(\$result) > 0){
				\$row = mysql_fetch_array ( \$result );
				\$this->" . $className . " = $className::constructFrom_" . $foreign_table . "_row(\$row);
			}
		}
		return \$this->" . $className . ";
	}";
			}

		}

		return $foreignClassGettersCode . $fullyInstantiatedConstructor;
	}

	/**
	 * Generates the constructor code
	 *
	 * @param string $primary_key_field
	 * @return string The constructor code
	 */
	protected function generateConstructor($primary_key_field, $primary_key_type) {
		$constructor = "

	public \$table_name = '$this->table_name';
	public \$primary_key_field = '$primary_key_field';

	/**
	 * Basic constructor for serialization
	 */
	public function __construct(){
		global \$" . $this->database . "_connection;
		\$this->className = '" . $this->class_name . "';
		\$this->connection = \$" . $this->database . "_connection;
	}
	
	/**
	* Constructs a $this->class_name using the given row $primary_key_field
	* @param " . $this->table_name . "_row \$" . $this->table_name . "_row A row of " . $this->table_name . "
	*/
	public function constructFrom_" . $this->table_name . "_row(\$" . $this->table_name . "_row) {
		\$me = new $this->class_name();
		\$me->initializeVariables(\$" . $this->table_name . "_row);
		return \$me;
	}
			
	/**
	* Gets the parameters for the constructFrom_" . $this->table_name . "_row constructor
	*/
	public function get_constructFrom_" . $this->table_name . "_row_parameters(){
		return '" . $this->table_name . "_row';
	}
	";
		if (isset ( $primary_key_field )) {
			$constructor .= "
	/**
	* Constructs a $this->class_name using the primary key field $primary_key_field
	* @param $primary_key_type $primary_key_field The primary key field
	*/
	public static function constructFrom_$primary_key_field(\$$primary_key_field){	
		global \$" . $this->database . "_connection;
		\$$primary_key_field = mysql_real_escape_string(\$$primary_key_field);
		\$sql = \"SELECT * FROM " . $this->table_name . " WHERE $primary_key_field='\$$primary_key_field';\";
		\$result = mysql_query ( \$sql, \$" . $this->database . "_connection);
		\$row = mysql_fetch_array ( \$result );
		if(mysql_num_rows(\$result) != 1){
			throw new Exception(\"Invalid $primary_key_field = \$$primary_key_field parameter passed to $this->class_name constructFrom_$primary_key_field(\$$primary_key_field) constructor\");
		}
		return $this->class_name::constructFrom_" . $this->table_name . "_row(\$row);
	}
		
	/**
	* Gets the parameters for the constructFrom_" . $primary_key_field . " constructor
	*/
	public function get_constructFrom_" . $primary_key_field . "_parameters(){
		return '$primary_key_field';
	}";
		}
		$constructor .= "
	/**
	* Initializes the variables using " . $this->table_name . "_row
	* @param " . $this->table_name . "_row \$row A row of " . $this->table_name . "
	*/
	protected function initializeVariables(\$row){
		//loop through each of the fields assigning their values
		foreach(\$row as \$field_name=>\$field_value){
			if (!is_int( \$field_name )) {//make sure we don't get the int keys from the row		
				\$this->\$field_name = \$field_value;
				if(\$field_name == \$this->primary_key_field){
					\$this->primary_key_value = \$this->\$field_name;
				}
			}
		}
	}
	";

		return $constructor;
	}

	/**
	 * @param string $field_type
	 * @param string $field_name
	 * @param string $parameter_value
	 * @return string the code for the parameter
	 */
	protected function generateFieldCode($field_properties, $is_primary_field) {
		$field_type = $field_properties [DATA_TYPE_FIELD];
		$comments = $field_properties [COMMENTS_FIELD];
		$field_name = $field_properties [FIELD_NAME_FIELD];

		$field_declaration = "
	
	/**
	* $field_name, $comments
	* Note, this value is public only so that it can be serialized. 
	* If you retrieved this object from the database, you should not get this value directly or assign to it directly.
	* Instead, use get_$field_name and set_$field_name.
	* The only time this field should be assigned directly is for instantiation to pass to an insertNew$this->class_name.
	* @var $field_type \$$field_name
	*/
	public \$$field_name;";
		$getter = "
	/**
	 * Gets $field_name, $comments
	 * @return $field_type
	 */
	public function get_$field_name(){
		return \$this->$field_name;
	}
	
	/**
	* Gets the properties of $field_name
	*/
	public function get_" . $field_name . "_properties(){
		return new FieldProperties(" . var_export ( $field_properties, true ) . ");
	}
	
	";
		if (! $is_primary_field) {
			$setter = "
	/**
	 * Sets $field_name, $comments
	 * This method updates the database	using optimistic concurrency: If the value of the field has changed since the $this->class_name's $field_name was retrieved from the database, the update will fail.
	 * @param $field_type \$previous_$field_name The old value for $field_name
	 * @param $field_type \$new_$field_name The new value for $field_name 
	 * @return boolean Update success or failure
	 */
	public function set_$field_name(\$previous_$field_name, \$new_$field_name){
		//check to see if the previous value is equal to the current value, then we know $field_name has not been altered since this object's instantiation
		//second condition makes it so that a passed in null value is equal to a blank
		if(\$this->$field_name == \$previous_$field_name || (\$previous_$field_name == 'null' && \$this->$field_name == '')){
			//they are the same, update
			\$this->$field_name = \$new_$field_name;
			\$this->update('$field_name',\$new_$field_name);
			return TRUE;
		}
		//update failed
		return FALSE;
	}
	
	/**
	* Gets the parameters for the $field_name setter
	*/
	public function get_set_" . $field_name . "_parameters(){
		return 'previous_$field_name, new_$field_name';
	}";
			if ($field_type == "string") {
				$setter .= "
	/**
	* Gets a html text input field for setting $field_name
	*/
	public function print_" . $field_name . "_input_setter(){
		echo \"<input
			type='text'
			class='" . $this->class_name . "_" . $field_name . "_input_setter'
			title='Type here to edit this $field_name.'
			onblur='$this->class_name\" . \$this->primary_key_value . \".set_$field_name(this.value, updateSuccessful)'
			value='\$this->$field_name'
		/>\";
	}
	
	";
			} else if ($field_type == "TextArea") {
				$setter .= "
	/**
	* Gets a html TextArea input field for setting $field_name
	*/
	public function print_" . $field_name . "_input_setter(){
		echo \"<TextArea
			class='" . $this->class_name . "_" . $field_name . "_input_setter'
			title='Type here to edit this $field_name.'
			onblur='$this->class_name\" . \$this->primary_key_value . \".set_$field_name(this.value, updateSuccessful)'
		>\$this->$field_name</TextArea>\";
	}
	
	";
			} else if ($field_type == "boolean") {
				$setter .= "
	/**
	* Gets a html checkbox input field for setting $field_name
	*/
	public function print_" . $field_name . "_input_setter(){
		echo \"<input
			type='checkbox'
			class='" . $this->class_name . "_" . $field_name . "_input_setter'
			title='Click here to toggle $field_name. Checked is yes, unchecked is no.'
			onchange='
			if(this.checked){
			$this->class_name\" . \$this->primary_key_value . \".set_$field_name(1, updateSuccessful);
			} else {
			$this->class_name\" . \$this->primary_key_value . \".set_$field_name(0, updateSuccessful);
			}'
			\";
			if(\$this->$field_name){
				echo \" checked='checked' \";
			}
		echo \"/>\";
	}
	
	";
			} else if ($field_type == "float") {
				$setter .= "
	/**
	* Gets a html text input field for setting $field_name
	* checks to make sure a number is submitted
	*/
	public function print_" . $field_name . "_input_setter(){
		echo \"<input
			type='text'
			size='3'
			class='" . $this->class_name . "_" . $field_name . "_input_setter'
			title='Type here to edit this $field_name.'
			onblur='
				if(isNaN(this.value)){
					alert(\\\"$field_name must be a number (float)\\\");
					this.value = $this->class_name\" . \$this->primary_key_value . \".$field_name;
				} 
				else {
				$this->class_name\" . \$this->primary_key_value . \".set_$field_name(this.value, updateSuccessful);
				}'
			value='\$this->$field_name'
		/>\";
	}
	";
			} else if ($field_type == "integer") {
				$setter .= "
	/**
	* Gets a html text input field for setting $field_name
	* checks to make sure a number is submitted
	*/
	public function print_" . $field_name . "_input_setter(){
		echo \"<input
			type='text'
			size='2'
			class='" . $this->class_name . "_" . $field_name . "_input_setter'
			title='Type here to edit this $field_name.'
			onblur='
				if(isNaN(this.value)){
					alert(\\\"$field_name must be a number (integer)\\\");
					this.value = $this->class_name\" . \$this->primary_key_value . \".$field_name;
				} 
				else {
				$this->class_name\" . \$this->primary_key_value . \".set_$field_name(this.value, updateSuccessful);
				}'
			value='\$this->$field_name'
		/>\";
	}
	";
			}
			else if ($field_type == "Date") {
				$setter .= "
	/**
	* Gets a date field for setting $field_name
	* Uses the jQuery ui Datepicker interface
	*/
	public function print_" . $field_name . "_input_setter(){				
		?>
		<input
			type='text'
			size='8'
			class='datepicker<?php echo \$this->primary_key_value; ?> " . $this->class_name . "_" . $field_name . "_input_setter ui-state-default ui-corner-all'
			title='Click to select a date'
			onblur='//$this->class_name<?php echo \$this->primary_key_value; ?>.set_$field_name($.datepicker.formatDate(\"yy-mm-dd\", new Date(this.value)), updateSuccessful)'
			value='<?php 
				if(\$this->$field_name != null){
					echo date(\"m/d/Y\", strtotime(\$this->$field_name));
				}
				?>'/>
				
			<script	type='text/javascript'>
				$(function() {
					$('.datepicker<?php echo \$this->primary_key_value; ?>').datepicker({
							onSelect: function(dateText, inst) {  
								 var myDate = $.datepicker.formatDate('yy-mm-dd', new Date(dateText));
								 $this->class_name<?php echo \$this->primary_key_value; ?>.set_$field_name(myDate, updateSuccessful)
							 }
					});
				});
			</script>
				
				<?php
				
	}
				";
			}
			else{
				//default setter
				$setter .= "
	/**
	* Gets a html text input field for setting $field_name
	*/
	public function print_" . $field_name . "_input_setter(){
		echo \"<input
			type='text'
			class='" . $this->class_name . "_" . $field_name . "_input_setter'
			title='Type here to edit this $field_name.'
			onblur='$this->class_name\" . \$this->primary_key_value . \".set_$field_name(this.value, updateSuccessful)'
			value='\$this->$field_name'
		/>\";
	}
	
	";

			}
		} else {
			$setter = "";
		}
		$static_class_array_getter = $this->generateStaticClassArrayGetter ( $field_name );
		return $field_declaration . $getter . $setter . $static_class_array_getter;
	}

	/**
	 * Generates and returns the javascript code for the class
	 *
	 * @return string The javascript code for the class
	 */
	public function generateJavascriptClass() {
		$cgj = new ClassGeneratorJavascript ( $this->database, $this->table_name, $this->fields, $this->foreign_tables['pointToMe'] );
		return $cgj->generateClassCode ();
	}

	/**
	 * Generate a static class getter
	 * @return string
	 */
	public function generateStaticClassArrayGetter($field_name) {

		$staticClassGetter = "
	/**
	* Gets an array of $this->class_name where $field_name = \$$field_name from the database
	* @param variable \$$field_name The value of the field to select by
	* @param string \$OrderBy Field(s) to order by. If multiple fields, separate by a comma.
	* @param string \$direction ASC (ascending) or DESC (descending)
	* @return array of $this->class_name
	*/
	public static function get" . $this->class_name . "s_by_$field_name(\$$field_name, \$OrderBy = NULL, \$direction = 'DESC'){
		global \$" . $this->database . "_connection;
		\$" . $this->class_name . "s = array();
		\$$field_name = mysql_real_escape_string(\$$field_name);
		if(\$OrderBy != NULL){
			\$sql = \"SELECT * FROM $this->table_name WHERE $field_name = '\$$field_name' ORDER BY \$OrderBy \$direction;\";
		}
		else{
			\$sql = \"SELECT * FROM $this->table_name WHERE $field_name = '\$$field_name'\";
		}
		
		\$result = mysql_query(\$sql, \$" . $this->database . "_connection);
		while(\$row = mysql_fetch_array(\$result)){
			array_push(\$" . $this->class_name . "s, $this->class_name::constructFrom_" . $this->table_name . "_row(\$row));
		}
		return \$" . $this->class_name . "s;
	}

";
		//parameters for static class getter
		$parameters_getter = "
	/**
	* Gets the parameters for get_" . $this->class_name . "s_by_$field_name(\$$field_name)
	*/
	public function get_get" . $this->class_name . "s_by_" . $field_name . "_parameters(){
		return '$field_name';
	}";
		return $staticClassGetter . $parameters_getter;
	}

}

?>
Return current item: MySQL Ajax Database Access Layer - MADAL