public function save($create = false)
	{
		// Get model informations
		$tableName	= static::tableName();
		$columns	= static::tableColumns();
		$idColumn	= static::$idColumn;

		$isModel	= false;
		$into		= "";
		$values		= "";
		$updates	= "";
		$condition	= "";
		$params		= [];
		$primaries	= [];
		$updateCondition = "";

		// Remove columns for which no value is specified
		foreach ($columns as $i => $column)
		{
			$name	= $column["column_name"];
			$key	= $column["column_key"];

			// Build query components
			if (property_exists($this, $name) && !in_array($name, static::$autos))
			{
				$into		.= "$name, ";
				$values		.= ":$name, ";
				$updates	.= "$name = :$name, ";
				$condition	.= "$name = :$name and ";
				$params[$name] = $this->encodeValue($name);
			}
			
			// Primary keys used for selecting the correct row in update
			if (strcasecmp($key, "PRI") === 0)
			{
				$updateCondition .= "$name = :$name and ";

				if (property_exists($this, $name) && !in_array($name, static::$autos))
					$primaries[$name] = $this->encodeValue($name);

				// Check if is model with id column
				if ($name === $idColumn) $isModel = true;
			}
		}
		
		// Remove trailing characters
		$into				= substr($into, 0, -2);
		$values				= substr($values, 0, -2);
		$updates			= substr($updates, 0, -2);
		$condition			= substr($condition, 0, -5);
		$updateCondition	= substr($updateCondition, 0, -5);

		try
		{
			// Try to insert model
			$status = Db::query("
				insert into $tableName ($into)
				values ($values)
			", $params, static::getDbName(), false) !== false;
		}
		catch (PDOException $e)
		{
			// If force creation, then bubble up exception
			if ($create) throw $e;

			// Use exception to determine if it was a primary key conflict
			if ($e->getCode() === "23000" && preg_match("/.*'PRIMARY'$/", $e->getMessage()))
			{
				// Try to update model
				$status = Db::query("
					update $tableName
					set $updates
					where $updateCondition
				", $params, static::getDbName(), false) !== false;
			}
			else
				throw $e;
		}

		if ($status)
		{
			// Get last insert id
			$lastInsertId = Db::instance(static::getDbName())->lastInsertId();

			if ($lastInsertId > 0)
				// Fetch with inserted id
				return static::find($lastInsertId);
			else
				// If no last inserted if try to use update condition and primary keys
				return static::select("where $updateCondition", $primaries, static::getDbName())->first();
		}
		else
			return false;
	}
public function save($create = false)
	{
		// Get model informations
		$tableName	= static::tableName();
		$columns	= static::tableColumns();
		$idColumn	= static::$idColumn;

		$isModel	= false;
		$into		= "";
		$values		= "";
		$updates	= "";
		$condition	= "";
		$params		= [];
		$primaries	= [];
		$updateCondition = "";

		// Remove columns for which no value is specified
		foreach ($columns as $i => $column)
		{
			$name	= $column["column_name"];
			$key	= $column["column_key"];

			// Build query components
			if (property_exists($this, $name) && !in_array($name, static::$autos))
			{
				$into		.= "$name, ";
				$values		.= ":$name, ";
				$updates	.= "$name = :$name, ";
				$condition	.= "$name = :$name and ";
				$params[$name] = $this->encodeValue($name);
			}
			
			// Primary keys used for selecting the correct row in update
			if (strcasecmp($key, "PRI") === 0)
			{
				$updateCondition .= "$name = :$name and ";

				if (property_exists($this, $name) && !in_array($name, static::$autos))
					$primaries[$name] = $this->encodeValue($name);

				// Check if is model with id column
				if ($name === $idColumn) $isModel = true;
			}
		}
		
		// Remove trailing characters
		$into				= substr($into, 0, -2);
		$values				= substr($values, 0, -2);
		$updates			= substr($updates, 0, -2);
		$condition			= substr($condition, 0, -5);
		$updateCondition	= substr($updateCondition, 0, -5);

		try
		{
			// Try to insert model
			$status = Db::query("
				insert into $tableName ($into)
				values ($values)
			", $params, static::getDbName(), false) !== false;
		}
		catch (PDOException $e)
		{
			// If force creation, then bubble up exception
			if ($create) throw $e;

			// Use exception to determine if it was a primary key conflict
			if ($e->getCode() === "23000" && preg_match("/.*'PRIMARY'$/", $e->getMessage()))
			{
				// Try to update model
				$status = Db::query("
					update $tableName
					set $updates
					where $updateCondition
				", $params, static::getDbName(), false) !== false;
			}
			else
				throw $e;
		}

		if ($status)
		{
			// Get last insert id
			$lastInsertId = Db::instance(static::getDbName())->lastInsertId();

			if ($lastInsertId > 0)
				// Fetch with inserted id
				return static::find($lastInsertId);
			else
				// If no last inserted if try to use update condition and primary keys
				return static::select("where $updateCondition", $primaries, static::getDbName())->first();
		}
		else
			return false;
	}
public function save ( $ create = false ) { // Get model informations
 $ tableName = static :: tableName ( ) ; $ columns = static :: tableColumns ( ) ; $ idColumn = static :: $ idColumn ; $ isModel = false ; $ into = "" ; $ values = "" ; $ updates = "" ; $ condition = "" ; $ params = [ ] ; $ primaries = [ ] ; $ updateCondition = "" ; // Remove columns for which no value is specified
 foreach ( $ columns as $ i => $ column ) { $ name = $ column [ "column_name" ] ; $ key = $ column [ "column_key" ] ; // Build query components
 if ( property_exists ( $ this , $ name ) && ! in_array ( $ name , static :: $ autos ) ) { $ into .= "$name, " ; $ values .= ":$name, " ; $ updates .= "$name = :$name, " ; $ condition .= "$name = :$name and " ; $ params [ $ name ] = $ this -> encodeValue ( $ name ) ; } // Primary keys used for selecting the correct row in update
 if ( strcasecmp ( $ key , "PRI" ) === 0 ) { $ updateCondition .= "$name = :$name and " ; if ( property_exists ( $ this , $ name ) && ! in_array ( $ name , static :: $ autos ) ) $ primaries [ $ name ] = $ this -> encodeValue ( $ name ) ; // Check if is model with id column
 if ( $ name === $ idColumn ) $ isModel = true ; } } // Remove trailing characters
 $ into = substr ( $ into , 0 , - 2 ) ; $ values = substr ( $ values , 0 , - 2 ) ; $ updates = substr ( $ updates , 0 , - 2 ) ; $ condition = substr ( $ condition , 0 , - 5 ) ; $ updateCondition = substr ( $ updateCondition , 0 , - 5 ) ; try { // Try to insert model
 $ status = Db :: query ( "
				insert into $tableName ($into)
				values ($values)
			" , $ params , static :: getDbName ( ) , false ) !== false ; } catch ( PDOException $ e ) { // If force creation, then bubble up exception
 if ( $ create ) throw $ e ; // Use exception to determine if it was a primary key conflict
 if ( $ e -> getCode ( ) === "23000" && preg_match ( "/.*'PRIMARY'$/" , $ e -> getMessage ( ) ) ) { // Try to update model
 $ status = Db :: query ( "
					update $tableName
					set $updates
					where $updateCondition
				" , $ params , static :: getDbName ( ) , false ) !== false ; } else throw $ e ; } if ( $ status ) { // Get last insert id
 $ lastInsertId = Db :: instance ( static :: getDbName ( ) ) -> lastInsertId ( ) ; if ( $ lastInsertId > 0 ) // Fetch with inserted id
 return static :: find ( $ lastInsertId ) ; else // If no last inserted if try to use update condition and primary keys
 return static :: select ( "where $updateCondition" , $ primaries , static :: getDbName ( ) ) -> first ( ) ; } else return false ; }
Save object to database, updating or inserting a new row

@param bool $create for models without an id column this determines if we insert or udpate the model

@return static|bool false if fails
Save object to database updating or inserting a new row