Пример #1
0
/**
 * Returns the annotations found in the methods' docblocks
 *
 * @return Phalcon\Annotations\Collection[]
 */
PHP_METHOD(Phalcon_Annotations_Reflection, getMethodsAnnotations){

	zval *annotations, *reflection_data, *reflection_methods;
	zval *collections, *reflection_method = NULL, *method_name = NULL;
	zval *collection = NULL;
	HashTable *ah0;
	HashPosition hp0;
	zval **hd;

	PHALCON_MM_GROW();

	PHALCON_OBS_VAR(annotations);
	phalcon_read_property(&annotations, this_ptr, SL("_methodAnnotations"), PH_NOISY_CC);
	if (Z_TYPE_P(annotations) != IS_OBJECT) {
	
		PHALCON_OBS_VAR(reflection_data);
		phalcon_read_property(&reflection_data, this_ptr, SL("_reflectionData"), PH_NOISY_CC);
		if (phalcon_array_isset_string(reflection_data, SS("methods"))) {
	
			PHALCON_OBS_VAR(reflection_methods);
			phalcon_array_fetch_string(&reflection_methods, reflection_data, SL("methods"), PH_NOISY_CC);
			if (phalcon_fast_count_ev(reflection_methods TSRMLS_CC)) {
	
				PHALCON_INIT_VAR(collections);
				array_init(collections);
	
				if (!phalcon_is_iterable(reflection_methods, &ah0, &hp0, 0, 0 TSRMLS_CC)) {
					return;
				}
	
				while (zend_hash_get_current_data_ex(ah0, (void**) &hd, &hp0) == SUCCESS) {
	
					PHALCON_GET_FOREACH_KEY(method_name, ah0, hp0);
					PHALCON_GET_FOREACH_VALUE(reflection_method);
	
					PHALCON_INIT_NVAR(collection);
					object_init_ex(collection, phalcon_annotations_collection_ce);
					PHALCON_CALL_METHOD_PARAMS_1_NORETURN(collection, "__construct", reflection_method);
	
					phalcon_array_update_zval(&collections, method_name, &collection, PH_COPY | PH_SEPARATE TSRMLS_CC);
	
					zend_hash_move_forward_ex(ah0, &hp0);
				}
	
				phalcon_update_property_zval(this_ptr, SL("_methodAnnotations"), collections TSRMLS_CC);
	
				RETURN_CTOR(collections);
			}
		}
	
		phalcon_update_property_bool(this_ptr, SL("_methodAnnotations"), 0 TSRMLS_CC);
		RETURN_MM_FALSE;
	}
	
	
	RETURN_CCTOR(annotations);
}
Пример #2
0
/**
 * Returns the annotations found in the properties' docblocks
 *
 * @return Phalcon\Annotations\Collection[]
 */
PHP_METHOD(Phalcon_Annotations_Reflection, getPropertiesAnnotations){

	zval *annotations, *reflection_data, *reflection_properties;
	zval *collections, *reflection_property = NULL, *property = NULL;
	zval *collection = NULL;
	HashTable *ah0;
	HashPosition hp0;
	zval **hd;

	PHALCON_MM_GROW();

	PHALCON_OBS_VAR(annotations);
	phalcon_read_property_this(&annotations, this_ptr, SL("_propertyAnnotations"), PH_NOISY_CC);
	if (Z_TYPE_P(annotations) != IS_OBJECT) {
	
		PHALCON_OBS_VAR(reflection_data);
		phalcon_read_property_this(&reflection_data, this_ptr, SL("_reflectionData"), PH_NOISY_CC);
		if (phalcon_array_isset_string(reflection_data, SS("properties"))) {
	
			PHALCON_OBS_VAR(reflection_properties);
			phalcon_array_fetch_string(&reflection_properties, reflection_data, SL("properties"), PH_NOISY_CC);
			if (phalcon_fast_count_ev(reflection_properties TSRMLS_CC)) {
	
				PHALCON_INIT_VAR(collections);
				array_init(collections);
	
				phalcon_is_iterable(reflection_properties, &ah0, &hp0, 0, 0);
	
				while (zend_hash_get_current_data_ex(ah0, (void**) &hd, &hp0) == SUCCESS) {
	
					PHALCON_GET_HKEY(property, ah0, hp0);
					PHALCON_GET_HVALUE(reflection_property);
	
					PHALCON_INIT_NVAR(collection);
					object_init_ex(collection, phalcon_annotations_collection_ce);
					phalcon_call_method_p1_noret(collection, "__construct", reflection_property);
	
					phalcon_array_update_zval(&collections, property, &collection, PH_COPY | PH_SEPARATE TSRMLS_CC);
	
					zend_hash_move_forward_ex(ah0, &hp0);
				}
	
				phalcon_update_property_this(this_ptr, SL("_propertyAnnotations"), collections TSRMLS_CC);
	
				RETURN_CTOR(collections);
			}
		}
	
		phalcon_update_property_bool(this_ptr, SL("_propertyAnnotations"), 0 TSRMLS_CC);
		RETURN_MM_FALSE;
	}
	
	RETURN_CCTOR(annotations);
}
Пример #3
0
/**
 * Checks if the internal meta-data container is empty
 *
 *<code>
 *	var_dump($metaData->isEmpty());
 *</code>
 *
 * @return boolean
 */
PHP_METHOD(Phalcon_Mvc_Model_MetaData, isEmpty){

	zval *meta_data;

	PHALCON_MM_GROW();

	PHALCON_OBS_VAR(meta_data);
	phalcon_read_property_this(&meta_data, this_ptr, SL("_metaData"), PH_NOISY TSRMLS_CC);
	if (phalcon_fast_count_ev(meta_data TSRMLS_CC)) {
		RETURN_MM_FALSE;
	}
	
	RETURN_MM_TRUE;
}
Пример #4
0
/**
 * Returns the translation related to the given key
 *
 * @param string $index
 * @param array $placeholders
 * @return string
 */
PHP_METHOD(Phalcon_Translate_Adapter_NativeArray, query){

	zval *index, *placeholders = NULL, *translate, *translation = NULL;
	zval *value = NULL, *key = NULL, *key_placeholder = NULL, *replaced = NULL;
	HashTable *ah0;
	HashPosition hp0;
	zval **hd;

	PHALCON_MM_GROW();

	phalcon_fetch_params(1, 1, 1, &index, &placeholders);
	
	if (!placeholders) {
		PHALCON_INIT_VAR(placeholders);
	}
	
	PHALCON_OBS_VAR(translate);
	phalcon_read_property_this(&translate, this_ptr, SL("_translate"), PH_NOISY_CC);
	if (phalcon_array_isset(translate, index)) {
	
		PHALCON_OBS_VAR(translation);
		phalcon_array_fetch(&translation, translate, index, PH_NOISY);
		if (Z_TYPE_P(placeholders) == IS_ARRAY) { 
			if (phalcon_fast_count_ev(placeholders TSRMLS_CC)) {
	
				phalcon_is_iterable(placeholders, &ah0, &hp0, 0, 0);
	
				while (zend_hash_get_current_data_ex(ah0, (void**) &hd, &hp0) == SUCCESS) {
	
					PHALCON_GET_HKEY(key, ah0, hp0);
					PHALCON_GET_HVALUE(value);
	
					PHALCON_INIT_NVAR(key_placeholder);
					PHALCON_CONCAT_SVS(key_placeholder, "%", key, "%");
	
					PHALCON_INIT_NVAR(replaced);
					phalcon_fast_str_replace(replaced, key_placeholder, value, translation);
					PHALCON_CPY_WRT(translation, replaced);
	
					zend_hash_move_forward_ex(ah0, &hp0);
				}
	
			}
		}
	
		RETURN_CCTOR(translation);
	}
	
	RETURN_CCTOR(index);
}
Пример #5
0
/**
 * Draws a polygon
 *
 *<code>
 * $coordinates = array( array( 'x' => 4, 'y' => 6 ), array( 'x' => 8, 'y' => 10 ) );
 * $image->polygon($coordinates);
 *</code>
 *
 * @param array $coordinates array of x and y
 * @param string $color
 * @return Phalcon\Image\Adapter\GD
 */
PHP_METHOD(Phalcon_Image_Adapter_GD, polygon){

	zval *coordinates, *color = NULL, image = {}, rgb = {}, r = {}, g = {}, b = {}, imagecolor = {}, *point, points = {}, num_points = {};

	phalcon_fetch_params(0, 1, 1, &coordinates, &color);

	if (!color) {
		color = &PHALCON_GLOBAL(z_null);
	}

	if (!phalcon_fast_count_ev(coordinates)) {
		PHALCON_THROW_EXCEPTION_STRW(phalcon_image_exception_ce, "Coordinates must be not empty");
		return;
	}
	phalcon_return_property(&image, getThis(), SL("_image"));

	if (Z_TYPE(image) == IS_RESOURCE) {
		PHALCON_CALL_METHODW(&rgb, getThis(), "getcolorrbg", color);
		phalcon_array_fetch_long(&r, &rgb, 0, PH_NOISY);
		phalcon_array_fetch_long(&g, &rgb, 1, PH_NOISY);
		phalcon_array_fetch_long(&b, &rgb, 2, PH_NOISY);

		PHALCON_CALL_FUNCTIONW(&imagecolor, "imagecolorallocate", &image, &r, &g, &b);

		array_init(&points);
		ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(coordinates), point) {
			zval x = {}, y = {};
			if (Z_TYPE_P(point) == IS_ARRAY) {
				if (phalcon_fast_count_int(point) != 2) {
					PHALCON_THROW_EXCEPTION_STRW(phalcon_image_exception_ce, "Coordinates point error");
					return;
				}
				if (!phalcon_array_isset_fetch_long(&x, point, 0)) {
					phalcon_array_fetch_str(&x, point, SL("x"), PH_NOISY);
				}
				if (!phalcon_array_isset_fetch_long(&y, point, 0)) {
					phalcon_array_fetch_str(&y, point, SL("y"), PH_NOISY);
				}
				phalcon_array_append(&points, &x, PH_COPY);
				phalcon_array_append(&points, &y, PH_COPY);
			} else {
				phalcon_array_append(&points, &_p->val, PH_COPY);
				_p++;
				phalcon_array_append(&points, &_p->val, PH_COPY);
			}
			phalcon_increment(&num_points);
		} ZEND_HASH_FOREACH_END();
Пример #6
0
/**
 * Checks whether there are messages attached to the element
 *
 * @return boolean
 */
PHP_METHOD(Phalcon_Forms_Element, hasMessages){

	zval *messages;

	PHALCON_MM_GROW();

	/** 
	 * Get the related form
	 */
	PHALCON_OBS_VAR(messages);
	phalcon_read_property_this(&messages, this_ptr, SL("_messages"), PH_NOISY_CC);
	if (Z_TYPE_P(messages) == IS_OBJECT) {
		if (phalcon_fast_count_ev(messages TSRMLS_CC)) {
			RETURN_MM_TRUE;
		}
	}
	
	RETURN_MM_FALSE;
}
Пример #7
0
/**
 * Removes transactions from the TransactionManager
 *
 * @param Phalcon\Mvc\Model\TransactionInterface $transaction
 */
PHP_METHOD(Phalcon_Mvc_Model_Transaction_Manager, _collectTransaction){

	zval *transaction, *transactions, *new_transactions;
	zval *managed_transaction = NULL;
	HashTable *ah0;
	HashPosition hp0;
	zval **hd;

	PHALCON_MM_GROW();

	phalcon_fetch_params(1, 1, 0, &transaction);
	
	PHALCON_OBS_VAR(transactions);
	phalcon_read_property_this(&transactions, this_ptr, SL("_transactions"), PH_NOISY_CC);
	if (phalcon_fast_count_ev(transactions TSRMLS_CC)) {
	
		PHALCON_INIT_VAR(new_transactions);
		array_init(new_transactions);
	
		phalcon_is_iterable(transactions, &ah0, &hp0, 0, 0);
	
		while (zend_hash_get_current_data_ex(ah0, (void**) &hd, &hp0) == SUCCESS) {
	
			PHALCON_GET_HVALUE(managed_transaction);
	
			if (PHALCON_IS_EQUAL(managed_transaction, transaction)) {
				phalcon_array_append(&new_transactions, transaction, PH_SEPARATE);
				phalcon_property_decr(this_ptr, SL("_number") TSRMLS_CC);
			}
	
			zend_hash_move_forward_ex(ah0, &hp0);
		}
	
		phalcon_update_property_this(this_ptr, SL("_transactions"), new_transactions TSRMLS_CC);
	}
	
	PHALCON_MM_RESTORE();
}
Пример #8
0
/**
 * Reads annotations from the class dockblocks, its methods and/or properties
 *
 * @param string $className
 * @return array
 */
PHP_METHOD(Phalcon_Annotations_Reader, parse){

	zval *class_name, *annotations, *reflection;
	zval *comment = NULL, *file = NULL, *line = NULL, *class_annotations;
	zval *properties, *annotations_properties;
	zval *property = NULL, *property_annotations = NULL, *name = NULL;
	zval *methods, *annotations_methods, *method = NULL;
	zval *method_annotations = NULL;
	HashTable *ah0, *ah1;
	HashPosition hp0, hp1;
	zval **hd;
	zend_class_entry *ce0;

	PHALCON_MM_GROW();

	phalcon_fetch_params(1, 1, 0, &class_name);
	
	if (unlikely(Z_TYPE_P(class_name) != IS_STRING)) {
		PHALCON_THROW_EXCEPTION_STR(phalcon_annotations_exception_ce, "The class name must be an object");
		return;
	}
	
	PHALCON_INIT_VAR(annotations);
	array_init(annotations);
	
	/** 
	 * A ReflectionClass is used to obtain the class dockblock
	 */
	ce0 = zend_fetch_class(SL("ReflectionClass"), ZEND_FETCH_CLASS_AUTO TSRMLS_CC);
	
	PHALCON_INIT_VAR(reflection);
	object_init_ex(reflection, ce0);
	if (phalcon_has_constructor(reflection TSRMLS_CC)) {
		phalcon_call_method_p1_noret(reflection, "__construct", class_name);
	}
	
	PHALCON_INIT_VAR(comment);
	phalcon_call_method(comment, reflection, "getdoccomment");
	if (Z_TYPE_P(comment) == IS_STRING) {
	
		/** 
		 * Get the file where the class was declared
		 */
		PHALCON_INIT_VAR(file);
		phalcon_call_method(file, reflection, "getfilename");
	
		/** 
		 * Get the line where the class was declared
		 */
		PHALCON_INIT_VAR(line);
		phalcon_call_method(line, reflection, "getstartline");
	
		/** 
		 * Read annotations from class
		 */
		PHALCON_INIT_VAR(class_annotations);
		if (phannot_parse_annotations(class_annotations, comment, file, line TSRMLS_CC) == FAILURE) {
			return;
		}
	
		/** 
		 * Append the class annotations to the annotations var
		 */
		if (Z_TYPE_P(class_annotations) == IS_ARRAY) { 
			phalcon_array_update_string(&annotations, SL("class"), &class_annotations, PH_COPY | PH_SEPARATE);
		}
	}
	
	/** 
	 * Get the class properties
	 */
	PHALCON_INIT_VAR(properties);
	phalcon_call_method(properties, reflection, "getproperties");
	if (phalcon_fast_count_ev(properties TSRMLS_CC)) {
	
		/** 
		 * Line declaration for properties isn't available
		 */
		PHALCON_INIT_NVAR(line);
		ZVAL_LONG(line, 1);
	
		PHALCON_INIT_VAR(annotations_properties);
		array_init(annotations_properties);
	
		phalcon_is_iterable(properties, &ah0, &hp0, 0, 0);
	
		while (zend_hash_get_current_data_ex(ah0, (void**) &hd, &hp0) == SUCCESS) {
	
			PHALCON_GET_HVALUE(property);
	
			/** 
			 * Read comment from method
			 */
			PHALCON_INIT_NVAR(comment);
			phalcon_call_method(comment, property, "getdoccomment");
			if (Z_TYPE_P(comment) == IS_STRING) {
	
				/** 
				 * Get the file where the property was declared
				 */
				PHALCON_INIT_NVAR(file);
				phalcon_call_method(file, reflection, "getfilename");
	
				/** 
				 * Read annotations from the docblock
				 */
				PHALCON_INIT_NVAR(property_annotations);
				if (phannot_parse_annotations(property_annotations, comment, file, line TSRMLS_CC) == FAILURE) {
					return;
				}
				if (Z_TYPE_P(property_annotations) == IS_ARRAY) { 
					PHALCON_OBS_NVAR(name);
					phalcon_read_property(&name, property, SL("name"), PH_NOISY_CC);
					phalcon_array_update_zval(&annotations_properties, name, &property_annotations, PH_COPY | PH_SEPARATE);
				}
			}
	
			zend_hash_move_forward_ex(ah0, &hp0);
		}
	
		if (phalcon_fast_count_ev(annotations_properties TSRMLS_CC)) {
			phalcon_array_update_string(&annotations, SL("properties"), &annotations_properties, PH_COPY | PH_SEPARATE);
		}
	}
	
	/** 
	 * Get the class methods
	 */
	PHALCON_INIT_VAR(methods);
	phalcon_call_method(methods, reflection, "getmethods");
	if (phalcon_fast_count_ev(methods TSRMLS_CC)) {
	
		PHALCON_INIT_VAR(annotations_methods);
		array_init(annotations_methods);
	
		phalcon_is_iterable(methods, &ah1, &hp1, 0, 0);
	
		while (zend_hash_get_current_data_ex(ah1, (void**) &hd, &hp1) == SUCCESS) {
	
			PHALCON_GET_HVALUE(method);
	
			/** 
			 * Read comment from method
			 */
			PHALCON_INIT_NVAR(comment);
			phalcon_call_method(comment, method, "getdoccomment");
			if (Z_TYPE_P(comment) == IS_STRING) {
	
				/** 
				 * Get the file where the method was declared
				 */
				PHALCON_INIT_NVAR(file);
				phalcon_call_method(file, method, "getfilename");
	
				/** 
				 * Get the line where the method was declared
				 */
				PHALCON_INIT_NVAR(line);
				phalcon_call_method(line, method, "getstartline");
	
				/** 
				 * Read annotations from class
				 */
				PHALCON_INIT_NVAR(method_annotations);
				if (phannot_parse_annotations(method_annotations, comment, file, line TSRMLS_CC) == FAILURE) {
					return;
				}
				if (Z_TYPE_P(method_annotations) == IS_ARRAY) { 
					PHALCON_OBS_NVAR(name);
					phalcon_read_property(&name, method, SL("name"), PH_NOISY_CC);
					phalcon_array_update_zval(&annotations_methods, name, &method_annotations, PH_COPY | PH_SEPARATE);
				}
			}
	
			zend_hash_move_forward_ex(ah1, &hp1);
		}
	
		if (phalcon_fast_count_ev(annotations_methods TSRMLS_CC)) {
			phalcon_array_update_string(&annotations, SL("methods"), &annotations_methods, PH_COPY | PH_SEPARATE);
		}
	}
	
	RETURN_CTOR(annotations);
}
Пример #9
0
/**
 * The meta-data is obtained by reading the column descriptions from the database information schema
 *
 * @param Phalcon\Mvc\ModelInterface $model
 * @param Phalcon\DiInterface $dependencyInjector
 * @return array
 */
PHP_METHOD(Phalcon_Mvc_Model_MetaData_Strategy_Introspection, getMetaData) {

    zval *model, *dependency_injector, *class_name;
    zval *schema, *table, *read_connection, *exists;
    zval *complete_table = NULL, *exception_message = NULL;
    zval *columns, *attributes, *primary_keys, *non_primary_keys;
    zval *numeric_typed, *not_null, *field_types;
    zval *field_bind_types, *automatic_default;
    zval *identity_field = NULL, *column = NULL, *field_name = NULL, *feature = NULL;
    zval *type = NULL, *bind_type = NULL, *model_metadata;
    HashTable *ah0;
    HashPosition hp0;
    zval **hd;

    PHALCON_MM_GROW();

    phalcon_fetch_params(1, 2, 0, &model, &dependency_injector);

    PHALCON_INIT_VAR(class_name);
    phalcon_get_class(class_name, model, 0 TSRMLS_CC);

    PHALCON_INIT_VAR(schema);
    phalcon_call_method(schema, model, "getschema");

    PHALCON_INIT_VAR(table);
    phalcon_call_method(table, model, "getsource");

    /**
     * Check if the mapped table exists on the database
     */
    PHALCON_INIT_VAR(read_connection);
    phalcon_call_method(read_connection, model, "getreadconnection");

    PHALCON_INIT_VAR(exists);
    phalcon_call_method_p2(exists, read_connection, "tableexists", table, schema);
    if (!zend_is_true(exists)) {
        if (zend_is_true(schema)) {
            PHALCON_INIT_VAR(complete_table);
            PHALCON_CONCAT_VSV(complete_table, schema, "\".\"", table);
        } else {
            PHALCON_CPY_WRT(complete_table, table);
        }

        /**
         * The table not exists
         */
        PHALCON_INIT_VAR(exception_message);
        PHALCON_CONCAT_SVSV(exception_message, "Table \"", complete_table, "\" doesn't exist on database when dumping meta-data for ", class_name);
        PHALCON_THROW_EXCEPTION_ZVAL(phalcon_mvc_model_exception_ce, exception_message);
        return;
    }

    /**
     * Try to describe the table
     */
    PHALCON_INIT_VAR(columns);
    phalcon_call_method_p2(columns, read_connection, "describecolumns", table, schema);
    if (!phalcon_fast_count_ev(columns TSRMLS_CC)) {
        if (zend_is_true(schema)) {
            PHALCON_INIT_NVAR(complete_table);
            PHALCON_CONCAT_VSV(complete_table, schema, "\".\"", table);
        } else {
            PHALCON_CPY_WRT(complete_table, table);
        }

        /**
         * The table not exists
         */
        PHALCON_INIT_NVAR(exception_message);
        PHALCON_CONCAT_SVSV(exception_message, "Cannot obtain table columns for the mapped source \"", complete_table, "\" used in model ", class_name);
        PHALCON_THROW_EXCEPTION_ZVAL(phalcon_mvc_model_exception_ce, exception_message);
        return;
    }

    /**
     * Initialize meta-data
     */
    PHALCON_INIT_VAR(attributes);
    array_init(attributes);

    PHALCON_INIT_VAR(primary_keys);
    array_init(primary_keys);

    PHALCON_INIT_VAR(non_primary_keys);
    array_init(non_primary_keys);

    PHALCON_INIT_VAR(numeric_typed);
    array_init(numeric_typed);

    PHALCON_INIT_VAR(not_null);
    array_init(not_null);

    PHALCON_INIT_VAR(field_types);
    array_init(field_types);

    PHALCON_INIT_VAR(field_bind_types);
    array_init(field_bind_types);

    PHALCON_INIT_VAR(automatic_default);
    array_init(automatic_default);

    PHALCON_INIT_VAR(identity_field);
    ZVAL_BOOL(identity_field, 0);

    phalcon_is_iterable(columns, &ah0, &hp0, 0, 0);

    while (zend_hash_get_current_data_ex(ah0, (void**) &hd, &hp0) == SUCCESS) {

        PHALCON_GET_HVALUE(column);

        PHALCON_INIT_NVAR(field_name);
        phalcon_call_method(field_name, column, "getname");
        phalcon_array_append(&attributes, field_name, PH_SEPARATE);

        /**
         * To mark fields as primary keys
         */
        PHALCON_INIT_NVAR(feature);
        phalcon_call_method(feature, column, "isprimary");
        if (PHALCON_IS_TRUE(feature)) {
            phalcon_array_append(&primary_keys, field_name, PH_SEPARATE);
        } else {
            phalcon_array_append(&non_primary_keys, field_name, PH_SEPARATE);
        }

        /**
         * To mark fields as numeric
         */
        PHALCON_INIT_NVAR(feature);
        phalcon_call_method(feature, column, "isnumeric");
        if (PHALCON_IS_TRUE(feature)) {
            phalcon_array_update_zval_bool(&numeric_typed, field_name, 1, PH_SEPARATE);
        }

        /**
         * To mark fields as not null
         */
        PHALCON_INIT_NVAR(feature);
        phalcon_call_method(feature, column, "isnotnull");
        if (PHALCON_IS_TRUE(feature)) {
            phalcon_array_append(&not_null, field_name, PH_SEPARATE);
        }

        /**
         * To mark fields as identity columns
         */
        PHALCON_INIT_NVAR(feature);
        phalcon_call_method(feature, column, "isautoincrement");
        if (PHALCON_IS_TRUE(feature)) {
            PHALCON_CPY_WRT(identity_field, field_name);
        }

        /**
         * To get the internal types
         */
        PHALCON_INIT_NVAR(type);
        phalcon_call_method(type, column, "gettype");
        phalcon_array_update_zval(&field_types, field_name, &type, PH_COPY | PH_SEPARATE);

        /**
         * To mark how the fields must be escaped
         */
        PHALCON_INIT_NVAR(bind_type);
        phalcon_call_method(bind_type, column, "getbindtype");
        phalcon_array_update_zval(&field_bind_types, field_name, &bind_type, PH_COPY | PH_SEPARATE);

        zend_hash_move_forward_ex(ah0, &hp0);
    }

    /**
     * Create an array using the MODELS_* constants as indexes
     */
    PHALCON_INIT_VAR(model_metadata);
    array_init(model_metadata);
    phalcon_array_update_long(&model_metadata, 0, &attributes, PH_COPY | PH_SEPARATE);
    phalcon_array_update_long(&model_metadata, 1, &primary_keys, PH_COPY | PH_SEPARATE);
    phalcon_array_update_long(&model_metadata, 2, &non_primary_keys, PH_COPY | PH_SEPARATE);
    phalcon_array_update_long(&model_metadata, 3, &not_null, PH_COPY | PH_SEPARATE);
    phalcon_array_update_long(&model_metadata, 4, &field_types, PH_COPY | PH_SEPARATE);
    phalcon_array_update_long(&model_metadata, 5, &numeric_typed, PH_COPY | PH_SEPARATE);
    phalcon_array_update_long(&model_metadata, 8, &identity_field, PH_COPY | PH_SEPARATE);
    phalcon_array_update_long(&model_metadata, 9, &field_bind_types, PH_COPY | PH_SEPARATE);
    phalcon_array_update_long(&model_metadata, 10, &automatic_default, PH_COPY | PH_SEPARATE);
    phalcon_array_update_long(&model_metadata, 11, &automatic_default, PH_COPY | PH_SEPARATE);

    RETURN_CTOR(model_metadata);
}
Пример #10
0
/**
 * Returns a PHQL statement built based on the builder parameters
 *
 * @return string
 */
PHP_METHOD(Phalcon_Mvc_Model_Query_Builder, getPhql){

	zval *dependency_injector = NULL, *models, *conditions = NULL;
	zval *one, *number_models, *invalid_condition;
	zval *model = NULL, *service_name, *meta_data, *model_instance;
	zval *no_primary = NULL, *primary_keys, *first_primary_key;
	zval *column_map = NULL, *attribute_field = NULL, *exception_message;
	zval *primary_key_condition, *phql, *columns;
	zval *selected_columns = NULL, *column = NULL, *alias = NULL, *aliased_column = NULL;
	zval *joined_columns = NULL, *selected_column = NULL, *selected_models;
	zval *selected_model = NULL, *joined_models, *joins;
	zval *join = NULL, *join_model = NULL, *join_conditions = NULL, *join_alias = NULL;
	zval *join_type = NULL, *group, *group_items, *group_item = NULL;
	zval *escaped_item = NULL, *joined_items = NULL, *having, *order;
	zval *order_items, *order_item = NULL, *limit, *number;
	zval *offset = NULL;
	HashTable *ah0, *ah1, *ah2, *ah3, *ah4, *ah5;
	HashPosition hp0, hp1, hp2, hp3, hp4, hp5;
	zval **hd;
	zend_class_entry *ce0;

	PHALCON_MM_GROW();

	PHALCON_OBS_VAR(dependency_injector);
	phalcon_read_property(&dependency_injector, this_ptr, SL("_dependencyInjector"), PH_NOISY_CC);
	if (Z_TYPE_P(dependency_injector) != IS_OBJECT) {
		PHALCON_INIT_NVAR(dependency_injector);
		PHALCON_CALL_STATIC(dependency_injector, "phalcon\\di", "getdefault");
		phalcon_update_property_zval(this_ptr, SL("_dependencyInjector"), dependency_injector TSRMLS_CC);
	}
	
	PHALCON_OBS_VAR(models);
	phalcon_read_property(&models, this_ptr, SL("_models"), PH_NOISY_CC);
	if (Z_TYPE_P(models) == IS_ARRAY) { 
		if (!phalcon_fast_count_ev(models TSRMLS_CC)) {
			PHALCON_THROW_EXCEPTION_STR(phalcon_mvc_model_exception_ce, "At least one model is required to build the query");
			return;
		}
	} else {
		if (!zend_is_true(models)) {
			PHALCON_THROW_EXCEPTION_STR(phalcon_mvc_model_exception_ce, "At least one model is required to build the query");
			return;
		}
	}
	
	PHALCON_OBS_VAR(conditions);
	phalcon_read_property(&conditions, this_ptr, SL("_conditions"), PH_NOISY_CC);
	if (phalcon_is_numeric(conditions)) {
	
		/** 
		 * If the conditions is a single numeric field. We internally create a condition
		 * using the related primary key
		 */
		if (Z_TYPE_P(models) == IS_ARRAY) { 
	
			PHALCON_INIT_VAR(one);
			ZVAL_LONG(one, 1);
	
			PHALCON_INIT_VAR(number_models);
			phalcon_fast_count(number_models, models TSRMLS_CC);
	
			PHALCON_INIT_VAR(invalid_condition);
			is_smaller_function(invalid_condition, one, number_models TSRMLS_CC);
			if (PHALCON_IS_TRUE(invalid_condition)) {
				PHALCON_THROW_EXCEPTION_STR(phalcon_mvc_model_exception_ce, "Cannot build the query. Invalid condition");
				return;
			}
	
			PHALCON_OBS_VAR(model);
			phalcon_array_fetch_long(&model, models, 0, PH_NOISY_CC);
		} else {
			PHALCON_CPY_WRT(model, models);
		}
	
		PHALCON_INIT_VAR(service_name);
		ZVAL_STRING(service_name, "modelsMetadata", 1);
	
		/** 
		 * Get the models metadata service to obtain the column names, column map and
		 * primary key
		 */
		PHALCON_INIT_VAR(meta_data);
		PHALCON_CALL_METHOD_PARAMS_1(meta_data, dependency_injector, "getshared", service_name);
		ce0 = phalcon_fetch_class(model TSRMLS_CC);
	
		PHALCON_INIT_VAR(model_instance);
		object_init_ex(model_instance, ce0);
		if (phalcon_has_constructor(model_instance TSRMLS_CC)) {
			PHALCON_CALL_METHOD_PARAMS_1_NORETURN(model_instance, "__construct", dependency_injector);
		}
	
		PHALCON_INIT_VAR(no_primary);
		ZVAL_BOOL(no_primary, 1);
	
		PHALCON_INIT_VAR(primary_keys);
		PHALCON_CALL_METHOD_PARAMS_1(primary_keys, meta_data, "getprimarykeyattributes", model_instance);
		if (phalcon_fast_count_ev(primary_keys TSRMLS_CC)) {
			if (phalcon_array_isset_long(primary_keys, 0)) {
	
				PHALCON_OBS_VAR(first_primary_key);
				phalcon_array_fetch_long(&first_primary_key, primary_keys, 0, PH_NOISY_CC);
	
				/** 
				 * The PHQL contains the renamed columns if available
				 */
				if (PHALCON_GLOBAL(orm).column_renaming) {
					PHALCON_INIT_VAR(column_map);
					PHALCON_CALL_METHOD_PARAMS_1(column_map, meta_data, "getcolumnmap", model_instance);
				} else {
					PHALCON_INIT_NVAR(column_map);
				}
	
				if (Z_TYPE_P(column_map) == IS_ARRAY) { 
					if (phalcon_array_isset(column_map, first_primary_key)) {
						PHALCON_OBS_VAR(attribute_field);
						phalcon_array_fetch(&attribute_field, column_map, first_primary_key, PH_NOISY_CC);
					} else {
						PHALCON_INIT_VAR(exception_message);
						PHALCON_CONCAT_SVS(exception_message, "Column '", first_primary_key, "\" isn't part of the column map");
						PHALCON_THROW_EXCEPTION_ZVAL(phalcon_mvc_model_exception_ce, exception_message);
						return;
					}
				} else {
					PHALCON_CPY_WRT(attribute_field, first_primary_key);
				}
	
				PHALCON_INIT_VAR(primary_key_condition);
				PHALCON_CONCAT_SVSVSV(primary_key_condition, "[", model, "].[", attribute_field, "] = ", conditions);
				PHALCON_CPY_WRT(conditions, primary_key_condition);
	
				ZVAL_BOOL(no_primary, 0);
			}
		}
	
		/** 
		 * A primary key is mandatory in these cases
		 */
		if (PHALCON_IS_TRUE(no_primary)) {
			PHALCON_THROW_EXCEPTION_STR(phalcon_mvc_model_exception_ce, "Source related to this model does not have a primary key defined");
			return;
		}
	}
	
	PHALCON_INIT_VAR(phql);
	ZVAL_STRING(phql, "SELECT ", 1);
	
	PHALCON_OBS_VAR(columns);
	phalcon_read_property(&columns, this_ptr, SL("_columns"), PH_NOISY_CC);
	if (Z_TYPE_P(columns) != IS_NULL) {
		if (Z_TYPE_P(columns) == IS_ARRAY) { 
	
			PHALCON_INIT_VAR(selected_columns);
			array_init(selected_columns);
	
			if (!phalcon_is_iterable(columns, &ah0, &hp0, 0, 0 TSRMLS_CC)) {
				return;
			}
	
			while (zend_hash_get_current_data_ex(ah0, (void**) &hd, &hp0) == SUCCESS) {
	
				PHALCON_GET_FOREACH_KEY(alias, ah0, hp0);
				PHALCON_GET_FOREACH_VALUE(column);
	
				if (Z_TYPE_P(alias) == IS_LONG) {
					phalcon_array_append(&selected_columns, column, PH_SEPARATE TSRMLS_CC);
				} else {
					PHALCON_INIT_NVAR(aliased_column);
					PHALCON_CONCAT_VSV(aliased_column, column, " AS ", alias);
					phalcon_array_append(&selected_columns, aliased_column, PH_SEPARATE TSRMLS_CC);
				}
	
				zend_hash_move_forward_ex(ah0, &hp0);
			}
	
			PHALCON_INIT_VAR(joined_columns);
			phalcon_fast_join_str(joined_columns, SL(", "), selected_columns TSRMLS_CC);
			phalcon_concat_self(&phql, joined_columns TSRMLS_CC);
		} else {
			phalcon_concat_self(&phql, columns TSRMLS_CC);
		}
	} else {
		if (Z_TYPE_P(models) == IS_ARRAY) { 
	
			PHALCON_INIT_NVAR(selected_columns);
			array_init(selected_columns);
	
			if (!phalcon_is_iterable(models, &ah1, &hp1, 0, 0 TSRMLS_CC)) {
				return;
			}
	
			while (zend_hash_get_current_data_ex(ah1, (void**) &hd, &hp1) == SUCCESS) {
	
				PHALCON_GET_FOREACH_KEY(alias, ah1, hp1);
				PHALCON_GET_FOREACH_VALUE(model);
	
				if (Z_TYPE_P(alias) == IS_LONG) {
					PHALCON_INIT_NVAR(selected_column);
					PHALCON_CONCAT_SVS(selected_column, "[", model, "].*");
				} else {
					PHALCON_INIT_NVAR(selected_column);
					PHALCON_CONCAT_SVS(selected_column, "[", alias, "].*");
				}
				phalcon_array_append(&selected_columns, selected_column, PH_SEPARATE TSRMLS_CC);
	
				zend_hash_move_forward_ex(ah1, &hp1);
			}
	
			PHALCON_INIT_NVAR(joined_columns);
			phalcon_fast_join_str(joined_columns, SL(", "), selected_columns TSRMLS_CC);
			phalcon_concat_self(&phql, joined_columns TSRMLS_CC);
		} else {
			PHALCON_SCONCAT_SVS(phql, "[", models, "].*");
		}
	}
	
	/** 
	 * Join multiple models or use a single one if it is a string
	 */
	if (Z_TYPE_P(models) == IS_ARRAY) { 
	
		PHALCON_INIT_VAR(selected_models);
		array_init(selected_models);
	
		if (!phalcon_is_iterable(models, &ah2, &hp2, 0, 0 TSRMLS_CC)) {
			return;
		}
	
		while (zend_hash_get_current_data_ex(ah2, (void**) &hd, &hp2) == SUCCESS) {
	
			PHALCON_GET_FOREACH_KEY(alias, ah2, hp2);
			PHALCON_GET_FOREACH_VALUE(model);
	
			if (Z_TYPE_P(alias) == IS_STRING) {
				PHALCON_INIT_NVAR(selected_model);
				PHALCON_CONCAT_SVSVS(selected_model, "[", model, "] AS [", alias, "]");
			} else {
				PHALCON_INIT_NVAR(selected_model);
				PHALCON_CONCAT_SVS(selected_model, "[", model, "]");
			}
			phalcon_array_append(&selected_models, selected_model, PH_SEPARATE TSRMLS_CC);
	
			zend_hash_move_forward_ex(ah2, &hp2);
		}
	
		PHALCON_INIT_VAR(joined_models);
		phalcon_fast_join_str(joined_models, SL(", "), selected_models TSRMLS_CC);
		PHALCON_SCONCAT_SV(phql, " FROM ", joined_models);
	} else {
		PHALCON_SCONCAT_SVS(phql, " FROM [", models, "]");
	}
	
	/** 
	 * Check if joins were passed to the builders
	 */
	PHALCON_OBS_VAR(joins);
	phalcon_read_property(&joins, this_ptr, SL("_joins"), PH_NOISY_CC);
	if (Z_TYPE_P(joins) == IS_ARRAY) { 
	
		if (!phalcon_is_iterable(joins, &ah3, &hp3, 0, 0 TSRMLS_CC)) {
			return;
		}
	
		while (zend_hash_get_current_data_ex(ah3, (void**) &hd, &hp3) == SUCCESS) {
	
			PHALCON_GET_FOREACH_VALUE(join);
	
			/** 
			 * The joined table is in the first place of the array
			 */
			PHALCON_OBS_NVAR(join_model);
			phalcon_array_fetch_long(&join_model, join, 0, PH_NOISY_CC);
	
			/** 
			 * The join conditions are in the second place of the array
			 */
			PHALCON_OBS_NVAR(join_conditions);
			phalcon_array_fetch_long(&join_conditions, join, 1, PH_NOISY_CC);
	
			/** 
			 * The join alias is in the second place of the array
			 */
			PHALCON_OBS_NVAR(join_alias);
			phalcon_array_fetch_long(&join_alias, join, 2, PH_NOISY_CC);
	
			/** 
			 * Join type
			 */
			PHALCON_OBS_NVAR(join_type);
			phalcon_array_fetch_long(&join_type, join, 3, PH_NOISY_CC);
	
			/** 
			 * Create the join according to the type
			 */
			if (zend_is_true(join_type)) {
				PHALCON_SCONCAT_VSVS(phql, join_type, " JOIN [", join_model, "]");
			} else {
				PHALCON_SCONCAT_SVS(phql, " JOIN [", join_model, "]");
			}
	
			/** 
			 * Alias comes first
			 */
			if (zend_is_true(join_alias)) {
				PHALCON_SCONCAT_SVS(phql, " AS [", join_alias, "]");
			}
	
			/** 
			 * Conditions then
			 */
			if (zend_is_true(join_conditions)) {
				PHALCON_SCONCAT_SV(phql, " ON ", join_conditions);
			}
	
			zend_hash_move_forward_ex(ah3, &hp3);
		}
	
	}
	
	if (Z_TYPE_P(conditions) == IS_STRING) {
		PHALCON_SCONCAT_SV(phql, " WHERE ", conditions);
	}
	
	/** 
	 * Process group parameters
	 */
	PHALCON_OBS_VAR(group);
	phalcon_read_property(&group, this_ptr, SL("_group"), PH_NOISY_CC);
	if (Z_TYPE_P(group) != IS_NULL) {
		if (Z_TYPE_P(group) == IS_ARRAY) { 
	
			PHALCON_INIT_VAR(group_items);
			array_init(group_items);
	
			if (!phalcon_is_iterable(group, &ah4, &hp4, 0, 0 TSRMLS_CC)) {
				return;
			}
	
			while (zend_hash_get_current_data_ex(ah4, (void**) &hd, &hp4) == SUCCESS) {
	
				PHALCON_GET_FOREACH_VALUE(group_item);
	
				if (phalcon_is_numeric(group_item)) {
					phalcon_array_append(&group_items, group_item, PH_SEPARATE TSRMLS_CC);
				} else {
					if (phalcon_memnstr_str(group_item, SL(".") TSRMLS_CC)) {
						phalcon_array_append(&group_items, group_item, PH_SEPARATE TSRMLS_CC);
					} else {
						PHALCON_INIT_NVAR(escaped_item);
						PHALCON_CONCAT_SVS(escaped_item, "[", group_item, "]");
						phalcon_array_append(&group_items, escaped_item, PH_SEPARATE TSRMLS_CC);
					}
				}
	
				zend_hash_move_forward_ex(ah4, &hp4);
			}
	
			PHALCON_INIT_VAR(joined_items);
			phalcon_fast_join_str(joined_items, SL(", "), group_items TSRMLS_CC);
			PHALCON_SCONCAT_SV(phql, " GROUP BY ", joined_items);
		} else {
			if (phalcon_is_numeric(group)) {
				PHALCON_SCONCAT_SV(phql, " GROUP BY ", group);
			} else {
				if (phalcon_memnstr_str(group, SL(".") TSRMLS_CC)) {
					PHALCON_SCONCAT_SV(phql, " GROUP BY ", group);
				} else {
					PHALCON_SCONCAT_SVS(phql, " GROUP BY [", group, "]");
				}
			}
		}
	
		PHALCON_OBS_VAR(having);
		phalcon_read_property(&having, this_ptr, SL("_having"), PH_NOISY_CC);
		if (Z_TYPE_P(having) != IS_NULL) {
			PHALCON_SCONCAT_SV(phql, " HAVING ", having);
		}
	}
	
	/** 
	 * Process order clause
	 */
	PHALCON_OBS_VAR(order);
	phalcon_read_property(&order, this_ptr, SL("_order"), PH_NOISY_CC);
	if (Z_TYPE_P(order) != IS_NULL) {
		if (Z_TYPE_P(order) == IS_ARRAY) { 
	
			PHALCON_INIT_VAR(order_items);
			array_init(order_items);
	
			if (!phalcon_is_iterable(order, &ah5, &hp5, 0, 0 TSRMLS_CC)) {
				return;
			}
	
			while (zend_hash_get_current_data_ex(ah5, (void**) &hd, &hp5) == SUCCESS) {
	
				PHALCON_GET_FOREACH_VALUE(order_item);
	
				if (phalcon_is_numeric(order_item)) {
					phalcon_array_append(&order_items, order_item, PH_SEPARATE TSRMLS_CC);
				} else {
					if (phalcon_memnstr_str(order_item, SL(".") TSRMLS_CC)) {
						phalcon_array_append(&order_items, order_item, PH_SEPARATE TSRMLS_CC);
					} else {
						PHALCON_INIT_NVAR(escaped_item);
						PHALCON_CONCAT_SVS(escaped_item, "[", order_item, "]");
						phalcon_array_append(&order_items, escaped_item, PH_SEPARATE TSRMLS_CC);
					}
				}
	
				zend_hash_move_forward_ex(ah5, &hp5);
			}
	
			PHALCON_INIT_NVAR(joined_items);
			phalcon_fast_join_str(joined_items, SL(", "), order_items TSRMLS_CC);
			PHALCON_SCONCAT_SV(phql, " ORDER BY ", joined_items);
		} else {
			PHALCON_SCONCAT_SV(phql, " ORDER BY ", order);
		}
	}
	
	/** 
	 * Process limit parameters
	 */
	PHALCON_OBS_VAR(limit);
	phalcon_read_property(&limit, this_ptr, SL("_limit"), PH_NOISY_CC);
	if (Z_TYPE_P(limit) != IS_NULL) {
		if (Z_TYPE_P(limit) == IS_ARRAY) { 
	
			PHALCON_OBS_VAR(number);
			phalcon_array_fetch_string(&number, limit, SL("number"), PH_NOISY_CC);
			if (phalcon_array_isset_string(limit, SS("offset"))) {
	
				PHALCON_OBS_VAR(offset);
				phalcon_array_fetch_string(&offset, limit, SL("offset"), PH_NOISY_CC);
				if (phalcon_is_numeric(offset)) {
					PHALCON_SCONCAT_SVSV(phql, " LIMIT ", number, " OFFSET ", offset);
				} else {
					PHALCON_SCONCAT_SVS(phql, " LIMIT ", number, " OFFSET 0");
				}
			} else {
				PHALCON_SCONCAT_SV(phql, " LIMIT ", number);
			}
		} else {
			if (phalcon_is_numeric(limit)) {
				PHALCON_SCONCAT_SV(phql, " LIMIT ", limit);
	
				PHALCON_OBS_NVAR(offset);
				phalcon_read_property(&offset, this_ptr, SL("_offset"), PH_NOISY_CC);
				if (Z_TYPE_P(offset) != IS_NULL) {
					if (phalcon_is_numeric(offset)) {
						PHALCON_SCONCAT_SV(phql, " OFFSET ", offset);
					} else {
						phalcon_concat_self_str(&phql, SL(" OFFSET 0") TSRMLS_CC);
					}
				}
			}
		}
	}
	
	
	RETURN_CTOR(phql);
}
Пример #11
0
/**
 * Sends the cookie to the HTTP client
 * Stores the cookie definition in session
 *
 * @return Phalcon\Http\Cookie
 */
PHP_METHOD(Phalcon_Http_Cookie, send){

	zval *name, *value, *expire, *domain, *path, *secure;
	zval *http_only, *dependency_injector, *definition;
	zval *service = NULL, *session, *key, *encryption, *crypt;
	zval *encrypt_value = NULL, *has_session;

	PHALCON_MM_GROW();

	PHALCON_OBS_VAR(name);
	phalcon_read_property_this(&name, this_ptr, SL("_name"), PH_NOISY_CC);
	
	PHALCON_OBS_VAR(value);
	phalcon_read_property_this(&value, this_ptr, SL("_value"), PH_NOISY_CC);
	
	PHALCON_OBS_VAR(expire);
	phalcon_read_property_this(&expire, this_ptr, SL("_expire"), PH_NOISY_CC);
	
	PHALCON_OBS_VAR(domain);
	phalcon_read_property_this(&domain, this_ptr, SL("_domain"), PH_NOISY_CC);
	
	PHALCON_OBS_VAR(path);
	phalcon_read_property_this(&path, this_ptr, SL("_path"), PH_NOISY_CC);
	
	PHALCON_OBS_VAR(secure);
	phalcon_read_property_this(&secure, this_ptr, SL("_secure"), PH_NOISY_CC);
	
	PHALCON_OBS_VAR(http_only);
	phalcon_read_property_this(&http_only, this_ptr, SL("_httpOnly"), PH_NOISY_CC);
	
	PHALCON_OBS_VAR(dependency_injector);
	phalcon_read_property_this(&dependency_injector, this_ptr, SL("_dependencyInjector"), PH_NOISY_CC);
	if (Z_TYPE_P(dependency_injector) == IS_OBJECT) {
		PHALCON_INIT_VAR(service);
		PHALCON_INIT_VAR(has_session);
		ZVAL_STRING(service, "session", 1);

		phalcon_call_method_p1(has_session, dependency_injector, "has", service);
		if (zend_is_true(has_session)) {
			PHALCON_INIT_VAR(definition);
			array_init(definition);
			if (!PHALCON_IS_LONG(expire, 0)) {
				phalcon_array_update_string(&definition, SL("expire"), &expire, PH_COPY);
			}

			if (PHALCON_IS_NOT_EMPTY(path)) {
				phalcon_array_update_string(&definition, SL("path"), &path, PH_COPY);
			}

			if (PHALCON_IS_NOT_EMPTY(domain)) {
				phalcon_array_update_string(&definition, SL("domain"), &domain, PH_COPY);
			}

			if (PHALCON_IS_NOT_EMPTY(secure)) {
				phalcon_array_update_string(&definition, SL("secure"), &secure, PH_COPY);
			}

			if (PHALCON_IS_NOT_EMPTY(http_only)) {
				phalcon_array_update_string(&definition, SL("httpOnly"), &http_only, PH_COPY);
			}

			/**
			 * The definition is stored in session
			 */
			if (phalcon_fast_count_ev(definition TSRMLS_CC)) {

				PHALCON_INIT_VAR(session);
				phalcon_call_method_p1(session, dependency_injector, "getshared", service);

				if (Z_TYPE_P(session) != IS_NULL) {
					PHALCON_INIT_VAR(key);
					PHALCON_CONCAT_SV(key, "_PHCOOKIE_", name);
					phalcon_call_method_p2_noret(session, "set", key, definition);
				}
			}
		}
	}
	
	PHALCON_OBS_VAR(encryption);
	phalcon_read_property_this(&encryption, this_ptr, SL("_useEncryption"), PH_NOISY_CC);
	if (zend_is_true(encryption) && PHALCON_IS_NOT_EMPTY(value)) {
		if (Z_TYPE_P(dependency_injector) != IS_OBJECT) {
			PHALCON_THROW_EXCEPTION_STR(phalcon_http_response_exception_ce, "A dependency injection object is required to access the 'filter' service");
			return;
		}

		PHALCON_INIT_NVAR(service);
		ZVAL_STRING(service, "crypt", 1);

		PHALCON_INIT_VAR(crypt);
		phalcon_call_method_p1(crypt, dependency_injector, "getshared", service);

		/**
		 * Encrypt the value also coding it with base64
		 */
		PHALCON_INIT_VAR(encrypt_value);
		phalcon_call_method_p1(encrypt_value, crypt, "encryptbase64", value);
	} else {
		PHALCON_CPY_WRT(encrypt_value, value);
	}
	
	/** 
	 * Sets the cookie using the standard 'setcookie' function
	 */
	convert_to_string_ex(&name);
	convert_to_string_ex(&encrypt_value);
	convert_to_long_ex(&expire);
	convert_to_string_ex(&path);
	convert_to_string_ex(&domain);
	convert_to_long_ex(&secure);
	convert_to_long_ex(&http_only);

	php_setcookie(
		Z_STRVAL_P(name), Z_STRLEN_P(name),
		Z_STRVAL_P(encrypt_value), Z_STRLEN_P(encrypt_value),
		Z_LVAL_P(expire),
		Z_STRVAL_P(path), Z_STRLEN_P(path),
		Z_STRVAL_P(domain), Z_STRLEN_P(domain),
		Z_LVAL_P(secure),
		1,
		Z_LVAL_P(http_only) TSRMLS_CC
	);
	
	RETURN_THIS();
}
Пример #12
0
/**
 * Shows a backtrace item
 *
 * @param int $n
 * @param array $trace
 */
PHP_METHOD(Phalcon_Debug, showTraceItem){

	zval *n, *trace, *link_format, *space, *two_spaces, *underscore;
	zval *minus, *html, *class_name;
	zval *namespace_separator, *prepare_uri_class;
	zval *lower_class_name, *prepared_function_name;
	zval *prepare_internal_class, *type, *function_name = NULL;
	zval *trace_args, *arguments, *argument = NULL, *dumped_argument = NULL;
	zval *span_argument = NULL, *joined_arguments, *z_one;
	zval *file, *line, *show_files, *lines = NULL, *number_lines;
	zval *show_file_fragment, *before_context, *before_line;
	zval *first_line = NULL, *after_context, *after_line, *last_line = NULL;
	zval *comment_pattern, *charset, *tab;
	zval *comment, *i = NULL, *line_position = NULL, *current_line = NULL;
	zval *trimmed = NULL, *is_comment = NULL, *spaced_current_line = NULL;
	zval *escaped_line = NULL, *formatted_file = NULL;
	HashTable *ah0;
	HashPosition hp0;
	zval **hd;

	PHALCON_MM_GROW();

	phalcon_fetch_params(1, 3, 0, &n, &trace, &link_format);

	PHALCON_INIT_VAR(space);
	ZVAL_STRING(space, " ", 1);

	PHALCON_INIT_VAR(two_spaces);
	ZVAL_STRING(two_spaces, "  ", 1);

	PHALCON_INIT_VAR(underscore);
	ZVAL_STRING(underscore, "_", 1);

	PHALCON_INIT_VAR(minus);
	ZVAL_STRING(minus, "-", 1);

	/** 
	 * Every trace in the backtrace have a unique number
	 */
	PHALCON_INIT_VAR(html);
	PHALCON_CONCAT_SVS(html, "<tr><td align=\"right\" valign=\"top\" class=\"error-number\">#", n, "</td><td>");
	if (phalcon_array_isset_string(trace, SS("class"))) {
		zend_class_entry *class_ce;

		PHALCON_OBS_VAR(class_name);
		phalcon_array_fetch_string(&class_name, trace, SL("class"), PH_NOISY);

		class_ce = zend_fetch_class(Z_STRVAL_P(class_name), Z_STRLEN_P(class_name), ZEND_FETCH_CLASS_AUTO | ZEND_FETCH_CLASS_SILENT TSRMLS_CC);

		if (!class_ce) {
			/* Unable to load the class, should never happen */
		}
		else if (is_phalcon_class(class_ce)) {
			PHALCON_INIT_VAR(namespace_separator);
			ZVAL_STRING(namespace_separator, "\\", 1);

			/* Prepare the class name according to the Phalcon's conventions */
			PHALCON_INIT_VAR(prepare_uri_class);
			phalcon_fast_str_replace(prepare_uri_class, namespace_separator, underscore, class_name);

			/* Generate a link to the official docs */
			PHALCON_SCONCAT_SVSVS(html, "<span class=\"error-class\"><a target=\"_new\" href=\"http://docs.phalconphp.com/en/latest/api/", prepare_uri_class, ".html\">", class_name, "</a></span>");
		} else if (class_ce->type == ZEND_INTERNAL_CLASS) {
			PHALCON_INIT_VAR(lower_class_name);
			phalcon_fast_strtolower(lower_class_name, class_name);

			PHALCON_INIT_VAR(prepare_internal_class);
			phalcon_fast_str_replace(prepare_internal_class, underscore, minus, lower_class_name);

			/* Generate a link to the official docs */
			PHALCON_SCONCAT_SVSVS(html, "<span class=\"error-class\"><a target=\"_new\" href=\"http://php.net/manual/en/class.", prepare_internal_class, ".php\">", class_name, "</a></span>");
		} else {
			PHALCON_SCONCAT_SVS(html, "<span class=\"error-class\">", class_name, "</span>");
		}

		/** 
		 * Object access operator: static/instance
		 */
		PHALCON_OBS_VAR(type);
		phalcon_array_fetch_string(&type, trace, SL("type"), PH_NOISY);
		phalcon_concat_self(&html, type TSRMLS_CC);
	}

	/** 
	 * Normally the backtrace contains only classes
	 */
	if (phalcon_array_isset_string(trace, SS("class"))) {
		PHALCON_OBS_VAR(function_name);
		phalcon_array_fetch_string(&function_name, trace, SL("function"), PH_NOISY);
		PHALCON_SCONCAT_SVS(html, "<span class=\"error-function\">", function_name, "</span>");
	} else {
		zend_function *func;

		PHALCON_OBS_NVAR(function_name);
		phalcon_array_fetch_string(&function_name, trace, SL("function"), PH_NOISY);
		convert_to_string(function_name);

		/** 
		 * Check if the function exists
		 */
		if (phalcon_fetch_function(&func, Z_STRVAL_P(function_name), Z_STRLEN_P(function_name) TSRMLS_CC) == SUCCESS) {

			/** 
			 * Internal functions links to the PHP documentation
			 */
			if (func->type == ZEND_INTERNAL_FUNCTION) {
				/** 
				 * Prepare function's name according to the conventions in the docs
				 */
				PHALCON_INIT_VAR(prepared_function_name);
				phalcon_fast_str_replace(prepared_function_name, underscore, minus, function_name);
				PHALCON_SCONCAT_SVSVS(html, "<span class=\"error-function\"><a target=\"_new\" href=\"http://php.net/manual/en/function.", prepared_function_name, ".php\">", function_name, "</a></span>");
			} else {
				PHALCON_SCONCAT_SVS(html, "<span class=\"error-function\">", function_name, "</span>");
			}
		} else {
			PHALCON_SCONCAT_SVS(html, "<span class=\"error-function\">", function_name, "</span>");
		}
	}

	/** 
	 * Check for arguments in the function
	 */
	if (phalcon_array_isset_string(trace, SS("args"))) {

		PHALCON_OBS_VAR(trace_args);
		phalcon_array_fetch_string(&trace_args, trace, SL("args"), PH_NOISY);
		if (phalcon_fast_count_ev(trace_args TSRMLS_CC)) {

			PHALCON_INIT_VAR(arguments);
			array_init(arguments);

			phalcon_is_iterable(trace_args, &ah0, &hp0, 0, 0);

			while (zend_hash_get_current_data_ex(ah0, (void**) &hd, &hp0) == SUCCESS) {

				PHALCON_GET_HVALUE(argument);

				/** 
				 * Every argument is generated using _getVarDump
				 */
				PHALCON_CALL_METHOD(&dumped_argument, this_ptr, "_getvardump", argument);

				PHALCON_INIT_NVAR(span_argument);
				PHALCON_CONCAT_SVS(span_argument, "<span class=\"error-parameter\">", dumped_argument, "</span>");

				/** 
				 * Append the HTML generated to the argument's list
				 */
				phalcon_array_append(&arguments, span_argument, PH_COPY);

				zend_hash_move_forward_ex(ah0, &hp0);
			}

			/** 
			 * Join all the arguments
			 */
			PHALCON_INIT_VAR(joined_arguments);
			phalcon_fast_join_str(joined_arguments, SL(", "), arguments TSRMLS_CC);
			PHALCON_SCONCAT_SVS(html, "(", joined_arguments, ")");
		} else {
			phalcon_concat_self_str(&html, SL("()") TSRMLS_CC);
		}
	}

	/** 
	 * When 'file' is present, it usually means the function is provided by the user
	 */
	if (phalcon_array_isset_string(trace, SS("file"))) {

		z_one = PHALCON_GLOBAL(z_one);

		PHALCON_OBS_VAR(file);
		phalcon_array_fetch_string(&file, trace, SL("file"), PH_NOISY);

		PHALCON_OBS_VAR(line);
		phalcon_array_fetch_string(&line, trace, SL("line"), PH_NOISY);

		PHALCON_CALL_METHOD(&formatted_file, getThis(), "getfilelink", file, line, link_format);

		/** 
		 * Realpath to the file and its line using a special header
		 */
		PHALCON_SCONCAT_SVSVS(html, "<br/><div class=\"error-file\">", formatted_file, " (", line, ")</div>");

		PHALCON_OBS_VAR(show_files);
		phalcon_read_property_this(&show_files, this_ptr, SL("_showFiles"), PH_NOISY TSRMLS_CC);

		/** 
		 * The developer can change if the files must be opened or not
		 */
		if (zend_is_true(show_files)) {

			/** 
			 * Open the file to an array using 'file', this respects the openbase-dir directive
			 */
			PHALCON_CALL_FUNCTION(&lines, "file", file);

			PHALCON_INIT_VAR(number_lines);
			phalcon_fast_count(number_lines, lines TSRMLS_CC);

			PHALCON_OBS_VAR(show_file_fragment);
			phalcon_read_property_this(&show_file_fragment, this_ptr, SL("_showFileFragment"), PH_NOISY TSRMLS_CC);

			/** 
			 * File fragments just show a piece of the file where the exception is located
			 */
			if (zend_is_true(show_file_fragment)) {

				/** 
				 * Take lines back to the current exception's line
				 */
				before_context = phalcon_fetch_nproperty_this(getThis(), SL("_beforeContext"), PH_NOISY TSRMLS_CC);

				PHALCON_INIT_VAR(before_line);
				phalcon_sub_function(before_line, line, before_context);

				/** 
				 * Check for overflows
				 */
				if (PHALCON_LT_LONG(before_line, 1)) {
					PHALCON_CPY_WRT_CTOR(first_line, z_one);
				} else {
					PHALCON_CPY_WRT(first_line, before_line);
				}

				/** 
				 * Take lines after the current exception's line
				 */
				after_context = phalcon_fetch_nproperty_this(getThis(), SL("_afterContext"), PH_NOISY TSRMLS_CC);

				PHALCON_INIT_VAR(after_line);
				phalcon_add_function(after_line, line, after_context);

				/** 
				 * Check for overflows
				 */
				if (PHALCON_GT(after_line, number_lines)) {
					PHALCON_CPY_WRT(last_line, number_lines);
				} else {
					PHALCON_CPY_WRT(last_line, after_line);
				}

				PHALCON_SCONCAT_SVSVSVS(html, "<pre class='prettyprint highlight:", first_line, ":", line, " linenums:", first_line, "'>");
			} else {
				PHALCON_CPY_WRT_CTOR(first_line, z_one);
				PHALCON_CPY_WRT(last_line, number_lines);
				PHALCON_SCONCAT_SVSVS(html, "<pre class='prettyprint highlight:", first_line, ":", line, " linenums error-scroll'>");
			}

			PHALCON_INIT_VAR(comment_pattern);
			ZVAL_STRING(comment_pattern, "#\\*\\/$#", 1);

			charset = phalcon_fetch_static_property_ce(phalcon_debug_ce, SL("_charset") TSRMLS_CC);

			PHALCON_INIT_VAR(tab);
			ZVAL_STRING(tab, "\t", 1);

			PHALCON_INIT_VAR(comment);
			ZVAL_STRING(comment, "* /", 1);
			PHALCON_CPY_WRT(i, first_line);

			while (PHALCON_LE(i, last_line)) {

				/** 
				 * Current line in the file
				 */
				PHALCON_INIT_NVAR(line_position);
				phalcon_sub_function(line_position, i, z_one);

				/** 
				 * Current line content in the piece of file
				 */
				PHALCON_OBS_NVAR(current_line);
				phalcon_array_fetch(&current_line, lines, line_position, PH_NOISY);

				/** 
				 * File fragments are cleaned, removing tabs and comments
				 */
				if (zend_is_true(show_file_fragment)) {
					if (PHALCON_IS_EQUAL(i, first_line)) {

						PHALCON_INIT_NVAR(trimmed);
						phalcon_fast_trim(trimmed, current_line, NULL, PHALCON_TRIM_RIGHT TSRMLS_CC);

						PHALCON_INIT_NVAR(is_comment);

						RETURN_MM_ON_FAILURE(phalcon_preg_match(is_comment, comment_pattern, current_line, NULL TSRMLS_CC));

						if (zend_is_true(is_comment)) {
							PHALCON_INIT_NVAR(spaced_current_line);
							phalcon_fast_str_replace(spaced_current_line, comment, space, current_line);
							PHALCON_CPY_WRT(current_line, spaced_current_line);
						}
					}
				}

				/** 
				 * Print a non break space if the current line is a line break, this allows to show
				 * the html zebra properly
				 */
				if (PHALCON_IS_STRING(current_line, "\n")) {
					phalcon_concat_self_str(&html, SL("&nbsp;\n") TSRMLS_CC);
				} else {
					if (PHALCON_IS_STRING(current_line, "\r\n")) {
						phalcon_concat_self_str(&html, SL("&nbsp;\n") TSRMLS_CC);
					} else {
						PHALCON_INIT_NVAR(spaced_current_line);
						phalcon_fast_str_replace(spaced_current_line, tab, two_spaces, current_line);

						PHALCON_INIT_NVAR(escaped_line);
						phalcon_htmlentities(escaped_line, spaced_current_line, NULL, charset TSRMLS_CC);
						phalcon_concat_self(&html, escaped_line TSRMLS_CC);
					}
				}

				phalcon_increment(i);
			}
			phalcon_concat_self_str(&html, SL("</pre>") TSRMLS_CC);
		}
	}

	phalcon_concat_self_str(&html, SL("</td></tr>") TSRMLS_CC);

	RETURN_CTOR(html);
}
Пример #13
0
/**
 * Returns the translation related to the given key
 *
 * @param string $index
 * @param array $placeholders
 * @return string
 */
PHP_METHOD(Phalcon_Translate_Adapter_NativeArray, query){

	zval *index, *placeholders = NULL, *translate, *translation = NULL;
	zval *value = NULL, *key = NULL, *key_placeholder = NULL, *replaced = NULL;
	HashTable *ah0;
	HashPosition hp0;
	zval **hd;
	char *hash_index;
	uint hash_index_len;
	ulong hash_num;
	int hash_type;
	int eval_int;

	PHALCON_MM_GROW();

	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z|z", &index, &placeholders) == FAILURE) {
		PHALCON_MM_RESTORE();
		RETURN_NULL();
	}

	if (!placeholders) {
		PHALCON_INIT_NVAR(placeholders);
	}
	
	PHALCON_INIT_VAR(translate);
	phalcon_read_property(&translate, this_ptr, SL("_translate"), PH_NOISY_CC);
	eval_int = phalcon_array_isset(translate, index);
	if (eval_int) {
		PHALCON_INIT_VAR(translation);
		phalcon_array_fetch(&translation, translate, index, PH_NOISY_CC);
		if (Z_TYPE_P(placeholders) == IS_ARRAY) { 
			if (phalcon_fast_count_ev(placeholders TSRMLS_CC)) {
				
				if (!phalcon_valid_foreach(placeholders TSRMLS_CC)) {
					return;
				}
				
				ah0 = Z_ARRVAL_P(placeholders);
				zend_hash_internal_pointer_reset_ex(ah0, &hp0);
				
				ph_cycle_start_0:
				
					if (zend_hash_get_current_data_ex(ah0, (void**) &hd, &hp0) != SUCCESS) {
						goto ph_cycle_end_0;
					}
					
					PHALCON_GET_FOREACH_KEY(key, ah0, hp0);
					PHALCON_GET_FOREACH_VALUE(value);
					
					PHALCON_INIT_NVAR(key_placeholder);
					PHALCON_CONCAT_SVS(key_placeholder, "%", key, "%");
					
					PHALCON_INIT_NVAR(replaced);
					phalcon_fast_str_replace(replaced, key_placeholder, value, translation TSRMLS_CC);
					PHALCON_CPY_WRT(translation, replaced);
					
					zend_hash_move_forward_ex(ah0, &hp0);
					goto ph_cycle_start_0;
					
				ph_cycle_end_0:
				if(0){}
				
			}
		}
		
		
		RETURN_CCTOR(translation);
	}
	
	
	RETURN_CCTOR(index);
}
Пример #14
0
/**
 * Builds a SELECT statement
 *
 * @param array $definition
 * @return string
 */
PHP_METHOD(Phalcon_Db_Dialect, select){

	zval *definition, *escape_char = NULL, *columns, *selected_columns;
	zval *column = NULL, *column_item = NULL, *column_sql = NULL, *columns_sql = NULL;
	zval *column_domain = NULL, *column_domain_sql = NULL, *column_alias = NULL;
	zval *column_alias_sql = NULL, *tables, *selected_tables;
	zval *table = NULL, *sql_table = NULL, *tables_sql = NULL, *sql, *joins;
	zval *join = NULL, *type = NULL, *sql_join = NULL, *join_conditions_array = NULL;
	zval *join_expressions = NULL, *join_condition = NULL, *join_expression = NULL;
	zval *join_conditions = NULL, *where_conditions;
	zval *where_expression, *group_items, *group_fields;
	zval *group_field = NULL, *group_expression = NULL, *group_sql;
	zval *group_clause, *having_conditions, *having_expression;
	zval *order_fields, *order_items, *order_item = NULL;
	zval *order_expression = NULL, *order_sql_item = NULL, *sql_order_type = NULL;
	zval *order_sql_item_type = NULL, *order_sql, *limit_value;
	zval *number, *offset;
	HashTable *ah0, *ah1, *ah2, *ah3, *ah4, *ah5;
	HashPosition hp0, hp1, hp2, hp3, hp4, hp5;
	zval **hd;

	PHALCON_MM_GROW();

	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &definition) == FAILURE) {
		RETURN_MM_NULL();
	}

	if (Z_TYPE_P(definition) != IS_ARRAY) { 
		PHALCON_THROW_EXCEPTION_STR(phalcon_db_exception_ce, "Invalid SELECT definition");
		return;
	}
	if (!phalcon_array_isset_string(definition, SS("tables"))) {
		PHALCON_THROW_EXCEPTION_STR(phalcon_db_exception_ce, "The index 'tables' is required in the definition array");
		return;
	}
	
	if (!phalcon_array_isset_string(definition, SS("columns"))) {
		PHALCON_THROW_EXCEPTION_STR(phalcon_db_exception_ce, "The index 'columns' is required in the definition array");
		return;
	}
	
	if (PHALCON_GLOBAL(db).escape_identifiers) {
		PHALCON_OBS_VAR(escape_char);
		phalcon_read_property(&escape_char, this_ptr, SL("_escapeChar"), PH_NOISY_CC);
	} else {
		PHALCON_INIT_NVAR(escape_char);
	}
	
	PHALCON_OBS_VAR(columns);
	phalcon_array_fetch_string(&columns, definition, SL("columns"), PH_NOISY_CC);
	if (Z_TYPE_P(columns) == IS_ARRAY) { 
	
		PHALCON_INIT_VAR(selected_columns);
		array_init(selected_columns);
	
		if (!phalcon_is_iterable(columns, &ah0, &hp0, 0, 0 TSRMLS_CC)) {
			return;
		}
	
		while (zend_hash_get_current_data_ex(ah0, (void**) &hd, &hp0) == SUCCESS) {
	
			PHALCON_GET_FOREACH_VALUE(column);
	
			/** 
			 * Escape column name
			 */
			PHALCON_OBS_NVAR(column_item);
			phalcon_array_fetch_long(&column_item, column, 0, PH_NOISY_CC);
			if (Z_TYPE_P(column_item) == IS_ARRAY) { 
				PHALCON_INIT_NVAR(column_sql);
				PHALCON_CALL_METHOD_PARAMS_2(column_sql, this_ptr, "getsqlexpression", column_item, escape_char);
			} else {
				if (PHALCON_IS_STRING(column_item, "*")) {
					PHALCON_CPY_WRT(column_sql, column_item);
				} else {
					if (PHALCON_GLOBAL(db).escape_identifiers) {
						PHALCON_INIT_NVAR(column_sql);
						PHALCON_CONCAT_VVV(column_sql, escape_char, column_item, escape_char);
					} else {
						PHALCON_CPY_WRT(columns_sql, column_item);
					}
				}
			}
	
			/** 
			 * Escape column domain
			 */
			PHALCON_OBS_NVAR(column_domain);
			phalcon_array_fetch_long(&column_domain, column, 1, PH_NOISY_CC);
			if (zend_is_true(column_domain)) {
				if (PHALCON_GLOBAL(db).escape_identifiers) {
					PHALCON_INIT_NVAR(column_domain_sql);
					PHALCON_CONCAT_VVVSV(column_domain_sql, escape_char, column_domain, escape_char, ".", column_sql);
				} else {
					PHALCON_INIT_NVAR(column_domain_sql);
					PHALCON_CONCAT_VSV(column_domain_sql, column_domain, ".", column_sql);
				}
			} else {
				PHALCON_CPY_WRT(column_domain_sql, column_sql);
			}
	
			/** 
			 * Escape column alias
			 */
			if (phalcon_array_isset_long(column, 2)) {
	
				PHALCON_OBS_NVAR(column_alias);
				phalcon_array_fetch_long(&column_alias, column, 2, PH_NOISY_CC);
				if (zend_is_true(column_alias)) {
					if (PHALCON_GLOBAL(db).escape_identifiers) {
						PHALCON_INIT_NVAR(column_alias_sql);
						PHALCON_CONCAT_VSVVV(column_alias_sql, column_domain_sql, " AS ", escape_char, column_alias, escape_char);
					} else {
						PHALCON_INIT_NVAR(column_alias_sql);
						PHALCON_CONCAT_VSV(column_alias_sql, column_domain_sql, " AS ", column_alias);
					}
				} else {
					PHALCON_CPY_WRT(column_alias_sql, column_domain_sql);
				}
			} else {
				PHALCON_CPY_WRT(column_alias_sql, column_domain_sql);
			}
	
			phalcon_array_append(&selected_columns, column_alias_sql, PH_SEPARATE TSRMLS_CC);
	
			zend_hash_move_forward_ex(ah0, &hp0);
		}
	
		PHALCON_INIT_VAR(columns_sql);
		phalcon_fast_join_str(columns_sql, SL(", "), selected_columns TSRMLS_CC);
	} else {
		PHALCON_CPY_WRT(columns_sql, columns);
	}
	
	/** 
	 * Check and escape tables
	 */
	PHALCON_OBS_VAR(tables);
	phalcon_array_fetch_string(&tables, definition, SL("tables"), PH_NOISY_CC);
	if (Z_TYPE_P(tables) == IS_ARRAY) { 
	
		PHALCON_INIT_VAR(selected_tables);
		array_init(selected_tables);
	
		if (!phalcon_is_iterable(tables, &ah1, &hp1, 0, 0 TSRMLS_CC)) {
			return;
		}
	
		while (zend_hash_get_current_data_ex(ah1, (void**) &hd, &hp1) == SUCCESS) {
	
			PHALCON_GET_FOREACH_VALUE(table);
	
			PHALCON_INIT_NVAR(sql_table);
			PHALCON_CALL_METHOD_PARAMS_2(sql_table, this_ptr, "getsqltable", table, escape_char);
			phalcon_array_append(&selected_tables, sql_table, PH_SEPARATE TSRMLS_CC);
	
			zend_hash_move_forward_ex(ah1, &hp1);
		}
	
		PHALCON_INIT_VAR(tables_sql);
		phalcon_fast_join_str(tables_sql, SL(", "), selected_tables TSRMLS_CC);
	} else {
		PHALCON_CPY_WRT(tables_sql, tables);
	}
	
	PHALCON_INIT_VAR(sql);
	PHALCON_CONCAT_SVSV(sql, "SELECT ", columns_sql, " FROM ", tables_sql);
	
	/** 
	 * Check for joins
	 */
	if (phalcon_array_isset_string(definition, SS("joins"))) {
	
		PHALCON_OBS_VAR(joins);
		phalcon_array_fetch_string(&joins, definition, SL("joins"), PH_NOISY_CC);
	
		if (!phalcon_is_iterable(joins, &ah2, &hp2, 0, 0 TSRMLS_CC)) {
			return;
		}
	
		while (zend_hash_get_current_data_ex(ah2, (void**) &hd, &hp2) == SUCCESS) {
	
			PHALCON_GET_FOREACH_VALUE(join);
	
			PHALCON_OBS_NVAR(type);
			phalcon_array_fetch_string(&type, join, SL("type"), PH_NOISY_CC);
	
			PHALCON_OBS_NVAR(table);
			phalcon_array_fetch_string(&table, join, SL("source"), PH_NOISY_CC);
	
			PHALCON_INIT_NVAR(sql_table);
			PHALCON_CALL_METHOD_PARAMS_2(sql_table, this_ptr, "getsqltable", table, escape_char);
			phalcon_array_append(&selected_tables, sql_table, PH_SEPARATE TSRMLS_CC);
	
			PHALCON_INIT_NVAR(sql_join);
			PHALCON_CONCAT_SVSV(sql_join, " ", type, " JOIN ", sql_table);
	
			/** 
			 * Check if the join has conditions
			 */
			if (phalcon_array_isset_string(join, SS("conditions"))) {
	
				PHALCON_OBS_NVAR(join_conditions_array);
				phalcon_array_fetch_string(&join_conditions_array, join, SL("conditions"), PH_NOISY_CC);
				if (phalcon_fast_count_ev(join_conditions_array TSRMLS_CC)) {
	
					PHALCON_INIT_NVAR(join_expressions);
					array_init(join_expressions);
	
					if (!phalcon_is_iterable(join_conditions_array, &ah3, &hp3, 0, 0 TSRMLS_CC)) {
						return;
					}
	
					while (zend_hash_get_current_data_ex(ah3, (void**) &hd, &hp3) == SUCCESS) {
	
						PHALCON_GET_FOREACH_VALUE(join_condition);
	
						PHALCON_INIT_NVAR(join_expression);
						PHALCON_CALL_METHOD_PARAMS_2(join_expression, this_ptr, "getsqlexpression", join_condition, escape_char);
						phalcon_array_append(&join_expressions, join_expression, PH_SEPARATE TSRMLS_CC);
	
						zend_hash_move_forward_ex(ah3, &hp3);
					}
	
					PHALCON_INIT_NVAR(join_conditions);
					phalcon_fast_join_str(join_conditions, SL(" AND "), join_expressions TSRMLS_CC);
					PHALCON_SCONCAT_SV(sql_join, " ON ", join_conditions);
				}
			}
	
			phalcon_concat_self(&sql, sql_join TSRMLS_CC);
	
			zend_hash_move_forward_ex(ah2, &hp2);
		}
	
	}
	
	/** 
	 * Check for a WHERE clause
	 */
	if (phalcon_array_isset_string(definition, SS("where"))) {
		PHALCON_OBS_VAR(where_conditions);
		phalcon_array_fetch_string(&where_conditions, definition, SL("where"), PH_NOISY_CC);
	
		PHALCON_INIT_VAR(where_expression);
		PHALCON_CALL_METHOD_PARAMS_2(where_expression, this_ptr, "getsqlexpression", where_conditions, escape_char);
		PHALCON_SCONCAT_SV(sql, " WHERE ", where_expression);
	}
	
	/** 
	 * Check for a GROUP clause
	 */
	if (phalcon_array_isset_string(definition, SS("group"))) {
	
		PHALCON_INIT_VAR(group_items);
		array_init(group_items);
	
		PHALCON_OBS_VAR(group_fields);
		phalcon_array_fetch_string(&group_fields, definition, SL("group"), PH_NOISY_CC);
	
		if (!phalcon_is_iterable(group_fields, &ah4, &hp4, 0, 0 TSRMLS_CC)) {
			return;
		}
	
		while (zend_hash_get_current_data_ex(ah4, (void**) &hd, &hp4) == SUCCESS) {
	
			PHALCON_GET_FOREACH_VALUE(group_field);
	
			PHALCON_INIT_NVAR(group_expression);
			PHALCON_CALL_METHOD_PARAMS_2(group_expression, this_ptr, "getsqlexpression", group_field, escape_char);
			phalcon_array_append(&group_items, group_expression, PH_SEPARATE TSRMLS_CC);
	
			zend_hash_move_forward_ex(ah4, &hp4);
		}
	
		PHALCON_INIT_VAR(group_sql);
		phalcon_fast_join_str(group_sql, SL(", "), group_items TSRMLS_CC);
	
		PHALCON_INIT_VAR(group_clause);
		PHALCON_CONCAT_SV(group_clause, " GROUP BY ", group_sql);
		phalcon_concat_self(&sql, group_clause TSRMLS_CC);
	
		/** 
		 * Check for a HAVING clause
		 */
		if (phalcon_array_isset_string(definition, SS("having"))) {
			PHALCON_OBS_VAR(having_conditions);
			phalcon_array_fetch_string(&having_conditions, definition, SL("having"), PH_NOISY_CC);
	
			PHALCON_INIT_VAR(having_expression);
			PHALCON_CALL_METHOD_PARAMS_2(having_expression, this_ptr, "getsqlexpression", having_conditions, escape_char);
			PHALCON_SCONCAT_SV(sql, " HAVING ", having_expression);
		}
	}
	
	/** 
	 * Check for a ORDER clause
	 */
	if (phalcon_array_isset_string(definition, SS("order"))) {
	
		PHALCON_OBS_VAR(order_fields);
		phalcon_array_fetch_string(&order_fields, definition, SL("order"), PH_NOISY_CC);
	
		PHALCON_INIT_VAR(order_items);
		array_init(order_items);
	
		if (!phalcon_is_iterable(order_fields, &ah5, &hp5, 0, 0 TSRMLS_CC)) {
			return;
		}
	
		while (zend_hash_get_current_data_ex(ah5, (void**) &hd, &hp5) == SUCCESS) {
	
			PHALCON_GET_FOREACH_VALUE(order_item);
	
			PHALCON_OBS_NVAR(order_expression);
			phalcon_array_fetch_long(&order_expression, order_item, 0, PH_NOISY_CC);
	
			PHALCON_INIT_NVAR(order_sql_item);
			PHALCON_CALL_METHOD_PARAMS_2(order_sql_item, this_ptr, "getsqlexpression", order_expression, escape_char);
	
			/** 
			 * In the numeric 1 position could be a ASC/DESC clause
			 */
			if (phalcon_array_isset_long(order_item, 1)) {
				PHALCON_OBS_NVAR(sql_order_type);
				phalcon_array_fetch_long(&sql_order_type, order_item, 1, PH_NOISY_CC);
	
				PHALCON_INIT_NVAR(order_sql_item_type);
				PHALCON_CONCAT_VSV(order_sql_item_type, order_sql_item, " ", sql_order_type);
			} else {
				PHALCON_CPY_WRT(order_sql_item_type, order_sql_item);
			}
	
			phalcon_array_append(&order_items, order_sql_item_type, PH_SEPARATE TSRMLS_CC);
	
			zend_hash_move_forward_ex(ah5, &hp5);
		}
	
		PHALCON_INIT_VAR(order_sql);
		phalcon_fast_join_str(order_sql, SL(", "), order_items TSRMLS_CC);
		PHALCON_SCONCAT_SV(sql, " ORDER BY ", order_sql);
	}
	
	/** 
	 * Check for a LIMIT condition
	 */
	if (phalcon_array_isset_string(definition, SS("limit"))) {
	
		PHALCON_OBS_VAR(limit_value);
		phalcon_array_fetch_string(&limit_value, definition, SL("limit"), PH_NOISY_CC);
		if (Z_TYPE_P(limit_value) == IS_ARRAY) { 
	
			PHALCON_OBS_VAR(number);
			phalcon_array_fetch_string(&number, limit_value, SL("number"), PH_NOISY_CC);
	
			/** 
			 * Check for a OFFSET condition
			 */
			if (phalcon_array_isset_string(limit_value, SS("offset"))) {
				PHALCON_OBS_VAR(offset);
				phalcon_array_fetch_string(&offset, limit_value, SL("offset"), PH_NOISY_CC);
				PHALCON_SCONCAT_SVSV(sql, " LIMIT ", number, " OFFSET ", offset);
			} else {
				PHALCON_SCONCAT_SV(sql, " LIMIT ", number);
			}
		} else {
			PHALCON_SCONCAT_SV(sql, " LIMIT ", limit_value);
		}
	}
	
	
	RETURN_CTOR(sql);
}
Пример #15
0
/**
 * Shows a backtrace item
 *
 * @param int $n
 * @param array $trace
 */
PHP_METHOD(Phalcon_Debug, showTraceItem){

	zval *n, *trace, *space, *two_spaces, *underscore;
	zval *minus, *html, *class_name, *pattern, *is_phalcon_class;
	zval *namespace_separator, *prepare_uri_class;
	zval *class_reflection, *is_internal = NULL, *lower_class_name;
	zval *prepare_internal_class, *type, *function_name = NULL;
	zval *function_reflection, *prepared_function_name;
	zval *trace_args, *arguments, *argument = NULL, *dumped_argument = NULL;
	zval *span_argument = NULL, *joined_arguments, *one;
	zval *file, *line, *show_files, *lines, *number_lines;
	zval *show_file_fragment, *seven, *before_line;
	zval *first_line = NULL, *five, *after_line, *last_line = NULL;
	zval *comment_pattern, *utf8, *ent_compat, *tab;
	zval *comment, *i = NULL, *line_position = NULL, *current_line = NULL;
	zval *trimmed = NULL, *is_comment = NULL, *spaced_current_line = NULL;
	zval *escaped_line = NULL;
	HashTable *ah0;
	HashPosition hp0;
	zval **hd;
	zend_class_entry *ce0, *ce1;

	PHALCON_MM_GROW();

	phalcon_fetch_params(1, 2, 0, &n, &trace);
	
	PHALCON_INIT_VAR(space);
	ZVAL_STRING(space, " ", 1);
	
	PHALCON_INIT_VAR(two_spaces);
	ZVAL_STRING(two_spaces, "  ", 1);
	
	PHALCON_INIT_VAR(underscore);
	ZVAL_STRING(underscore, "_", 1);
	
	PHALCON_INIT_VAR(minus);
	ZVAL_STRING(minus, "-", 1);
	
	/** 
	 * Every trace in the backtrace have a unique number
	 */
	PHALCON_INIT_VAR(html);
	PHALCON_CONCAT_SVS(html, "<tr><td align=\"right\" valign=\"top\" class=\"error-number\">#", n, "</td><td>");
	if (phalcon_array_isset_string(trace, SS("class"))) {
	
		PHALCON_OBS_VAR(class_name);
		phalcon_array_fetch_string(&class_name, trace, SL("class"), PH_NOISY);
	
		PHALCON_INIT_VAR(pattern);
		ZVAL_STRING(pattern, "/^Phalcon/", 1);
	
		PHALCON_INIT_VAR(is_phalcon_class);
	
		phalcon_preg_match(is_phalcon_class, pattern, class_name, NULL TSRMLS_CC);
	
		/** 
		 * We assume that classes starting by Phalcon are framework's classes
		 */
		if (zend_is_true(is_phalcon_class)) {
			PHALCON_INIT_VAR(namespace_separator);
			ZVAL_STRING(namespace_separator, "\\", 1);
	
			/** 
			 * Prepare the class name according to the Phalcon's conventions
			 */
			PHALCON_INIT_VAR(prepare_uri_class);
			phalcon_fast_str_replace(prepare_uri_class, namespace_separator, underscore, class_name);
	
			/** 
			 * Generate a link to the official docs
			 */
			PHALCON_SCONCAT_SVSVS(html, "<span class=\"error-class\"><a target=\"_new\" href=\"http://docs.phalconphp.com/en/latest/api/", prepare_uri_class, ".html\">", class_name, "</a></span>");
		} else {
			ce0 = zend_fetch_class(SL("ReflectionClass"), ZEND_FETCH_CLASS_AUTO TSRMLS_CC);
			PHALCON_INIT_VAR(class_reflection);
			object_init_ex(class_reflection, ce0);
			if (phalcon_has_constructor(class_reflection TSRMLS_CC)) {
				phalcon_call_method_p1_noret(class_reflection, "__construct", class_name);
			}
	
			/** 
			 * Check if classes are PHP's classes
			 */
			PHALCON_INIT_VAR(is_internal);
			phalcon_call_method(is_internal, class_reflection, "isinternal");
			if (zend_is_true(is_internal)) {
				PHALCON_INIT_VAR(lower_class_name);
				phalcon_fast_strtolower(lower_class_name, class_name);
	
				PHALCON_INIT_VAR(prepare_internal_class);
				phalcon_fast_str_replace(prepare_internal_class, underscore, minus, lower_class_name);
	
				/** 
				 * Generate a link to the official docs
				 */
				PHALCON_SCONCAT_SVSVS(html, "<span class=\"error-class\"><a target=\"_new\" href=\"http://php.net/manual/en/class.", prepare_internal_class, ".php\">", class_name, "</a></span>");
			} else {
				PHALCON_SCONCAT_SVS(html, "<span class=\"error-class\">", class_name, "</span>");
			}
		}
	
		/** 
		 * Object access operator: static/instance
		 */
		PHALCON_OBS_VAR(type);
		phalcon_array_fetch_string(&type, trace, SL("type"), PH_NOISY);
		phalcon_concat_self(&html, type TSRMLS_CC);
	}
	
	/** 
	 * Normally the backtrace contains only classes
	 */
	if (phalcon_array_isset_string(trace, SS("class"))) {
		PHALCON_OBS_VAR(function_name);
		phalcon_array_fetch_string(&function_name, trace, SL("function"), PH_NOISY);
		PHALCON_SCONCAT_SVS(html, "<span class=\"error-function\">", function_name, "</span>");
	} else {
		PHALCON_OBS_NVAR(function_name);
		phalcon_array_fetch_string(&function_name, trace, SL("function"), PH_NOISY);
	
		/** 
		 * Check if the function exists
		 */
		if (phalcon_function_exists(function_name TSRMLS_CC) == SUCCESS) {
			ce1 = zend_fetch_class(SL("ReflectionFunction"), ZEND_FETCH_CLASS_AUTO TSRMLS_CC);
			PHALCON_INIT_VAR(function_reflection);
			object_init_ex(function_reflection, ce1);
			if (phalcon_has_constructor(function_reflection TSRMLS_CC)) {
				phalcon_call_method_p1_noret(function_reflection, "__construct", function_name);
			}
	
			PHALCON_INIT_NVAR(is_internal);
			phalcon_call_method(is_internal, function_reflection, "isinternal");
	
			/** 
			 * Internal functions links to the PHP documentation
			 */
			if (zend_is_true(is_internal)) {
				/** 
				 * Prepare function's name according to the conventions in the docs
				 */
				PHALCON_INIT_VAR(prepared_function_name);
				phalcon_fast_str_replace(prepared_function_name, underscore, minus, function_name);
				PHALCON_SCONCAT_SVSVS(html, "<span class=\"error-function\"><a target=\"_new\" href=\"http://php.net/manual/en/function.", prepared_function_name, ".php\">", function_name, "</a></span>");
			} else {
				PHALCON_SCONCAT_SVS(html, "<span class=\"error-function\">", function_name, "</span>");
			}
		} else {
			PHALCON_SCONCAT_SVS(html, "<span class=\"error-function\">", function_name, "</span>");
		}
	}
	
	/** 
	 * Check for arguments in the function
	 */
	if (phalcon_array_isset_string(trace, SS("args"))) {
	
		PHALCON_OBS_VAR(trace_args);
		phalcon_array_fetch_string(&trace_args, trace, SL("args"), PH_NOISY);
		if (phalcon_fast_count_ev(trace_args TSRMLS_CC)) {
	
			PHALCON_INIT_VAR(arguments);
			array_init(arguments);
	
			phalcon_is_iterable(trace_args, &ah0, &hp0, 0, 0);
	
			while (zend_hash_get_current_data_ex(ah0, (void**) &hd, &hp0) == SUCCESS) {
	
				PHALCON_GET_HVALUE(argument);
	
				/** 
				 * Every argument is generated using _getVarDump
				 */
				PHALCON_INIT_NVAR(dumped_argument);
				phalcon_call_method_p1(dumped_argument, this_ptr, "_getvardump", argument);
	
				PHALCON_INIT_NVAR(span_argument);
				PHALCON_CONCAT_SVS(span_argument, "<span class=\"error-parameter\">", dumped_argument, "</span>");
	
				/** 
				 * Append the HTML generated to the argument's list
				 */
				phalcon_array_append(&arguments, span_argument, PH_SEPARATE);
	
				zend_hash_move_forward_ex(ah0, &hp0);
			}
	
			/** 
			 * Join all the arguments
			 */
			PHALCON_INIT_VAR(joined_arguments);
			phalcon_fast_join_str(joined_arguments, SL(", "), arguments TSRMLS_CC);
			PHALCON_SCONCAT_SVS(html, "(", joined_arguments, ")");
		} else {
			phalcon_concat_self_str(&html, SL("()") TSRMLS_CC);
		}
	}
	
	/** 
	 * When 'file' is present, it usually means the function is provided by the user
	 */
	if (phalcon_array_isset_string(trace, SS("file"))) {
	
		PHALCON_INIT_VAR(one);
		ZVAL_LONG(one, 1);
	
		PHALCON_OBS_VAR(file);
		phalcon_array_fetch_string(&file, trace, SL("file"), PH_NOISY);
	
		PHALCON_OBS_VAR(line);
		phalcon_array_fetch_string(&line, trace, SL("line"), PH_NOISY);
	
		/** 
		 * Realpath to the file and its line using a special header
		 */
		PHALCON_SCONCAT_SVSVS(html, "<br/><div class=\"error-file\">", file, " (", line, ")</div>");
	
		PHALCON_OBS_VAR(show_files);
		phalcon_read_property_this(&show_files, this_ptr, SL("_showFiles"), PH_NOISY_CC);
	
		/** 
		 * The developer can change if the files must be opened or not
		 */
		if (zend_is_true(show_files)) {
	
			/** 
			 * Open the file to an array using 'file', this respects the openbase-dir directive
			 */
			PHALCON_INIT_VAR(lines);
			phalcon_call_func_p1(lines, "file", file);
	
			PHALCON_INIT_VAR(number_lines);
			phalcon_fast_count(number_lines, lines TSRMLS_CC);
	
			PHALCON_OBS_VAR(show_file_fragment);
			phalcon_read_property_this(&show_file_fragment, this_ptr, SL("_showFileFragment"), PH_NOISY_CC);
	
			/** 
			 * File fragments just show a piece of the file where the exception is located
			 */
			if (zend_is_true(show_file_fragment)) {
	
				/** 
				 * Take seven lines back to the current exception's line, @TODO add an option for
				 * this
				 */
				PHALCON_INIT_VAR(seven);
				ZVAL_LONG(seven, 7);
	
				PHALCON_INIT_VAR(before_line);
				sub_function(before_line, line, seven TSRMLS_CC);
	
				/** 
				 * Check for overflows
				 */
				if (PHALCON_LT_LONG(before_line, 1)) {
					PHALCON_CPY_WRT(first_line, one);
				} else {
					PHALCON_CPY_WRT(first_line, before_line);
				}
	
				/** 
				 * Take five lines after the current exception's line, @TODO add an option for this
				 */
				PHALCON_INIT_VAR(five);
				ZVAL_LONG(five, 5);
	
				PHALCON_INIT_VAR(after_line);
				phalcon_add_function(after_line, line, five TSRMLS_CC);
	
				/** 
				 * Check for overflows
				 */
				if (PHALCON_GT(after_line, number_lines)) {
					PHALCON_CPY_WRT(last_line, number_lines);
				} else {
					PHALCON_CPY_WRT(last_line, after_line);
				}
	
				PHALCON_SCONCAT_SVSVSVS(html, "<pre class='prettyprint highlight:", first_line, ":", line, " linenums:", first_line, "'>");
			} else {
				PHALCON_CPY_WRT(first_line, one);
				PHALCON_CPY_WRT(last_line, number_lines);
				PHALCON_SCONCAT_SVSVS(html, "<pre class='prettyprint highlight:", first_line, ":", line, " linenums error-scroll'>");
			}
	
			PHALCON_INIT_VAR(comment_pattern);
			ZVAL_STRING(comment_pattern, "#\\*\\/$#", 1);
	
			/** 
			 * We assume the file is utf-8 encoded, @TODO add an option for this
			 */
			PHALCON_INIT_VAR(utf8);
			ZVAL_STRING(utf8, "UTF-8", 1);
	
			/** 
			 * Don't escape quotes
			 */
			PHALCON_INIT_VAR(ent_compat);
			ZVAL_LONG(ent_compat, 2);
	
			PHALCON_INIT_VAR(tab);
			ZVAL_STRING(tab, "\t", 1);
	
			PHALCON_INIT_VAR(comment);
			ZVAL_STRING(comment, "* /", 1);
			PHALCON_CPY_WRT(i, first_line);
	
			while (1) {
	
				if (PHALCON_LE(i, last_line)) {
				} else {
					break;
				}
	
				/** 
				 * Current line in the file
				 */
				PHALCON_INIT_NVAR(line_position);
				sub_function(line_position, i, one TSRMLS_CC);
	
				/** 
				 * Current line content in the piece of file
				 */
				PHALCON_OBS_NVAR(current_line);
				phalcon_array_fetch(&current_line, lines, line_position, PH_NOISY);
	
				/** 
				 * File fragments are cleaned, removing tabs and comments
				 */
				if (zend_is_true(show_file_fragment)) {
					if (PHALCON_IS_EQUAL(i, first_line)) {
	
						PHALCON_INIT_NVAR(trimmed);
						phalcon_fast_trim(trimmed, current_line, PHALCON_TRIM_RIGHT TSRMLS_CC);
	
						PHALCON_INIT_NVAR(is_comment);
	
						phalcon_preg_match(is_comment, comment_pattern, current_line, NULL TSRMLS_CC);
	
						if (zend_is_true(is_comment)) {
							PHALCON_INIT_NVAR(spaced_current_line);
							phalcon_fast_str_replace(spaced_current_line, comment, space, current_line);
							PHALCON_CPY_WRT(current_line, spaced_current_line);
						}
					}
				}
	
				/** 
				 * Print a non break space if the current line is a line break, this allows to show
				 * the html zebra properly
				 */
				if (PHALCON_IS_STRING(current_line, "\n")) {
					phalcon_concat_self_str(&html, SL("&nbsp;\n") TSRMLS_CC);
				} else {
					if (PHALCON_IS_STRING(current_line, "\r\n")) {
						phalcon_concat_self_str(&html, SL("&nbsp;\n") TSRMLS_CC);
					} else {
						PHALCON_INIT_NVAR(spaced_current_line);
						phalcon_fast_str_replace(spaced_current_line, tab, two_spaces, current_line);
	
						PHALCON_INIT_NVAR(escaped_line);
						phalcon_call_func_p3(escaped_line, "htmlentities", spaced_current_line, ent_compat, utf8);
						phalcon_concat_self(&html, escaped_line TSRMLS_CC);
					}
				}
	
				PHALCON_SEPARATE(i);
				phalcon_increment(i);
			}
			phalcon_concat_self_str(&html, SL("</pre>") TSRMLS_CC);
		}
	}
	
	phalcon_concat_self_str(&html, SL("</td></tr>") TSRMLS_CC);
	
	RETURN_CTOR(html);
}
Пример #16
0
/**
 * Validates the form
 *
 * @param array $data
 * @param object $entity
 * @return boolean
 */
PHP_METHOD(Phalcon_Forms_Form, isValid){

	zval *data = NULL, *entity = NULL, *elements, *not_failed = NULL, *messages;
	zval *element = NULL, *validators = NULL, *name = NULL, *prepared_validators = NULL;
	zval *validator = NULL, *scope = NULL, *validation = NULL, *element_messages = NULL;
	HashTable *ah0, *ah1;
	HashPosition hp0, hp1;
	zval **hd;

	PHALCON_MM_GROW();

	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|zz", &data, &entity) == FAILURE) {
		RETURN_MM_NULL();
	}

	if (!data) {
		PHALCON_INIT_VAR(data);
	} else {
		PHALCON_SEPARATE_PARAM(data);
	}
	
	if (!entity) {
		PHALCON_INIT_VAR(entity);
	}
	
	PHALCON_OBS_VAR(elements);
	phalcon_read_property(&elements, this_ptr, SL("_elements"), PH_NOISY_CC);
	if (Z_TYPE_P(elements) == IS_ARRAY) { 
	
		/** 
		 * If the user doesn't pass an entity we use the one in this_ptr->_entity
		 */
		if (Z_TYPE_P(entity) == IS_OBJECT) {
			PHALCON_CALL_METHOD_PARAMS_2_NORETURN(this_ptr, "bind", data, entity);
		}
	
		/** 
		 * If the data is not an array use the one passed previously
		 */
		if (Z_TYPE_P(data) != IS_ARRAY) { 
			PHALCON_OBS_NVAR(data);
			phalcon_read_property(&data, this_ptr, SL("_data"), PH_NOISY_CC);
		}
	
		PHALCON_INIT_VAR(not_failed);
		ZVAL_BOOL(not_failed, 1);
	
		PHALCON_INIT_VAR(messages);
		array_init(messages);
	
		if (!phalcon_is_iterable(elements, &ah0, &hp0, 0, 0 TSRMLS_CC)) {
			return;
		}
	
		while (zend_hash_get_current_data_ex(ah0, (void**) &hd, &hp0) == SUCCESS) {
	
			PHALCON_GET_FOREACH_VALUE(element);
	
			PHALCON_INIT_NVAR(validators);
			PHALCON_CALL_METHOD(validators, element, "getvalidators");
			if (Z_TYPE_P(validators) == IS_ARRAY) { 
				if (phalcon_fast_count_ev(validators TSRMLS_CC)) {
	
					/** 
					 * Element's name
					 */
					PHALCON_INIT_NVAR(name);
					PHALCON_CALL_METHOD(name, element, "getname");
	
					/** 
					 * Prepare the validators
					 */
					PHALCON_INIT_NVAR(prepared_validators);
					array_init(prepared_validators);
	
					if (!phalcon_is_iterable(validators, &ah1, &hp1, 0, 0 TSRMLS_CC)) {
						return;
					}
	
					while (zend_hash_get_current_data_ex(ah1, (void**) &hd, &hp1) == SUCCESS) {
	
						PHALCON_GET_FOREACH_VALUE(validator);
	
						PHALCON_INIT_NVAR(scope);
						array_init_size(scope, 2);
						phalcon_array_append(&scope, name, PH_SEPARATE TSRMLS_CC);
						phalcon_array_append(&scope, validator, PH_SEPARATE TSRMLS_CC);
						phalcon_array_append(&prepared_validators, scope, PH_SEPARATE TSRMLS_CC);
	
						zend_hash_move_forward_ex(ah1, &hp1);
					}
	
					/** 
					 * Create an implicit validation
					 */
					PHALCON_INIT_NVAR(validation);
					object_init_ex(validation, phalcon_validation_ce);
					PHALCON_CALL_METHOD_PARAMS_1_NORETURN(validation, "__construct", prepared_validators);
	
					PHALCON_INIT_NVAR(element_messages);
					PHALCON_CALL_METHOD_PARAMS_2(element_messages, validation, "validate", data, entity);
					if (phalcon_fast_count_ev(element_messages TSRMLS_CC)) {
						PHALCON_INIT_NVAR(name);
						PHALCON_CALL_METHOD(name, element, "getname");
						phalcon_array_update_zval(&messages, name, &element_messages, PH_COPY | PH_SEPARATE TSRMLS_CC);
	
						PHALCON_INIT_NVAR(not_failed);
						ZVAL_BOOL(not_failed, 0);
					}
				}
			}
	
			zend_hash_move_forward_ex(ah0, &hp0);
		}
	
		/** 
		 * If the validation fails
		 */
		if (!zend_is_true(not_failed)) {
			phalcon_update_property_zval(this_ptr, SL("_messages"), messages TSRMLS_CC);
		}
	
		/** 
		 * Return the validation status
		 */
	
		RETURN_NCTOR(not_failed);
	}
	
	PHALCON_MM_RESTORE();
}
Пример #17
0
/**
 * The meta-data is obtained by reading the column descriptions from the database information schema
 *
 * @param Phalcon\Mvc\ModelInterface $model
 * @param Phalcon\DiInterface $dependencyInjector
 * @return array
 */
PHP_METHOD(Phalcon_Mvc_Model_MetaData_Strategy_Annotations, getMetaData){

	zval *model, *dependency_injector, *service;
	zval *annotations = NULL, *class_name, *reflection = NULL;
	zval *exception_message = NULL, *properties_annotations = NULL;
	zval *attributes, *primary_keys, *non_primary_keys;
	zval *numeric_typed, *not_null, *field_types, *field_sizes, *field_bytes, *field_scales;
	zval *field_bind_types, *automatic_create_attributes, *automatic_update_attributes;
	zval *identity_field = NULL, *column_annot_name;
	zval *primary_annot_name, *id_annot_name;
	zval *column_map_name, *column_type_name, *column_size_name, *column_bytes_name, *column_scale_name, *column_nullable_name;
	zval *prop_annotations = NULL, *property = NULL, *has_annotation = NULL;
	zval *column_annotation = NULL, *real_property = NULL, *feature = NULL;
	zval *field_default_values, *column_default_value = NULL;
	HashTable *ah0;
	HashPosition hp0;
	zval **hd;

	PHALCON_MM_GROW();

	phalcon_fetch_params(1, 2, 0, &model, &dependency_injector);

	if (Z_TYPE_P(dependency_injector) != IS_OBJECT) {
		PHALCON_THROW_EXCEPTION_STR(phalcon_mvc_model_exception_ce, "The dependency injector is invalid");
		return;
	}

	PHALCON_INIT_VAR(service);
	ZVAL_STRING(service, "annotations", 1);

	PHALCON_CALL_METHOD(&annotations, dependency_injector, "get", service);

	PHALCON_INIT_VAR(class_name);
	phalcon_get_class(class_name, model, 0 TSRMLS_CC);

	PHALCON_CALL_METHOD(&reflection, annotations, "get", class_name);
	if (Z_TYPE_P(reflection) != IS_OBJECT) {
		PHALCON_INIT_VAR(exception_message);
		PHALCON_CONCAT_SV(exception_message, "No annotations were found in class ", class_name);
		PHALCON_THROW_EXCEPTION_ZVAL(phalcon_mvc_model_exception_ce, exception_message);
		return;
	}

	/** 
	 * Get the properties defined in 
	 */
	PHALCON_CALL_METHOD(&properties_annotations, reflection, "getpropertiesannotations");

	if (!zend_is_true(properties_annotations) || !phalcon_fast_count_ev(properties_annotations TSRMLS_CC)) {
		PHALCON_INIT_NVAR(exception_message);
		PHALCON_CONCAT_SV(exception_message, "No properties with annotations were found in class ", class_name);
		PHALCON_THROW_EXCEPTION_ZVAL(phalcon_mvc_model_exception_ce, exception_message);
		return;
	}

	/** 
	 * Initialize meta-data
	 */
	PHALCON_INIT_VAR(attributes);
	array_init(attributes);

	PHALCON_INIT_VAR(primary_keys);
	array_init(primary_keys);

	PHALCON_INIT_VAR(non_primary_keys);
	array_init(non_primary_keys);

	PHALCON_INIT_VAR(numeric_typed);
	array_init(numeric_typed);

	PHALCON_INIT_VAR(not_null);
	array_init(not_null);

	PHALCON_INIT_VAR(field_types);
	array_init(field_types);

	PHALCON_INIT_VAR(field_sizes);
	array_init(field_sizes);

	PHALCON_INIT_VAR(field_bytes);
	array_init(field_bytes);

	PHALCON_INIT_VAR(field_scales);
	array_init(field_scales);

	PHALCON_INIT_VAR(field_bind_types);
	array_init(field_bind_types);

	PHALCON_INIT_VAR(automatic_create_attributes);
	array_init(automatic_create_attributes);

	PHALCON_INIT_VAR(automatic_update_attributes);
	array_init(automatic_update_attributes);

	PHALCON_INIT_VAR(identity_field);
	ZVAL_FALSE(identity_field);

	PHALCON_INIT_VAR(field_default_values);
	array_init(field_default_values);

	PHALCON_INIT_VAR(column_annot_name);
	ZVAL_STRING(column_annot_name, "Column", 1);

	PHALCON_INIT_VAR(primary_annot_name);
	ZVAL_STRING(primary_annot_name, "Primary", 1);

	PHALCON_INIT_VAR(id_annot_name);
	ZVAL_STRING(id_annot_name, "Identity", 1);

	PHALCON_INIT_VAR(column_map_name);
	ZVAL_STRING(column_map_name, "column", 1);

	PHALCON_INIT_VAR(column_type_name);
	ZVAL_STRING(column_type_name, "type", 1);

	PHALCON_INIT_VAR(column_size_name);
	ZVAL_STRING(column_size_name, "size", 1);

	PHALCON_INIT_VAR(column_bytes_name);
	ZVAL_STRING(column_bytes_name, "bytes", 1);

	PHALCON_INIT_VAR(column_scale_name);
	ZVAL_STRING(column_scale_name, "scale", 1);

	PHALCON_INIT_VAR(column_default_value);
	ZVAL_STRING(column_default_value, "default", 1);

	PHALCON_INIT_VAR(column_nullable_name);
	ZVAL_STRING(column_nullable_name, "nullable", 1);

	phalcon_is_iterable(properties_annotations, &ah0, &hp0, 0, 0);
	
	while (zend_hash_get_current_data_ex(ah0, (void**) &hd, &hp0) == SUCCESS) {
	
		PHALCON_GET_HKEY(property, ah0, hp0);
		PHALCON_GET_HVALUE(prop_annotations);

		/** 
		 * All columns marked with the 'Column' annotation are considered columns
		 */
		PHALCON_CALL_METHOD(&has_annotation, prop_annotations, "has", column_annot_name);
		if (!zend_is_true(has_annotation)) {
			zend_hash_move_forward_ex(ah0, &hp0);
			continue;
		}

		/** 
		 * Fetch the 'column' annotation
		 */
		PHALCON_CALL_METHOD(&column_annotation, prop_annotations, "get", column_annot_name);

		/** 
		 * Check column map
		 */
		PHALCON_CALL_METHOD(&real_property, column_annotation, "getargument", column_map_name);

		if (PHALCON_IS_EMPTY(real_property)) {
			PHALCON_CPY_WRT(real_property, property);
		}

		/** 
		 * Check if annotation has the 'type' named parameter
		 */
		PHALCON_CALL_METHOD(&feature, column_annotation, "getargument", column_type_name);

		if (PHALCON_IS_STRING(feature, "integer")) {
			phalcon_array_update_zval_long(&field_types, real_property, 0, PH_COPY);
			phalcon_array_update_zval_long(&field_bind_types, real_property, 1, PH_COPY);
			phalcon_array_update_zval_bool(&numeric_typed, real_property, 1, PH_COPY);
		} else if (PHALCON_IS_STRING(feature, "decimal")) {
			phalcon_array_update_zval_long(&field_types, real_property, 3, PH_COPY);
			phalcon_array_update_zval_long(&field_bind_types, real_property, 32, PH_COPY);
			phalcon_array_update_zval_bool(&numeric_typed, real_property, 1, PH_COPY);
		} else if (PHALCON_IS_STRING(feature, "boolean")) {
			phalcon_array_update_zval_long(&field_types, real_property, 8, PH_COPY);
			phalcon_array_update_zval_long(&field_bind_types, real_property, 5, PH_COPY);
		} else {
			if (PHALCON_IS_STRING(feature, "date")) {
				phalcon_array_update_zval_long(&field_types, real_property, 1, PH_COPY);
			} else {
				/**
				 * By default all columns are varchar/string
				 */
				phalcon_array_update_zval_long(&field_types, real_property, 2, PH_COPY);
			}
			phalcon_array_update_zval_long(&field_bind_types, real_property, 2, PH_COPY);
		}

		PHALCON_CALL_METHOD(&feature, column_annotation, "getargument", column_size_name);
		if (!PHALCON_IS_EMPTY(feature)) {
			phalcon_array_update_zval(&field_sizes, real_property, feature, PH_COPY);
			phalcon_array_update_zval(&field_bytes, real_property, feature, PH_COPY);
		}

		PHALCON_CALL_METHOD(&feature, column_annotation, "getargument", column_bytes_name);
		if (!PHALCON_IS_EMPTY(feature)) {
			phalcon_array_update_zval(&field_bytes, real_property, feature, PH_COPY);
		}

		PHALCON_CALL_METHOD(&feature, column_annotation, "getargument", column_scale_name);
		if (!PHALCON_IS_EMPTY(feature)) {
			phalcon_array_update_zval(&field_scales, real_property, feature, PH_COPY);
		}

		PHALCON_CALL_METHOD(&feature, column_annotation, "getargument", column_default_value);
		if (!PHALCON_IS_EMPTY(feature)) {
			phalcon_array_update_zval(&field_default_values, real_property, feature, PH_COPY);
		}

		PHALCON_CALL_METHOD(&feature, column_annotation, "getargument", column_default_value);
		if (!PHALCON_IS_EMPTY(feature)) {
			phalcon_array_update_zval(&field_default_values, property, feature, PH_COPY);
		}

		PHALCON_CALL_METHOD(&feature, column_annotation, "getargument", column_default_value);
		if (!PHALCON_IS_EMPTY(feature)) {
			phalcon_array_update_zval(&field_default_values, property, feature, PH_COPY);
		}

		/** 
		 * All columns marked with the 'Primary' annotation are considered primary keys
		 */
		PHALCON_CALL_METHOD(&has_annotation, prop_annotations, "has", primary_annot_name);
		if (zend_is_true(has_annotation)) {
			phalcon_array_append(&primary_keys, real_property, PH_COPY);
		} else {
			phalcon_array_append(&non_primary_keys, real_property, PH_COPY);
		}

		/** 
		 * All columns marked with the 'Primary' annotation are considered primary keys
		 */
		PHALCON_CALL_METHOD(&has_annotation, prop_annotations, "has", id_annot_name);
		if (zend_is_true(has_annotation)) {
			PHALCON_CPY_WRT(identity_field, real_property);
		}

		/** 
		 * Check if the column 
		 */
		PHALCON_CALL_METHOD(&feature, column_annotation, "getargument", column_nullable_name);
		if (!zend_is_true(feature)) {
			phalcon_array_append(&not_null, real_property, PH_COPY);
		}

		phalcon_array_append(&attributes, real_property, PH_COPY);
	
		zend_hash_move_forward_ex(ah0, &hp0);
	}
	
	/** 
	 * Create an array using the MODELS_* constants as indexes
	 */
	array_init_size(return_value, 14);
	phalcon_array_update_long(&return_value, PHALCON_MVC_MODEL_METADATA_MODELS_ATTRIBUTES,  attributes, PH_COPY);
	phalcon_array_update_long(&return_value, PHALCON_MVC_MODEL_METADATA_MODELS_PRIMARY_KEY,  primary_keys, PH_COPY);
	phalcon_array_update_long(&return_value, PHALCON_MVC_MODEL_METADATA_MODELS_NON_PRIMARY_KEY,  non_primary_keys, PH_COPY);
	phalcon_array_update_long(&return_value, PHALCON_MVC_MODEL_METADATA_MODELS_NOT_NULL,  not_null, PH_COPY);
	phalcon_array_update_long(&return_value, PHALCON_MVC_MODEL_METADATA_MODELS_DATA_TYPES,  field_types, PH_COPY);
	phalcon_array_update_long(&return_value, PHALCON_MVC_MODEL_METADATA_MODELS_DATA_TYPES_NUMERIC,  numeric_typed, PH_COPY);
	phalcon_array_update_long(&return_value, PHALCON_MVC_MODEL_METADATA_MODELS_IDENTITY_COLUMN,  identity_field, PH_COPY);
	phalcon_array_update_long(&return_value, PHALCON_MVC_MODEL_METADATA_MODELS_DATA_TYPES_BIND,  field_bind_types, PH_COPY);
	phalcon_array_update_long(&return_value, PHALCON_MVC_MODEL_METADATA_MODELS_AUTOMATIC_DEFAULT_INSERT, automatic_create_attributes, PH_COPY);
	phalcon_array_update_long(&return_value, PHALCON_MVC_MODEL_METADATA_MODELS_AUTOMATIC_DEFAULT_UPDATE, automatic_update_attributes, PH_COPY);
	phalcon_array_update_long(&return_value, PHALCON_MVC_MODEL_METADATA_MODELS_DATA_DEFAULT_VALUE, field_default_values, PH_COPY);
	phalcon_array_update_long(&return_value, PHALCON_MVC_MODEL_METADATA_MODELS_DATA_SZIE, field_sizes, PH_COPY);
	phalcon_array_update_long(&return_value, PHALCON_MVC_MODEL_METADATA_MODELS_DATA_SCALE, field_scales, PH_COPY);
	phalcon_array_update_long(&return_value, PHALCON_MVC_MODEL_METADATA_MODELS_DATA_BYTE, field_bytes, PH_COPY);
	
	PHALCON_MM_RESTORE();
}
Пример #18
0
/**
 * Read the model's column map, this can't be inferred
 *
 * @param Phalcon\Mvc\ModelInterface $model
 * @param Phalcon\DiInterface $dependencyInjector
 * @return array
 */
PHP_METHOD(Phalcon_Mvc_Model_MetaData_Strategy_Annotations, getColumnMaps){

	zval *model, *dependency_injector, *service;
	zval *ordered_column_map, *reversed_column_map = NULL;
	zval *annotations = NULL, *class_name, *reflection = NULL;
	zval *exception_message = NULL, *properties_annotations = NULL;
	zval *column_annot_name, *column_map_name;
	zval *prop_annotations = NULL, *property = NULL, *has_annotation = NULL;
	zval *column_annotation = NULL, *real_property = NULL;
	HashTable *ah0;
	HashPosition hp0;
	zval **hd;

	PHALCON_MM_GROW();

	phalcon_fetch_params(1, 2, 0, &model, &dependency_injector);

	if (!PHALCON_GLOBAL(orm).column_renaming) {
		RETURN_MM_NULL();
	}

	if (Z_TYPE_P(dependency_injector) != IS_OBJECT) {
		PHALCON_THROW_EXCEPTION_STR(phalcon_mvc_model_exception_ce, "The dependency injector is invalid");
		return;
	}

	PHALCON_INIT_VAR(service);
	ZVAL_STRING(service, "annotations", 1);

	PHALCON_CALL_METHOD(&annotations, dependency_injector, "get", service);

	PHALCON_INIT_VAR(class_name);
	phalcon_get_class(class_name, model, 0 TSRMLS_CC);

	PHALCON_CALL_METHOD(&reflection, annotations, "get", class_name);
	if (Z_TYPE_P(reflection) != IS_OBJECT) {
		PHALCON_INIT_VAR(exception_message);
		PHALCON_CONCAT_SV(exception_message, "No annotations were found in class ", class_name);
		PHALCON_THROW_EXCEPTION_ZVAL(phalcon_mvc_model_exception_ce, exception_message);
		return;
	}

	/** 
	 * Get the properties defined in 
	 */
	PHALCON_CALL_METHOD(&properties_annotations, reflection, "getpropertiesannotations");
	if (!phalcon_fast_count_ev(properties_annotations TSRMLS_CC)) {
		PHALCON_INIT_NVAR(exception_message);
		PHALCON_CONCAT_SV(exception_message, "No properties with annotations were found in class ", class_name);
		PHALCON_THROW_EXCEPTION_ZVAL(phalcon_mvc_model_exception_ce, exception_message);
		return;
	}

	/** 
	 * Initialize meta-data
	 */
	PHALCON_INIT_VAR(ordered_column_map);
	array_init(ordered_column_map);

	PHALCON_INIT_VAR(reversed_column_map);
	array_init(reversed_column_map);

	PHALCON_INIT_VAR(column_annot_name);
	ZVAL_STRING(column_annot_name, "Column", 1);

	PHALCON_INIT_VAR(column_map_name);
	ZVAL_STRING(column_map_name, "column", 1);

	phalcon_is_iterable(properties_annotations, &ah0, &hp0, 0, 0);

	while (zend_hash_get_current_data_ex(ah0, (void**) &hd, &hp0) == SUCCESS) {

		PHALCON_GET_HKEY(property, ah0, hp0);
		PHALCON_GET_HVALUE(prop_annotations);

		/** 
		 * All columns marked with the 'Column' annotation are considered columns
		 */
		PHALCON_CALL_METHOD(&has_annotation, prop_annotations, "has", column_annot_name);
		if (!zend_is_true(has_annotation)) {
			zend_hash_move_forward_ex(ah0, &hp0);
			continue;
		}

		/** 
		 * Fetch the 'column' annotation
		 */
		PHALCON_CALL_METHOD(&column_annotation, prop_annotations, "get", column_annot_name);

		/** 
		 * Check column map
		 */
		PHALCON_CALL_METHOD(&real_property, column_annotation, "getargument", column_map_name);
		if (!PHALCON_IS_EMPTY(real_property)) {
			phalcon_array_update_zval(&ordered_column_map, real_property, property, PH_COPY);
			phalcon_array_update_zval(&reversed_column_map, property, real_property, PH_COPY);
		} else {
			phalcon_array_update_zval(&ordered_column_map, property, property, PH_COPY);
			phalcon_array_update_zval(&reversed_column_map, property, property, PH_COPY);
		}

		zend_hash_move_forward_ex(ah0, &hp0);
	}

	array_init_size(return_value, 2);
	phalcon_array_update_long(&return_value, 0, ordered_column_map, PH_COPY);
	phalcon_array_update_long(&return_value, 1, reversed_column_map, PH_COPY);

	PHALCON_MM_RESTORE();
}
Пример #19
0
/**
 * Magic method to get or set services using setters/getters
 *
 * @param string $method
 * @param array $arguments
 * @return mixed
 */
PHP_METHOD(Phalcon_DI, __call){

	zval *method, *arguments = NULL, *services, *service_name = NULL;
	zval *possible_service = NULL, *handler, *exception_message;

	PHALCON_MM_GROW();

	phalcon_fetch_params(1, 1, 1, &method, &arguments);
	
	if (!arguments) {
		PHALCON_INIT_VAR(arguments);
	}
	
	/** 
	 * If the magic method starts with 'get' we try to get a service with that name
	 */
	if (phalcon_start_with_str(method, SL("get"))) {
	
		PHALCON_OBS_VAR(services);
		phalcon_read_property_this(&services, this_ptr, SL("_services"), PH_NOISY_CC);
	
		PHALCON_INIT_VAR(service_name);
		phalcon_substr(service_name, method, 3, 0);
	
		PHALCON_INIT_VAR(possible_service);
		phalcon_lcfirst(possible_service, service_name);
		if (phalcon_array_isset(services, possible_service)) {
			if (phalcon_fast_count_ev(arguments TSRMLS_CC)) {
				phalcon_call_method_p2(return_value, this_ptr, "get", possible_service, arguments);
				RETURN_MM();
			}
			phalcon_call_method_p1(return_value, this_ptr, "get", possible_service);
			RETURN_MM();
		}
	}
	
	/** 
	 * If the magic method starts with 'set' we try to set a service using that name
	 */
	if (phalcon_start_with_str(method, SL("set"))) {
		if (phalcon_array_isset_long(arguments, 0)) {
			PHALCON_INIT_NVAR(service_name);
			phalcon_substr(service_name, method, 3, 0);
	
			PHALCON_INIT_NVAR(possible_service);
			phalcon_lcfirst(possible_service, service_name);
	
			PHALCON_OBS_VAR(handler);
			phalcon_array_fetch_long(&handler, arguments, 0, PH_NOISY);
			phalcon_call_method_p2_noret(this_ptr, "set", possible_service, handler);
			RETURN_MM_NULL();
		}
	}
	
	/** 
	 * The method doesn't start with set/get throw an exception
	 */
	PHALCON_INIT_VAR(exception_message);
	PHALCON_CONCAT_SVS(exception_message, "Call to undefined method or service '", method, "'");
	PHALCON_THROW_EXCEPTION_ZVAL(phalcon_di_exception_ce, exception_message);
	return;
}
Пример #20
0
/**
 * Builds a Phalcon\Mvc\Model\Criteria based on an input array like $_POST
 *
 * @param Phalcon\DiInterface $dependencyInjector
 * @param string $modelName
 * @param array $data
 * @return static
 */
PHP_METHOD(Phalcon_Mvc_Model_Criteria, fromInput){

	zval *dependency_injector, *model_name, *data;
	zval *conditions, *service, *meta_data, *model;
	zval *data_types, *bind, *value = NULL, *field = NULL, *type = NULL, *condition = NULL;
	zval *value_pattern = NULL, *criteria, *join_conditions;
	HashTable *ah0;
	HashPosition hp0;
	zval **hd;
	zend_class_entry *ce0;

	PHALCON_MM_GROW();

	phalcon_fetch_params(1, 3, 0, &dependency_injector, &model_name, &data);
	
	if (Z_TYPE_P(data) != IS_ARRAY) { 
		PHALCON_THROW_EXCEPTION_STR(phalcon_mvc_model_exception_ce, "Input data must be an Array");
		return;
	}
	if (Z_TYPE_P(dependency_injector) != IS_OBJECT) {
		PHALCON_THROW_EXCEPTION_STR(phalcon_mvc_model_exception_ce, "A dependency injector container is required to obtain the ORM services");
		return;
	}
	
	PHALCON_INIT_VAR(conditions);
	array_init(conditions);
	if (phalcon_fast_count_ev(data TSRMLS_CC)) {
	
		PHALCON_INIT_VAR(service);
		ZVAL_STRING(service, "modelsMetadata", 1);
	
		PHALCON_INIT_VAR(meta_data);
		phalcon_call_method_p1(meta_data, dependency_injector, "getshared", service);
		ce0 = phalcon_fetch_class(model_name TSRMLS_CC);
	
		PHALCON_INIT_VAR(model);
		object_init_ex(model, ce0);
		if (phalcon_has_constructor(model TSRMLS_CC)) {
			phalcon_call_method_noret(model, "__construct");
		}
	
		PHALCON_INIT_VAR(data_types);
		phalcon_call_method_p1(data_types, meta_data, "getdatatypes", model);
	
		PHALCON_INIT_VAR(bind);
		array_init(bind);
	
		/** 
		 * We look for attributes in the array passed as data
		 */
		phalcon_is_iterable(data, &ah0, &hp0, 0, 0);
	
		while (zend_hash_get_current_data_ex(ah0, (void**) &hd, &hp0) == SUCCESS) {
	
			PHALCON_GET_HKEY(field, ah0, hp0);
			PHALCON_GET_HVALUE(value);
	
			if (phalcon_array_isset(data_types, field)) {
				if (Z_TYPE_P(value) != IS_NULL) {
					if (!PHALCON_IS_STRING(value, "")) {
	
						PHALCON_OBS_NVAR(type);
						phalcon_array_fetch(&type, data_types, field, PH_NOISY_CC);
						if (PHALCON_IS_LONG(type, 2)) {
							/** 
							 * For varchar types we use LIKE operator
							 */
							PHALCON_INIT_NVAR(condition);
							PHALCON_CONCAT_VSVS(condition, field, " LIKE :", field, ":");
	
							PHALCON_INIT_NVAR(value_pattern);
							PHALCON_CONCAT_SVS(value_pattern, "%", value, "%");
							phalcon_array_update_zval(&bind, field, &value_pattern, PH_COPY | PH_SEPARATE TSRMLS_CC);
						} else {
							/** 
							 * For the rest of data types we use a plain = operator
							 */
							PHALCON_INIT_NVAR(condition);
							PHALCON_CONCAT_VSVS(condition, field, "=:", field, ":");
							phalcon_array_update_zval(&bind, field, &value, PH_COPY | PH_SEPARATE TSRMLS_CC);
						}
	
						phalcon_array_append(&conditions, condition, PH_SEPARATE TSRMLS_CC);
					}
				}
			}
	
			zend_hash_move_forward_ex(ah0, &hp0);
		}
	
	}
	
	/** 
	 * Create an object instance and pass the paramaters to it
	 */
	PHALCON_INIT_VAR(criteria);
	object_init_ex(criteria, phalcon_mvc_model_criteria_ce);
	if (phalcon_fast_count_ev(conditions TSRMLS_CC)) {
		PHALCON_INIT_VAR(join_conditions);
		phalcon_fast_join_str(join_conditions, SL(" AND "), conditions TSRMLS_CC);
		phalcon_call_method_p1_noret(criteria, "where", join_conditions);
		phalcon_call_method_p1_noret(criteria, "bind", bind);
	}
	
	RETURN_CTOR(criteria);
}
Пример #21
0
/**
 * Initialize the metadata for certain table
 *
 * @param Phalcon\Mvc\Model $model
 * @param string $key
 * @param string $table
 * @param string $schema
 */
PHP_METHOD(Phalcon_Mvc_Model_MetaData, _initializeMetaData){

	zval *model, *key, *table, *schema, *meta_data, *data;
	zval *table_metadata = NULL, *class_name = NULL, *exception_message = NULL;
	zval *connection, *exists, *complete_table = NULL, *attributes;
	zval *primary_keys, *non_primary_keys, *numeric_typed;
	zval *not_null, *field_types, *field_bind_types;
	zval *automatic_default, *identity_field = NULL;
	zval *columns, *column = NULL, *field_name = NULL, *feature = NULL, *type = NULL;
	zval *bind_type = NULL;
	zval *t0 = NULL;
	HashTable *ah0;
	HashPosition hp0;
	zval **hd;
	int eval_int;

	PHALCON_MM_GROW();

	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zzzz", &model, &key, &table, &schema) == FAILURE) {
		PHALCON_MM_RESTORE();
		RETURN_NULL();
	}

	PHALCON_INIT_VAR(meta_data);
	phalcon_read_property(&meta_data, this_ptr, SL("_metaData"), PH_NOISY_CC);
	eval_int = phalcon_array_isset(meta_data, key);
	if (!eval_int) {
		PHALCON_INIT_VAR(data);
		PHALCON_CALL_METHOD_PARAMS_1(data, this_ptr, "read", key, PH_NO_CHECK);
		if (Z_TYPE_P(data) != IS_NULL) {
			phalcon_array_update_zval(&meta_data, key, &data, PH_COPY | PH_SEPARATE TSRMLS_CC);
			phalcon_update_property_zval(this_ptr, SL("_metaData"), meta_data TSRMLS_CC);
			PHALCON_MM_RESTORE();
			RETURN_NULL();
		}
		
		if (phalcon_method_exists_ex(model, SS("metadata") TSRMLS_CC) == SUCCESS) {
			PHALCON_INIT_VAR(table_metadata);
			PHALCON_CALL_METHOD(table_metadata, model, "metadata", PH_NO_CHECK);
			if (Z_TYPE_P(table_metadata) != IS_ARRAY) { 
				PHALCON_INIT_VAR(class_name);
				phalcon_get_class(class_name, model TSRMLS_CC);
				
				PHALCON_INIT_VAR(exception_message);
				PHALCON_CONCAT_SV(exception_message, "Invalid meta-data for model ", class_name);
				PHALCON_THROW_EXCEPTION_ZVAL(phalcon_mvc_model_exception_ce, exception_message);
				return;
			}
		} else {
			PHALCON_INIT_VAR(connection);
			PHALCON_CALL_METHOD(connection, model, "getconnection", PH_NO_CHECK);
			
			PHALCON_INIT_VAR(exists);
			PHALCON_CALL_METHOD_PARAMS_2(exists, connection, "tableexists", table, schema, PH_NO_CHECK);
			if (!zend_is_true(exists)) {
				if (zend_is_true(schema)) {
					PHALCON_INIT_VAR(complete_table);
					PHALCON_CONCAT_VSV(complete_table, schema, "\".\"", table);
				} else {
					PHALCON_CPY_WRT(complete_table, table);
				}
				
				PHALCON_INIT_NVAR(class_name);
				phalcon_get_class(class_name, model TSRMLS_CC);
				
				PHALCON_INIT_NVAR(exception_message);
				PHALCON_CONCAT_SVSV(exception_message, "Table \"", complete_table, "\" doesn't exist on database when dumping meta-data for ", class_name);
				PHALCON_THROW_EXCEPTION_ZVAL(phalcon_mvc_model_exception_ce, exception_message);
				return;
			}
			
			PHALCON_INIT_VAR(attributes);
			array_init(attributes);
			
			PHALCON_INIT_VAR(primary_keys);
			array_init(primary_keys);
			
			PHALCON_INIT_VAR(non_primary_keys);
			array_init(non_primary_keys);
			
			PHALCON_INIT_VAR(numeric_typed);
			array_init(numeric_typed);
			
			PHALCON_INIT_VAR(not_null);
			array_init(not_null);
			
			PHALCON_INIT_VAR(field_types);
			array_init(field_types);
			
			PHALCON_INIT_VAR(field_bind_types);
			array_init(field_bind_types);
			
			PHALCON_INIT_VAR(automatic_default);
			array_init(automatic_default);
			
			PHALCON_INIT_VAR(identity_field);
			ZVAL_BOOL(identity_field, 0);
			
			PHALCON_INIT_VAR(columns);
			PHALCON_CALL_METHOD_PARAMS_2(columns, connection, "describecolumns", table, schema, PH_NO_CHECK);
			if (!phalcon_fast_count_ev(columns TSRMLS_CC)) {
				if (zend_is_true(schema)) {
					PHALCON_INIT_NVAR(complete_table);
					PHALCON_CONCAT_VSV(complete_table, schema, "\".\"", table);
				} else {
					PHALCON_CPY_WRT(complete_table, table);
				}
				
				PHALCON_INIT_NVAR(class_name);
				phalcon_get_class(class_name, model TSRMLS_CC);
				
				PHALCON_INIT_NVAR(exception_message);
				PHALCON_CONCAT_SVSV(exception_message, "Cannot obtain table columns for the mapped source \"", complete_table, "\" used in model ", class_name);
				PHALCON_THROW_EXCEPTION_ZVAL(phalcon_mvc_model_exception_ce, exception_message);
				return;
			}
			
			
			if (!phalcon_valid_foreach(columns TSRMLS_CC)) {
				return;
			}
			
			ah0 = Z_ARRVAL_P(columns);
			zend_hash_internal_pointer_reset_ex(ah0, &hp0);
			
			ph_cycle_start_0:
			
				if (zend_hash_get_current_data_ex(ah0, (void**) &hd, &hp0) != SUCCESS) {
					goto ph_cycle_end_0;
				}
				
				PHALCON_GET_FOREACH_VALUE(column);
				
				PHALCON_INIT_NVAR(field_name);
				PHALCON_CALL_METHOD(field_name, column, "getname", PH_NO_CHECK);
				phalcon_array_append(&attributes, field_name, PH_SEPARATE TSRMLS_CC);
				
				PHALCON_INIT_NVAR(feature);
				PHALCON_CALL_METHOD(feature, column, "isprimary", PH_NO_CHECK);
				if (PHALCON_IS_TRUE(feature)) {
					phalcon_array_append(&primary_keys, field_name, PH_SEPARATE TSRMLS_CC);
				} else {
					phalcon_array_append(&non_primary_keys, field_name, PH_SEPARATE TSRMLS_CC);
				}
				
				PHALCON_INIT_NVAR(feature);
				PHALCON_CALL_METHOD(feature, column, "isnumeric", PH_NO_CHECK);
				if (PHALCON_IS_TRUE(feature)) {
					phalcon_array_update_zval_bool(&numeric_typed, field_name, 1, PH_SEPARATE TSRMLS_CC);
				}
				
				PHALCON_INIT_NVAR(feature);
				PHALCON_CALL_METHOD(feature, column, "isnotnull", PH_NO_CHECK);
				if (PHALCON_IS_TRUE(feature)) {
					phalcon_array_append(&not_null, field_name, PH_SEPARATE TSRMLS_CC);
				}
				
				PHALCON_INIT_NVAR(feature);
				PHALCON_CALL_METHOD(feature, column, "isautoincrement", PH_NO_CHECK);
				if (PHALCON_IS_TRUE(feature)) {
					PHALCON_CPY_WRT(identity_field, field_name);
				}
				
				PHALCON_INIT_NVAR(type);
				PHALCON_CALL_METHOD(type, column, "gettype", PH_NO_CHECK);
				phalcon_array_update_zval(&field_types, field_name, &type, PH_COPY | PH_SEPARATE TSRMLS_CC);
				
				PHALCON_INIT_NVAR(bind_type);
				PHALCON_CALL_METHOD(bind_type, column, "getbindtype", PH_NO_CHECK);
				phalcon_array_update_zval(&field_bind_types, field_name, &bind_type, PH_COPY | PH_SEPARATE TSRMLS_CC);
				
				zend_hash_move_forward_ex(ah0, &hp0);
				goto ph_cycle_start_0;
				
			ph_cycle_end_0:
			
			PHALCON_INIT_NVAR(table_metadata);
			array_init(table_metadata);
			phalcon_array_update_long(&table_metadata, 0, &attributes, PH_COPY | PH_SEPARATE TSRMLS_CC);
			phalcon_array_update_long(&table_metadata, 1, &primary_keys, PH_COPY | PH_SEPARATE TSRMLS_CC);
			phalcon_array_update_long(&table_metadata, 2, &non_primary_keys, PH_COPY | PH_SEPARATE TSRMLS_CC);
			phalcon_array_update_long(&table_metadata, 3, &not_null, PH_COPY | PH_SEPARATE TSRMLS_CC);
			phalcon_array_update_long(&table_metadata, 4, &field_types, PH_COPY | PH_SEPARATE TSRMLS_CC);
			phalcon_array_update_long(&table_metadata, 5, &numeric_typed, PH_COPY | PH_SEPARATE TSRMLS_CC);
			phalcon_array_update_long(&table_metadata, 8, &identity_field, PH_COPY | PH_SEPARATE TSRMLS_CC);
			phalcon_array_update_long(&table_metadata, 9, &field_bind_types, PH_COPY | PH_SEPARATE TSRMLS_CC);
			phalcon_array_update_long(&table_metadata, 10, &automatic_default, PH_COPY | PH_SEPARATE TSRMLS_CC);
			phalcon_array_update_long(&table_metadata, 11, &automatic_default, PH_COPY | PH_SEPARATE TSRMLS_CC);
		}
		
		PHALCON_INIT_VAR(t0);
		phalcon_read_property(&t0, this_ptr, SL("_metaData"), PH_NOISY_CC);
		phalcon_array_update_zval(&t0, key, &table_metadata, PH_COPY TSRMLS_CC);
		phalcon_update_property_zval(this_ptr, SL("_metaData"), t0 TSRMLS_CC);
		PHALCON_CALL_METHOD_PARAMS_2_NORETURN(this_ptr, "write", key, table_metadata, PH_NO_CHECK);
	}
	
	PHALCON_MM_RESTORE();
}
Пример #22
0
/**
 * Resolves the service
 *
 * @param array $parameters
 * @param Phalcon\DiInterface $dependencyInjector
 * @return mixed
 */
PHP_METHOD(Phalcon_DI_Service, resolve){

	zval *parameters = NULL, *dependency_injector = NULL, *shared;
	zval *shared_instance, *found = NULL, *instance = NULL, *definition;
	zval *builder, *name, *exception_message;

	PHALCON_MM_GROW();

	phalcon_fetch_params(1, 0, 2, &parameters, &dependency_injector);
	
	if (!parameters) {
		PHALCON_INIT_VAR(parameters);
	}
	
	if (!dependency_injector) {
		PHALCON_INIT_VAR(dependency_injector);
	}
	
	PHALCON_OBS_VAR(shared);
	phalcon_read_property_this(&shared, this_ptr, SL("_shared"), PH_NOISY_CC);
	
	/** 
	 * Check if the service is shared
	 */
	if (zend_is_true(shared)) {
	
		PHALCON_OBS_VAR(shared_instance);
		phalcon_read_property_this(&shared_instance, this_ptr, SL("_sharedInstance"), PH_NOISY_CC);
		if (Z_TYPE_P(shared_instance) != IS_NULL) {
			RETURN_CCTOR(shared_instance);
		}
	}
	
	PHALCON_INIT_VAR(found);
	ZVAL_BOOL(found, 1);
	
	PHALCON_INIT_VAR(instance);
	
	PHALCON_OBS_VAR(definition);
	phalcon_read_property_this(&definition, this_ptr, SL("_definition"), PH_NOISY_CC);
	if (Z_TYPE_P(definition) == IS_STRING) {
	
		/** 
		 * String definitions can be class names without implicit parameters
		 */
		if (phalcon_class_exists(definition, 1 TSRMLS_CC)) {
			if (Z_TYPE_P(parameters) == IS_ARRAY) { 
				if (phalcon_fast_count_ev(parameters TSRMLS_CC)) {
					if (phalcon_create_instance_params(instance, definition, parameters TSRMLS_CC) == FAILURE) {
						return;
					}
				} else {
					PHALCON_INIT_NVAR(instance);
					if (phalcon_create_instance(instance, definition TSRMLS_CC) == FAILURE) {
						return;
					}
				}
			} else {
				PHALCON_INIT_NVAR(instance);
				if (phalcon_create_instance(instance, definition TSRMLS_CC) == FAILURE) {
					return;
				}
			}
		} else {
			ZVAL_BOOL(found, 0);
		}
	} else {
		/** 
		 * Object definitions can be a Closure or an already resolved instance
		 */
		if (likely(Z_TYPE_P(definition) == IS_OBJECT)) {
			if (phalcon_is_instance_of(definition, SL("Closure") TSRMLS_CC)) {
				if (Z_TYPE_P(parameters) == IS_ARRAY) { 
					PHALCON_INIT_NVAR(instance);
					PHALCON_CALL_USER_FUNC_ARRAY(instance, definition, parameters);
				} else {
					PHALCON_INIT_NVAR(instance);
					PHALCON_CALL_USER_FUNC(instance, definition);
				}
			} else {
				PHALCON_CPY_WRT(instance, definition);
			}
		} else {
			/** 
			 * Array definitions require a 'className' parameter
			 */
			if (Z_TYPE_P(definition) == IS_ARRAY) { 
				PHALCON_INIT_VAR(builder);
				object_init_ex(builder, phalcon_di_service_builder_ce);
	
				PHALCON_INIT_NVAR(instance);
				phalcon_call_method_p3(instance, builder, "build", dependency_injector, definition, parameters);
			} else {
				PHALCON_INIT_NVAR(found);
				ZVAL_BOOL(found, 0);
			}
		}
	}
	
	/** 
	 * If the service can't be built, we must throw an exception
	 */
	if (PHALCON_IS_FALSE(found)) {
		PHALCON_OBS_VAR(name);
		phalcon_read_property_this(&name, this_ptr, SL("_name"), PH_NOISY_CC);
	
		PHALCON_INIT_VAR(exception_message);
		PHALCON_CONCAT_SVS(exception_message, "Service '", name, "' cannot be resolved");
		PHALCON_THROW_EXCEPTION_ZVAL(phalcon_di_exception_ce, exception_message);
		return;
	}
	
	/** 
	 * Update the shared instance if the service is shared
	 */
	if (zend_is_true(shared)) {
		phalcon_update_property_this(this_ptr, SL("_sharedInstance"), instance TSRMLS_CC);
	}
	
	RETURN_CCTOR(instance);
}
Пример #23
0
/**
 * Manually bind params to a SQL statement. This method requires an active connection to a database system
 *
 *<code>
 *	$sql = $connection->bindParams('SELECT * FROM robots WHERE name = ?0', array('Bender'));
 *  echo $sql; // SELECT * FROM robots WHERE name = 'Bender'
 *</code>
 *
 * @param string $sqlStatement
 * @param array $params
 * @return string
 */
PHP_METHOD(Phalcon_Db_Adapter_Pdo, bindParams){

	zval *sql_statement, *params, *sql = NULL, *pdo, *bind_value = NULL;
	zval *index = NULL, *value = NULL, *place_key = NULL, *replaced_sql = NULL;
	HashTable *ah0;
	HashPosition hp0;
	zval **hd;

	PHALCON_MM_GROW();

	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zz", &sql_statement, &params) == FAILURE) {
		RETURN_MM_NULL();
	}

	if (Z_TYPE_P(params) == IS_ARRAY) { 
		if (phalcon_fast_count_ev(params TSRMLS_CC)) {
			PHALCON_CPY_WRT(sql, sql_statement);
	
			PHALCON_OBS_VAR(pdo);
			phalcon_read_property(&pdo, this_ptr, SL("_pdo"), PH_NOISY_CC);
	
			if (!phalcon_is_iterable(params, &ah0, &hp0, 0, 0 TSRMLS_CC)) {
				return;
			}
	
			while (zend_hash_get_current_data_ex(ah0, (void**) &hd, &hp0) == SUCCESS) {
	
				PHALCON_GET_FOREACH_KEY(index, ah0, hp0);
				PHALCON_GET_FOREACH_VALUE(bind_value);
	
				if (phalcon_is_numeric(bind_value)) {
					PHALCON_CPY_WRT(value, bind_value);
				} else {
					if (Z_TYPE_P(bind_value) == IS_OBJECT) {
						PHALCON_INIT_NVAR(value);
						PHALCON_CALL_FUNC_PARAMS_1(value, "strval", bind_value);
					} else {
						PHALCON_INIT_NVAR(value);
						PHALCON_CALL_METHOD_PARAMS_1(value, pdo, "quote", bind_value);
					}
				}
	
				/** 
				 * Handle long parameters as numeric placeholders: ?0, ?1
				 */
				if (Z_TYPE_P(index) == IS_LONG) {
					PHALCON_INIT_NVAR(place_key);
					PHALCON_CONCAT_SV(place_key, "?", index);
	
					PHALCON_INIT_NVAR(replaced_sql);
					phalcon_fast_str_replace(replaced_sql, place_key, value, sql TSRMLS_CC);
					PHALCON_CPY_WRT(sql, replaced_sql);
					zend_hash_move_forward_ex(ah0, &hp0);
					continue;
				}
	
				/** 
				 * Handle long parameters as string placeholders: :name:, :other:
				 */
				if (Z_TYPE_P(index) == IS_STRING) {
					PHALCON_INIT_NVAR(place_key);
					PHALCON_CONCAT_SVS(place_key, ":", index, ":");
	
					PHALCON_INIT_NVAR(replaced_sql);
					phalcon_fast_str_replace(replaced_sql, place_key, value, sql TSRMLS_CC);
					PHALCON_CPY_WRT(sql, replaced_sql);
					zend_hash_move_forward_ex(ah0, &hp0);
					continue;
				}
	
				/** 
				 * Unrecognized parameter type
				 */
				PHALCON_THROW_EXCEPTION_STR(phalcon_db_exception_ce, "Invalid bind parameter");
				return;
	
				zend_hash_move_forward_ex(ah0, &hp0);
			}
	
	
			RETURN_CCTOR(sql);
		}
	}
	
	RETURN_CCTOR(sql_statement);
}
Пример #24
0
/**
 * Resolves the service based on its configuration
 *
 * @param string $name
 * @param array $parameters
 * @return mixed
 */
PHP_METHOD(Phalcon_DI, get){

	zval *name, *parameters = NULL, *services, *service, *instance = NULL;
	zval *exception_message;

	PHALCON_MM_GROW();

	phalcon_fetch_params(1, 1, 1, &name, &parameters);
	
	if (!parameters) {
		PHALCON_INIT_VAR(parameters);
	}
	
	/** 
	 * A valid service alias is a string
	 */
	if (Z_TYPE_P(name) != IS_STRING) {
		PHALCON_THROW_EXCEPTION_STR(phalcon_di_exception_ce, "The service alias must be a string");
		return;
	}
	
	PHALCON_OBS_VAR(services);
	phalcon_read_property_this(&services, this_ptr, SL("_services"), PH_NOISY_CC);
	if (phalcon_array_isset(services, name)) {
		/** 
		 * The service is registered in the DI
		 */
		PHALCON_OBS_VAR(service);
		phalcon_array_fetch(&service, services, name, PH_NOISY);
	
		PHALCON_INIT_VAR(instance);
		phalcon_call_method_p2(instance, service, "resolve", parameters, this_ptr);
	} else {
		/** 
		 * The DI also acts as builder for any class even if it isn't defined in the DI
		 */
		if (phalcon_class_exists(name, 1 TSRMLS_CC)) {
			if (Z_TYPE_P(parameters) == IS_ARRAY) { 
				if (phalcon_fast_count_ev(parameters TSRMLS_CC)) {
					PHALCON_INIT_NVAR(instance);
					if (phalcon_create_instance_params(instance, name, parameters TSRMLS_CC) == FAILURE) {
						return;
					}
				} else {
					PHALCON_INIT_NVAR(instance);
					if (phalcon_create_instance(instance, name TSRMLS_CC) == FAILURE) {
						return;
					}
				}
			} else {
				PHALCON_INIT_NVAR(instance);
				if (phalcon_create_instance(instance, name TSRMLS_CC) == FAILURE) {
					return;
				}
			}
		} else {
			PHALCON_INIT_VAR(exception_message);
			PHALCON_CONCAT_SVS(exception_message, "Service '", name, "' wasn't found in the dependency injection container");
			PHALCON_THROW_EXCEPTION_ZVAL(phalcon_di_exception_ce, exception_message);
			return;
		}
	}
	
	/** 
	 * Pass the DI itself if the instance implements Phalcon\DI\InjectionAwareInterface
	 */
	if (Z_TYPE_P(instance) == IS_OBJECT) {
		if (phalcon_method_exists_ex(instance, SS("setdi") TSRMLS_CC) == SUCCESS) {
			phalcon_call_method_p1_noret(instance, "setdi", this_ptr);
		}
	}
	
	RETURN_CCTOR(instance);
}
Пример #25
0
/**
 * Inserts data into a table using custom RBDM SQL syntax
 *
 * <code>
 * //Inserting a new robot
 * $success = $connection->insert(
 *     "robots",
 *     array("Astro Boy", 1952),
 *     array("name", "year")
 * );
 *
 * //Next SQL sentence is sent to the database system
 * INSERT INTO `robots` (`name`, `year`) VALUES ("Astro boy", 1952);
 * </code>
 *
 * @param 	string $table
 * @param 	array $values
 * @param 	array $fields
 * @param 	array $dataTypes
 * @return 	boolean
 */
PHP_METHOD(Phalcon_Db, insert){

	zval *table, *values, *fields = NULL, *data_types = NULL, *exception_message;
	zval *placeholders, *insert_values, *bind_data_types = NULL;
	zval *value = NULL, *position = NULL, *str_value = NULL, *bind_type = NULL;
	zval *joined_values, *joined_fields, *insert_sql = NULL;
	zval *success;
	HashTable *ah0;
	HashPosition hp0;
	zval **hd;
	char *hash_index;
	uint hash_index_len;
	ulong hash_num;
	int hash_type;
	int eval_int;

	PHALCON_MM_GROW();

	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zz|zz", &table, &values, &fields, &data_types) == FAILURE) {
		PHALCON_MM_RESTORE();
		RETURN_NULL();
	}

	if (!fields) {
		PHALCON_INIT_NVAR(fields);
	}
	
	if (!data_types) {
		PHALCON_INIT_NVAR(data_types);
	}
	
	if (Z_TYPE_P(values) != IS_ARRAY) { 
		PHALCON_THROW_EXCEPTION_STR(phalcon_db_exception_ce, "The second parameter for insert isn't an Array");
		return;
	}
	if (!phalcon_fast_count_ev(values TSRMLS_CC)) {
		PHALCON_INIT_VAR(exception_message);
		PHALCON_CONCAT_SVS(exception_message, "Unable to insert into ", table, " without data");
		PHALCON_THROW_EXCEPTION_ZVAL(phalcon_db_exception_ce, exception_message);
		return;
	}
	
	PHALCON_INIT_VAR(placeholders);
	array_init(placeholders);
	
	PHALCON_INIT_VAR(insert_values);
	array_init(insert_values);
	if (Z_TYPE_P(data_types) == IS_ARRAY) { 
		PHALCON_INIT_VAR(bind_data_types);
		array_init(bind_data_types);
	} else {
		PHALCON_CPY_WRT(bind_data_types, data_types);
	}
	
	
	if (!phalcon_valid_foreach(values TSRMLS_CC)) {
		return;
	}
	
	ah0 = Z_ARRVAL_P(values);
	zend_hash_internal_pointer_reset_ex(ah0, &hp0);
	
	ph_cycle_start_0:
	
		if (zend_hash_get_current_data_ex(ah0, (void**) &hd, &hp0) != SUCCESS) {
			goto ph_cycle_end_0;
		}
		
		PHALCON_GET_FOREACH_KEY(position, ah0, hp0);
		PHALCON_GET_FOREACH_VALUE(value);
		
		if (Z_TYPE_P(value) == IS_OBJECT) {
			PHALCON_INIT_NVAR(str_value);
			PHALCON_CALL_FUNC_PARAMS_1(str_value, "strval", value);
			phalcon_array_append(&placeholders, str_value, PH_SEPARATE TSRMLS_CC);
		} else {
			if (Z_TYPE_P(value) == IS_NULL) {
				phalcon_array_append_string(&placeholders, SL("null"), PH_SEPARATE TSRMLS_CC);
			} else {
				phalcon_array_append_string(&placeholders, SL("?"), PH_SEPARATE TSRMLS_CC);
				phalcon_array_append(&insert_values, value, PH_SEPARATE TSRMLS_CC);
				if (Z_TYPE_P(data_types) == IS_ARRAY) { 
					eval_int = phalcon_array_isset(data_types, position);
					if (!eval_int) {
						PHALCON_THROW_EXCEPTION_STR(phalcon_db_exception_ce, "Incomplete number of bind types");
						return;
					}
					
					PHALCON_INIT_NVAR(bind_type);
					phalcon_array_fetch(&bind_type, data_types, position, PH_NOISY_CC);
					phalcon_array_append(&bind_data_types, bind_type, PH_SEPARATE TSRMLS_CC);
				}
			}
		}
		
		zend_hash_move_forward_ex(ah0, &hp0);
		goto ph_cycle_start_0;
		
	ph_cycle_end_0:
	
	PHALCON_INIT_VAR(joined_values);
	phalcon_fast_join_str(joined_values, SL(", "), placeholders TSRMLS_CC);
	if (Z_TYPE_P(fields) == IS_ARRAY) { 
		PHALCON_INIT_VAR(joined_fields);
		phalcon_fast_join_str(joined_fields, SL(", "), fields TSRMLS_CC);
		
		PHALCON_INIT_VAR(insert_sql);
		PHALCON_CONCAT_SVSVSVS(insert_sql, "INSERT INTO ", table, " (", joined_fields, ") VALUES (", joined_values, ")");
	} else {
		PHALCON_INIT_NVAR(insert_sql);
		PHALCON_CONCAT_SVSVS(insert_sql, "INSERT INTO ", table, " VALUES (", joined_values, ")");
	}
	
	PHALCON_INIT_VAR(success);
	PHALCON_CALL_METHOD_PARAMS_3(success, this_ptr, "execute", insert_sql, insert_values, bind_data_types, PH_NO_CHECK);
	
	RETURN_CCTOR(success);
}
Пример #26
0
/**
 * Sends the cookie to the HTTP client
 * Stores the cookie definition in session
 *
 * @return Phalcon\Http\Cookie
 */
PHP_METHOD(Phalcon_Http_Cookie, send){

	zval *name, *value, *expire, *domain, *path, *secure;
	zval *http_only, *dependency_injector, *definition;
	zval *service = NULL, *session = NULL, *key, *encryption, *crypt = NULL;
	zval *encrypt_value = NULL, *has_session = NULL;

	PHALCON_MM_GROW();

	name = phalcon_read_property(getThis(), SL("_name"), PH_NOISY);
	value = phalcon_read_property(getThis(), SL("_value"), PH_NOISY);
	expire = phalcon_read_property(getThis(), SL("_expire"), PH_NOISY);
	domain = phalcon_read_property(getThis(), SL("_domain"), PH_NOISY);
	path = phalcon_read_property(getThis(), SL("_path"), PH_NOISY);
	secure = phalcon_read_property(getThis(), SL("_secure"), PH_NOISY);
	http_only = phalcon_read_property(getThis(), SL("_httpOnly"), PH_NOISY);
	dependency_injector = phalcon_read_property(getThis(), SL("_dependencyInjector"), PH_NOISY);
	if (Z_TYPE_P(dependency_injector) == IS_OBJECT) {
		PHALCON_INIT_VAR(service);
		ZVAL_STR(service, IS(session));

		PHALCON_CALL_METHOD(&has_session, dependency_injector, "has", service);
		if (zend_is_true(has_session)) {
			PHALCON_INIT_VAR(definition);
			array_init(definition);
			if (!PHALCON_IS_LONG(expire, 0)) {
				phalcon_array_update_str(definition, SL("expire"), expire, PH_COPY);
			}

			if (PHALCON_IS_NOT_EMPTY(path)) {
				phalcon_array_update_str(definition, SL("path"), path, PH_COPY);
			}

			if (PHALCON_IS_NOT_EMPTY(domain)) {
				phalcon_array_update_string(definition, IS(domain), domain, PH_COPY);
			}

			if (PHALCON_IS_NOT_EMPTY(secure)) {
				phalcon_array_update_str(definition, SL("secure"), secure, PH_COPY);
			}

			if (PHALCON_IS_NOT_EMPTY(http_only)) {
				phalcon_array_update_str(definition, SL("httpOnly"), http_only, PH_COPY);
			}

			/**
			 * The definition is stored in session
			 */
			if (phalcon_fast_count_ev(definition)) {
				PHALCON_CALL_METHOD(&session, dependency_injector, "getshared", service);

				if (Z_TYPE_P(session) != IS_NULL) {
					PHALCON_VERIFY_INTERFACE(session, phalcon_session_adapterinterface_ce);

					PHALCON_INIT_VAR(key);
					PHALCON_CONCAT_SV(key, "_PHCOOKIE_", name);
					PHALCON_CALL_METHOD(NULL, session, "set", key, definition);
				}
			}
		}
	}

	encryption = phalcon_read_property(getThis(), SL("_useEncryption"), PH_NOISY);
	if (zend_is_true(encryption) && PHALCON_IS_NOT_EMPTY(value)) {
		if (Z_TYPE_P(dependency_injector) != IS_OBJECT) {
			PHALCON_THROW_EXCEPTION_STR(phalcon_http_cookie_exception_ce, "A dependency injection object is required to access the 'filter' service");
			return;
		}

		PHALCON_INIT_NVAR(service);
		ZVAL_STRING(service, "crypt");

		PHALCON_CALL_METHOD(&crypt, dependency_injector, "getshared", service);
		PHALCON_VERIFY_INTERFACE(crypt, phalcon_cryptinterface_ce);

		/**
		 * Encrypt the value also coding it with base64
		 */
		PHALCON_CALL_METHOD(&encrypt_value, crypt, "encryptbase64", value);
	} else {
		PHALCON_CPY_WRT(encrypt_value, value);
	}

	/** 
	 * Sets the cookie using the standard 'setcookie' function
	 */
	convert_to_string_ex(name);
	convert_to_string_ex(encrypt_value);
	convert_to_long_ex(expire);
	convert_to_string_ex(path);
	convert_to_string_ex(domain);
	convert_to_long_ex(secure);
	convert_to_long_ex(http_only);

	php_setcookie(Z_STR_P(name), Z_STR_P(encrypt_value), Z_LVAL_P(expire), Z_STR_P(path), Z_STR_P(domain), Z_LVAL_P(secure), 1, Z_LVAL_P(http_only));

	RETURN_THIS();
}
Пример #27
0
/**
 * The meta-data is obtained by reading the column descriptions from the database information schema
 *
 * @param Phalcon\Mvc\ModelInterface $model
 * @param Phalcon\DiInterface $dependencyInjector
 * @return array
 */
PHP_METHOD(Phalcon_Mvc_Model_MetaData_Strategy_Annotations, getMetaData){

	zval *model, *dependency_injector, *service;
	zval *annotations, *class_name, *reflection;
	zval *exception_message = NULL, *properties_annotations;
	zval *attributes, *primary_keys, *non_primary_keys;
	zval *numeric_typed, *not_null, *field_types;
	zval *field_bind_types, *automatic_default;
	zval *identity_field = NULL, *column_annot_name;
	zval *primary_annot_name, *id_annot_name;
	zval *column_type_name, *column_nullable_name;
	zval *prop_annotations = NULL, *property = NULL, *has_annotation = NULL;
	zval *column_annotation = NULL, *feature = NULL, *model_metadata;
	HashTable *ah0;
	HashPosition hp0;
	zval **hd;

	PHALCON_MM_GROW();

	phalcon_fetch_params(1, 2, 0, &model, &dependency_injector);
	
	if (Z_TYPE_P(dependency_injector) != IS_OBJECT) {
		PHALCON_THROW_EXCEPTION_STR(phalcon_mvc_model_exception_ce, "The dependency injector is invalid");
		return;
	}
	
	PHALCON_INIT_VAR(service);
	ZVAL_STRING(service, "annotations", 1);
	
	PHALCON_INIT_VAR(annotations);
	phalcon_call_method_p1(annotations, dependency_injector, "get", service);
	
	PHALCON_INIT_VAR(class_name);
	phalcon_get_class(class_name, model, 0 TSRMLS_CC);
	
	PHALCON_INIT_VAR(reflection);
	phalcon_call_method_p1(reflection, annotations, "get", class_name);
	if (Z_TYPE_P(reflection) != IS_OBJECT) {
		PHALCON_INIT_VAR(exception_message);
		PHALCON_CONCAT_SV(exception_message, "No annotations were found in class ", class_name);
		PHALCON_THROW_EXCEPTION_ZVAL(phalcon_mvc_model_exception_ce, exception_message);
		return;
	}
	
	/** 
	 * Get the properties defined in 
	 */
	PHALCON_INIT_VAR(properties_annotations);
	phalcon_call_method(properties_annotations, reflection, "getpropertiesannotations");
	if (!phalcon_fast_count_ev(properties_annotations TSRMLS_CC)) {
		PHALCON_INIT_NVAR(exception_message);
		PHALCON_CONCAT_SV(exception_message, "No properties with annotations were found in class ", class_name);
		PHALCON_THROW_EXCEPTION_ZVAL(phalcon_mvc_model_exception_ce, exception_message);
		return;
	}
	
	/** 
	 * Initialize meta-data
	 */
	PHALCON_INIT_VAR(attributes);
	array_init(attributes);
	
	PHALCON_INIT_VAR(primary_keys);
	array_init(primary_keys);
	
	PHALCON_INIT_VAR(non_primary_keys);
	array_init(non_primary_keys);
	
	PHALCON_INIT_VAR(numeric_typed);
	array_init(numeric_typed);
	
	PHALCON_INIT_VAR(not_null);
	array_init(not_null);
	
	PHALCON_INIT_VAR(field_types);
	array_init(field_types);
	
	PHALCON_INIT_VAR(field_bind_types);
	array_init(field_bind_types);
	
	PHALCON_INIT_VAR(automatic_default);
	array_init(automatic_default);
	
	PHALCON_INIT_VAR(identity_field);
	ZVAL_BOOL(identity_field, 0);
	
	PHALCON_INIT_VAR(column_annot_name);
	ZVAL_STRING(column_annot_name, "Column", 1);
	
	PHALCON_INIT_VAR(primary_annot_name);
	ZVAL_STRING(primary_annot_name, "Primary", 1);
	
	PHALCON_INIT_VAR(id_annot_name);
	ZVAL_STRING(id_annot_name, "Identity", 1);
	
	PHALCON_INIT_VAR(column_type_name);
	ZVAL_STRING(column_type_name, "type", 1);
	
	PHALCON_INIT_VAR(column_nullable_name);
	ZVAL_STRING(column_nullable_name, "nullable", 1);
	
	phalcon_is_iterable(properties_annotations, &ah0, &hp0, 0, 0);
	
	while (zend_hash_get_current_data_ex(ah0, (void**) &hd, &hp0) == SUCCESS) {
	
		PHALCON_GET_HKEY(property, ah0, hp0);
		PHALCON_GET_HVALUE(prop_annotations);
	
		/** 
		 * All columns marked with the 'Column' annotation are considered columns
		 */
		PHALCON_INIT_NVAR(has_annotation);
		phalcon_call_method_p1(has_annotation, prop_annotations, "has", column_annot_name);
		if (!zend_is_true(has_annotation)) {
			zend_hash_move_forward_ex(ah0, &hp0);
			continue;
		}
	
		/** 
		 * Fetch the 'column' annotation
		 */
		PHALCON_INIT_NVAR(column_annotation);
		phalcon_call_method_p1(column_annotation, prop_annotations, "get", column_annot_name);
	
		/** 
		 * Check if annotation has the 'type' named parameter
		 */
		PHALCON_INIT_NVAR(feature);
		phalcon_call_method_p1(feature, column_annotation, "getnamedparameter", column_type_name);
		if (PHALCON_IS_STRING(feature, "integer")) {
			phalcon_array_update_zval_long(&field_types, property, 0, PH_SEPARATE);
			phalcon_array_update_zval_long(&field_bind_types, property, 1, PH_SEPARATE);
			phalcon_array_update_zval_bool(&numeric_typed, property, 1, PH_SEPARATE);
		} else {
			if (PHALCON_IS_STRING(feature, "decimal")) {
				phalcon_array_update_zval_long(&field_types, property, 3, PH_SEPARATE);
				phalcon_array_update_zval_long(&field_bind_types, property, 32, PH_SEPARATE);
				phalcon_array_update_zval_bool(&numeric_typed, property, 1, PH_SEPARATE);
			} else {
				if (PHALCON_IS_STRING(feature, "boolean")) {
					phalcon_array_update_zval_long(&field_types, property, 8, PH_SEPARATE);
					phalcon_array_update_zval_long(&field_bind_types, property, 5, PH_SEPARATE);
				} else {
					if (PHALCON_IS_STRING(feature, "date")) {
						phalcon_array_update_zval_long(&field_types, property, 1, PH_SEPARATE);
					} else {
						/** 
						 * By default all columns are varchar/string
						 */
						phalcon_array_update_zval_long(&field_types, property, 2, PH_SEPARATE);
					}
					phalcon_array_update_zval_long(&field_bind_types, property, 2, PH_SEPARATE);
				}
			}
		}
	
		/** 
		 * All columns marked with the 'Primary' annotation are considered primary keys
		 */
		PHALCON_INIT_NVAR(has_annotation);
		phalcon_call_method_p1(has_annotation, prop_annotations, "has", primary_annot_name);
		if (zend_is_true(has_annotation)) {
			phalcon_array_append(&primary_keys, property, PH_SEPARATE);
		} else {
			phalcon_array_append(&non_primary_keys, property, PH_SEPARATE);
		}
	
		/** 
		 * All columns marked with the 'Primary' annotation are considered primary keys
		 */
		PHALCON_INIT_NVAR(has_annotation);
		phalcon_call_method_p1(has_annotation, prop_annotations, "has", id_annot_name);
		if (zend_is_true(has_annotation)) {
			PHALCON_CPY_WRT(identity_field, property);
		}
	
		/** 
		 * Check if the column 
		 */
		PHALCON_INIT_NVAR(feature);
		phalcon_call_method_p1(feature, column_annotation, "getnamedparameter", column_nullable_name);
		if (!zend_is_true(feature)) {
			phalcon_array_append(&not_null, property, PH_SEPARATE);
		}
	
		phalcon_array_append(&attributes, property, PH_SEPARATE);
	
		zend_hash_move_forward_ex(ah0, &hp0);
	}
	
	/** 
	 * Create an array using the MODELS_* constants as indexes
	 */
	PHALCON_INIT_VAR(model_metadata);
	array_init(model_metadata);
	phalcon_array_update_long(&model_metadata, 0, &attributes, PH_COPY | PH_SEPARATE);
	phalcon_array_update_long(&model_metadata, 1, &primary_keys, PH_COPY | PH_SEPARATE);
	phalcon_array_update_long(&model_metadata, 2, &non_primary_keys, PH_COPY | PH_SEPARATE);
	phalcon_array_update_long(&model_metadata, 3, &not_null, PH_COPY | PH_SEPARATE);
	phalcon_array_update_long(&model_metadata, 4, &field_types, PH_COPY | PH_SEPARATE);
	phalcon_array_update_long(&model_metadata, 5, &numeric_typed, PH_COPY | PH_SEPARATE);
	phalcon_array_update_long(&model_metadata, 8, &identity_field, PH_COPY | PH_SEPARATE);
	phalcon_array_update_long(&model_metadata, 9, &field_bind_types, PH_COPY | PH_SEPARATE);
	phalcon_array_update_long(&model_metadata, 10, &automatic_default, PH_COPY | PH_SEPARATE);
	phalcon_array_update_long(&model_metadata, 11, &automatic_default, PH_COPY | PH_SEPARATE);
	
	RETURN_CTOR(model_metadata);
}
Пример #28
0
/**
 * Builds a SELECT statement
 *
 * @param array $definition
 * @return string
 */
PHP_METHOD(Phalcon_Db_Dialect_Oracle, select){

	zval *definition, *escape_char = NULL, *columns, *selected_columns, *distinct;
	zval *column = NULL, *column_item = NULL, *column_sql = NULL, *columns_sql = NULL;
	zval *column_domain = NULL, *column_domain_sql = NULL, *column_alias = NULL;
	zval *column_alias_sql = NULL, *tables, *selected_tables;
	zval *table = NULL, *sql_table = NULL, *tables_sql = NULL, *sql, *joins;
	zval *join = NULL, *type = NULL, *sql_join = NULL, *join_conditions_array = NULL;
	zval *join_expressions = NULL, *join_condition = NULL, *join_expression = NULL;
	zval *join_conditions = NULL, *where_conditions;
	zval *where_expression = NULL, *group_items, *group_fields;
	zval *group_field = NULL, *group_expression = NULL, *group_sql;
	zval *group_clause, *having_conditions, *having_expression = NULL;
	zval *order_fields, *order_items, *order_item = NULL;
	zval *order_expression = NULL, *order_sql_item = NULL, *sql_order_type = NULL;
	zval *order_sql_item_type = NULL, *order_sql, *limit_value;
	zval *number, *offset, *tmp1 = NULL, *tmp2 = NULL;
	zval *z_one, *ini_range, *end_range = NULL, *sql_limit;
	HashTable *ah0, *ah1, *ah2, *ah3, *ah4, *ah5;
	HashPosition hp0, hp1, hp2, hp3, hp4, hp5;
	zval **hd;

	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &definition) == FAILURE) {
		RETURN_NULL();
	}

	if (Z_TYPE_P(definition) != IS_ARRAY) {
		PHALCON_THROW_EXCEPTION_STRW(phalcon_db_exception_ce, "Invalid SELECT definition");
		return;
	}
	if (!phalcon_array_isset_string(definition, SS("tables"))) {
		PHALCON_THROW_EXCEPTION_STRW(phalcon_db_exception_ce, "The index 'tables' is required in the definition array");
		return;
	}

	if (!phalcon_array_isset_string(definition, SS("columns"))) {
		PHALCON_THROW_EXCEPTION_STRW(phalcon_db_exception_ce, "The index 'columns' is required in the definition array");
		return;
	}

	PHALCON_MM_GROW();

	if (PHALCON_GLOBAL(db).escape_identifiers) {
		PHALCON_OBS_VAR(escape_char);
		phalcon_read_property_this(&escape_char, this_ptr, SL("_escapeChar"), PH_NOISY TSRMLS_CC);
	} else {
		PHALCON_INIT_NVAR(escape_char);
	}

	PHALCON_OBS_VAR(columns);
	phalcon_array_fetch_string(&columns, definition, SL("columns"), PH_NOISY);
	if (Z_TYPE_P(columns) == IS_ARRAY) {

		PHALCON_INIT_VAR(selected_columns);
		array_init(selected_columns);

		phalcon_is_iterable(columns, &ah0, &hp0, 0, 0);

		while (zend_hash_get_current_data_ex(ah0, (void**) &hd, &hp0) == SUCCESS) {

			PHALCON_GET_HVALUE(column);

			/**
			 * Escape column name
			 */
			PHALCON_OBS_NVAR(column_item);
			phalcon_array_fetch_long(&column_item, column, 0, PH_NOISY);
			if (Z_TYPE_P(column_item) == IS_ARRAY) {
				PHALCON_CALL_METHOD(&column_sql, this_ptr, "getsqlexpression", column_item, escape_char);
			} else if (PHALCON_IS_STRING(column_item, "*")) {
				PHALCON_CPY_WRT(column_sql, column_item);
			} else if (PHALCON_GLOBAL(db).escape_identifiers) {
				PHALCON_INIT_NVAR(column_sql);
				PHALCON_CONCAT_VVV(column_sql, escape_char, column_item, escape_char);
			} else {
				PHALCON_CPY_WRT(columns_sql, column_item);
			}

			/**
			 * Escape column domain
			 */
			if (phalcon_array_isset_long(column, 1)) {

				PHALCON_OBS_NVAR(column_domain);
				phalcon_array_fetch_long(&column_domain, column, 1, PH_NOISY);
				if (zend_is_true(column_domain)) {
					if (PHALCON_GLOBAL(db).escape_identifiers) {
						PHALCON_INIT_NVAR(column_domain_sql);
						PHALCON_CONCAT_VVVSV(column_domain_sql, escape_char, column_domain, escape_char, ".", column_sql);
					} else {
						PHALCON_INIT_NVAR(column_domain_sql);
						PHALCON_CONCAT_VSV(column_domain_sql, column_domain, ".", column_sql);
					}
				} else {
					PHALCON_CPY_WRT(column_domain_sql, column_sql);
				}
			} else {
				PHALCON_CPY_WRT(column_domain_sql, column_sql);
			}

			/**
			 * Escape column alias
			 */
			if (phalcon_array_isset_long(column, 2)) {

				PHALCON_OBS_NVAR(column_alias);
				phalcon_array_fetch_long(&column_alias, column, 2, PH_NOISY);
				if (zend_is_true(column_alias)) {
					if (PHALCON_GLOBAL(db).escape_identifiers) {
						PHALCON_INIT_NVAR(column_alias_sql);
						PHALCON_CONCAT_VSVVV(column_alias_sql, column_domain_sql, " ", escape_char, column_alias, escape_char);
					} else {
						PHALCON_INIT_NVAR(column_alias_sql);
						PHALCON_CONCAT_VSV(column_alias_sql, column_domain_sql, " ", column_alias);
					}
				} else {
					PHALCON_CPY_WRT(column_alias_sql, column_domain_sql);
				}
			} else {
				PHALCON_CPY_WRT(column_alias_sql, column_domain_sql);
			}

			phalcon_array_append(&selected_columns, column_alias_sql, PH_SEPARATE);

			zend_hash_move_forward_ex(ah0, &hp0);
		}

		PHALCON_INIT_VAR(columns_sql);
		phalcon_fast_join_str(columns_sql, SL(", "), selected_columns TSRMLS_CC);
	} else {
		PHALCON_CPY_WRT(columns_sql, columns);
	}

	/**
	 * Check and escape tables
	 */
	PHALCON_OBS_VAR(tables);
	phalcon_array_fetch_string(&tables, definition, SL("tables"), PH_NOISY);
	if (Z_TYPE_P(tables) == IS_ARRAY) {

		PHALCON_INIT_VAR(selected_tables);
		array_init(selected_tables);

		phalcon_is_iterable(tables, &ah1, &hp1, 0, 0);

		while (zend_hash_get_current_data_ex(ah1, (void**) &hd, &hp1) == SUCCESS) {

			PHALCON_GET_HVALUE(table);

			PHALCON_CALL_METHOD(&sql_table, this_ptr, "getsqltable", table, escape_char);
			phalcon_array_append(&selected_tables, sql_table, PH_SEPARATE);

			zend_hash_move_forward_ex(ah1, &hp1);
		}

		PHALCON_INIT_VAR(tables_sql);
		phalcon_fast_join_str(tables_sql, SL(", "), selected_tables TSRMLS_CC);
	} else {
		PHALCON_CPY_WRT(tables_sql, tables);
	}

	PHALCON_INIT_VAR(sql);
	if (phalcon_array_isset_string_fetch(&distinct, definition, SS("definition"))) {
		assert(Z_TYPE_P(distinct) == IS_LONG);
		if (Z_LVAL_P(distinct) == 0) {
			ZVAL_STRING(sql, "SELECT ALL ", 1);
		}
		else if (Z_LVAL_P(distinct) == 1) {
			ZVAL_STRING(sql, "SELECT DISTINCT ", 1);
		}
		else {
			ZVAL_STRING(sql, "SELECT ", 1);
		}
	}
	else {
		ZVAL_STRING(sql, "SELECT ", 1);
	}

	/**
	 * Check for joins
	 */
	if (phalcon_array_isset_string(definition, SS("joins"))) {

		PHALCON_OBS_VAR(joins);
		phalcon_array_fetch_string(&joins, definition, SL("joins"), PH_NOISY);

		phalcon_is_iterable(joins, &ah2, &hp2, 0, 0);

		while (zend_hash_get_current_data_ex(ah2, (void**) &hd, &hp2) == SUCCESS) {

			PHALCON_GET_HVALUE(join);

			PHALCON_OBS_NVAR(type);
			phalcon_array_fetch_string(&type, join, SL("type"), PH_NOISY);

			PHALCON_OBS_NVAR(table);
			phalcon_array_fetch_string(&table, join, SL("source"), PH_NOISY);

			PHALCON_CALL_METHOD(&sql_table, this_ptr, "getsqltable", table, escape_char);
			phalcon_array_append(&selected_tables, sql_table, PH_SEPARATE);

			PHALCON_INIT_NVAR(sql_join);
			PHALCON_CONCAT_SVSV(sql_join, " ", type, " JOIN ", sql_table);

			/**
			 * Check if the join has conditions
			 */
			if (phalcon_array_isset_string(join, SS("conditions"))) {

				PHALCON_OBS_NVAR(join_conditions_array);
				phalcon_array_fetch_string(&join_conditions_array, join, SL("conditions"), PH_NOISY);
				if (phalcon_fast_count_ev(join_conditions_array TSRMLS_CC)) {

					PHALCON_INIT_NVAR(join_expressions);
					array_init(join_expressions);

					phalcon_is_iterable(join_conditions_array, &ah3, &hp3, 0, 0);

					while (zend_hash_get_current_data_ex(ah3, (void**) &hd, &hp3) == SUCCESS) {

						PHALCON_GET_HVALUE(join_condition);

						PHALCON_CALL_METHOD(&join_expression, this_ptr, "getsqlexpression", join_condition, escape_char);
						phalcon_array_append(&join_expressions, join_expression, PH_SEPARATE);

						zend_hash_move_forward_ex(ah3, &hp3);
					}

					PHALCON_INIT_NVAR(join_conditions);
					phalcon_fast_join_str(join_conditions, SL(" AND "), join_expressions TSRMLS_CC);
					PHALCON_SCONCAT_SV(sql_join, " ON ", join_conditions);
				}
			}

			phalcon_concat_self(&sql, sql_join TSRMLS_CC);

			zend_hash_move_forward_ex(ah2, &hp2);
		}

	}

	/**
	 * Check for a WHERE clause
	 */
	if (phalcon_array_isset_string(definition, SS("where"))) {

		PHALCON_OBS_VAR(where_conditions);
		phalcon_array_fetch_string(&where_conditions, definition, SL("where"), PH_NOISY);
		if (Z_TYPE_P(where_conditions) == IS_ARRAY) {
			PHALCON_CALL_METHOD(&where_expression, this_ptr, "getsqlexpression", where_conditions, escape_char);
			PHALCON_SCONCAT_SV(sql, " WHERE ", where_expression);
		} else {
			PHALCON_SCONCAT_SV(sql, " WHERE ", where_conditions);
		}
	}

	/**
	 * Check for a GROUP clause
	 */
	if (phalcon_array_isset_string(definition, SS("group"))) {

		PHALCON_INIT_VAR(group_items);
		array_init(group_items);

		PHALCON_OBS_VAR(group_fields);
		phalcon_array_fetch_string(&group_fields, definition, SL("group"), PH_NOISY);

		phalcon_is_iterable(group_fields, &ah4, &hp4, 0, 0);

		while (zend_hash_get_current_data_ex(ah4, (void**) &hd, &hp4) == SUCCESS) {

			PHALCON_GET_HVALUE(group_field);

			PHALCON_CALL_METHOD(&group_expression, this_ptr, "getsqlexpression", group_field, escape_char);
			phalcon_array_append(&group_items, group_expression, PH_SEPARATE);

			zend_hash_move_forward_ex(ah4, &hp4);
		}

		PHALCON_INIT_VAR(group_sql);
		phalcon_fast_join_str(group_sql, SL(", "), group_items TSRMLS_CC);

		PHALCON_INIT_VAR(group_clause);
		PHALCON_CONCAT_SV(group_clause, " GROUP BY ", group_sql);
		phalcon_concat_self(&sql, group_clause TSRMLS_CC);
	}

	/**
	 * Check for a HAVING clause
	 */
	if (phalcon_array_isset_string(definition, SS("having"))) {
		PHALCON_OBS_VAR(having_conditions);
		phalcon_array_fetch_string(&having_conditions, definition, SL("having"), PH_NOISY);

		PHALCON_CALL_METHOD(&having_expression, this_ptr, "getsqlexpression", having_conditions, escape_char);
		PHALCON_SCONCAT_SV(sql, " HAVING ", having_expression);
	}

	/**
	 * Check for a ORDER clause
	 */
	if (phalcon_array_isset_string(definition, SS("order"))) {

		PHALCON_OBS_VAR(order_fields);
		phalcon_array_fetch_string(&order_fields, definition, SL("order"), PH_NOISY);

		PHALCON_INIT_VAR(order_items);
		array_init(order_items);

		phalcon_is_iterable(order_fields, &ah5, &hp5, 0, 0);

		while (zend_hash_get_current_data_ex(ah5, (void**) &hd, &hp5) == SUCCESS) {

			PHALCON_GET_HVALUE(order_item);

			PHALCON_OBS_NVAR(order_expression);
			phalcon_array_fetch_long(&order_expression, order_item, 0, PH_NOISY);

			PHALCON_CALL_METHOD(&order_sql_item, this_ptr, "getsqlexpression", order_expression, escape_char);

			/**
			 * In the numeric 1 position could be a ASC/DESC clause
			 */
			if (phalcon_array_isset_long(order_item, 1)) {
				PHALCON_OBS_NVAR(sql_order_type);
				phalcon_array_fetch_long(&sql_order_type, order_item, 1, PH_NOISY);

				PHALCON_INIT_NVAR(order_sql_item_type);
				PHALCON_CONCAT_VSV(order_sql_item_type, order_sql_item, " ", sql_order_type);
			} else {
				PHALCON_CPY_WRT(order_sql_item_type, order_sql_item);
			}

			phalcon_array_append(&order_items, order_sql_item_type, PH_SEPARATE);

			zend_hash_move_forward_ex(ah5, &hp5);
		}

		PHALCON_INIT_VAR(order_sql);
		phalcon_fast_join_str(order_sql, SL(", "), order_items TSRMLS_CC);
		PHALCON_SCONCAT_SV(sql, " ORDER BY ", order_sql);
	}

    /**
	 * Oracle does not implement the LIMIT clause as some RDBMS do.
	 * We have to simulate it with subqueries and ROWNUM.
	 * Unfortunately because we use the column wildcard "*",
	 * this puts an extra column into the query result set.
	 */
	if (phalcon_array_isset_string_fetch(&limit_value, definition, SS("limit"))) {
		if (likely(Z_TYPE_P(limit_value) == IS_ARRAY)) {
			if (likely(phalcon_array_isset_string_fetch(&number, limit_value, SS("number")))) {
				PHALCON_OBS_NVAR(tmp1);
				phalcon_array_fetch_string(&tmp1, number, SL("value"), PH_NOISY);

				if (phalcon_array_isset_string_fetch(&offset, limit_value, SS("offset"))) {
					PHALCON_OBS_NVAR(tmp2);
					phalcon_array_fetch_string(&tmp2, offset, SL("value"), PH_NOISY);
				} else {
					PHALCON_INIT_NVAR(tmp2);
					ZVAL_LONG(tmp2, 0);
				}

				z_one = PHALCON_GLOBAL(z_one);

				PHALCON_INIT_VAR(ini_range);
				phalcon_add_function(ini_range, tmp2, z_one TSRMLS_CC);

				PHALCON_INIT_VAR(end_range);
				phalcon_add_function(end_range, tmp2, tmp1 TSRMLS_CC);

				PHALCON_INIT_VAR(sql_limit);
				PHALCON_SCONCAT_SVSVSV(sql_limit,"SELECT Z2.* FROM (SELECT Z1.*, ROWNUM DB_ROWNUM FROM ( ", sql, " ) Z1 ) Z2 WHERE Z2.DB_ROWNUM BETWEEN ", ini_range , " AND ",  end_range );
				PHALCON_CPY_WRT(sql, sql_limit);
			}
		} else {

			PHALCON_INIT_VAR(ini_range);
			ZVAL_LONG(ini_range, 1);

			PHALCON_CPY_WRT(end_range, limit_value);

			PHALCON_INIT_VAR(sql_limit);
			PHALCON_SCONCAT_SVSVSV(sql_limit,"SELECT Z2.* FROM (SELECT Z1.*, ROWNUM DB_ROWNUM FROM ( ", sql, " ) Z1 ) Z2 WHERE Z2.DB_ROWNUM BETWEEN ", ini_range , " AND ",  end_range );
			PHALCON_CPY_WRT(sql, sql_limit);
		}
	}

	RETURN_CTOR(sql);
}
Пример #29
0
/**
 * Initialize the metadata for certain table
 *
 * @param Phalcon\Mvc\ModelInterface $model
 * @param string $key
 * @param string $table
 * @param string $schema
 */
PHP_METHOD(Phalcon_Mvc_Model_MetaData, _initialize){

	zval *model, *key, *table, *schema, *class_name = NULL, *meta_data = NULL;
	zval *data = NULL, *table_metadata = NULL, *exception_message = NULL;
	zval *connection, *exists, *complete_table = NULL, *columns;
	zval *attributes, *primary_keys, *non_primary_keys;
	zval *numeric_typed, *not_null, *field_types;
	zval *field_bind_types, *automatic_default;
	zval *identity_field = NULL, *column = NULL, *field_name = NULL, *feature = NULL;
	zval *type = NULL, *bind_type = NULL, *column_map = NULL, *ordered_column_map = NULL;
	zval *reversed_column_map = NULL, *user_column_map;
	zval *user_name = NULL, *name = NULL, *model_column_map;
	zval *t0 = NULL;
	HashTable *ah0, *ah1;
	HashPosition hp0, hp1;
	zval **hd;
	char *hash_index;
	uint hash_index_len;
	ulong hash_num;
	int hash_type;
	int eval_int;

	PHALCON_MM_GROW();

	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zzzz", &model, &key, &table, &schema) == FAILURE) {
		PHALCON_MM_RESTORE();
		RETURN_NULL();
	}

	PHALCON_INIT_VAR(class_name);
	phalcon_get_class(class_name, model TSRMLS_CC);
	if (Z_TYPE_P(key) != IS_NULL) {
		PHALCON_INIT_VAR(meta_data);
		phalcon_read_property(&meta_data, this_ptr, SL("_metaData"), PH_NOISY_CC);
		eval_int = phalcon_array_isset(meta_data, key);
		if (!eval_int) {
			/** 
			 * The meta-data is read from the adapter always
			 */
			PHALCON_INIT_VAR(data);
			PHALCON_CALL_METHOD_PARAMS_1(data, this_ptr, "read", key, PH_NO_CHECK);
			if (Z_TYPE_P(data) != IS_NULL) {
				if (Z_TYPE_P(meta_data) != IS_ARRAY) { 
					PHALCON_INIT_NVAR(meta_data);
					array_init(meta_data);
				}
				phalcon_array_update_zval(&meta_data, key, &data, PH_COPY | PH_SEPARATE TSRMLS_CC);
				phalcon_update_property_zval(this_ptr, SL("_metaData"), meta_data TSRMLS_CC);
			} else {
				/** 
				 * Check if there is a method 'metaData' in the model and retrieve data from it
				 */
				if (phalcon_method_exists_ex(model, SS("metadata") TSRMLS_CC) == SUCCESS) {
					PHALCON_INIT_VAR(table_metadata);
					PHALCON_CALL_METHOD(table_metadata, model, "metadata", PH_NO_CHECK);
					if (Z_TYPE_P(table_metadata) != IS_ARRAY) { 
						PHALCON_INIT_VAR(exception_message);
						PHALCON_CONCAT_SV(exception_message, "Invalid meta-data for model ", class_name);
						PHALCON_THROW_EXCEPTION_ZVAL(phalcon_mvc_model_exception_ce, exception_message);
						return;
					}
				} else {
					/** 
					 * Check if the mapped table exists on the database
					 */
					PHALCON_INIT_VAR(connection);
					PHALCON_CALL_METHOD(connection, model, "getconnection", PH_NO_CHECK);
	
					PHALCON_INIT_VAR(exists);
					PHALCON_CALL_METHOD_PARAMS_2(exists, connection, "tableexists", table, schema, PH_NO_CHECK);
					if (!zend_is_true(exists)) {
						if (zend_is_true(schema)) {
							PHALCON_INIT_VAR(complete_table);
							PHALCON_CONCAT_VSV(complete_table, schema, "\".\"", table);
						} else {
							PHALCON_CPY_WRT(complete_table, table);
						}
	
						PHALCON_INIT_NVAR(exception_message);
						PHALCON_CONCAT_SVSV(exception_message, "Table \"", complete_table, "\" doesn't exist on database when dumping meta-data for ", class_name);
						PHALCON_THROW_EXCEPTION_ZVAL(phalcon_mvc_model_exception_ce, exception_message);
						return;
					}
	
					/** 
					 * Try to describe the table
					 */
					PHALCON_INIT_VAR(columns);
					PHALCON_CALL_METHOD_PARAMS_2(columns, connection, "describecolumns", table, schema, PH_NO_CHECK);
					if (!phalcon_fast_count_ev(columns TSRMLS_CC)) {
						if (zend_is_true(schema)) {
							PHALCON_INIT_NVAR(complete_table);
							PHALCON_CONCAT_VSV(complete_table, schema, "\".\"", table);
						} else {
							PHALCON_CPY_WRT(complete_table, table);
						}
	
						PHALCON_INIT_NVAR(exception_message);
						PHALCON_CONCAT_SVSV(exception_message, "Cannot obtain table columns for the mapped source \"", complete_table, "\" used in model ", class_name);
						PHALCON_THROW_EXCEPTION_ZVAL(phalcon_mvc_model_exception_ce, exception_message);
						return;
					}
	
					/** 
					 * Initialize meta-data
					 */
					PHALCON_INIT_VAR(attributes);
					array_init(attributes);
	
					PHALCON_INIT_VAR(primary_keys);
					array_init(primary_keys);
	
					PHALCON_INIT_VAR(non_primary_keys);
					array_init(non_primary_keys);
	
					PHALCON_INIT_VAR(numeric_typed);
					array_init(numeric_typed);
	
					PHALCON_INIT_VAR(not_null);
					array_init(not_null);
	
					PHALCON_INIT_VAR(field_types);
					array_init(field_types);
	
					PHALCON_INIT_VAR(field_bind_types);
					array_init(field_bind_types);
	
					PHALCON_INIT_VAR(automatic_default);
					array_init(automatic_default);
	
					PHALCON_INIT_VAR(identity_field);
					ZVAL_BOOL(identity_field, 0);
	
					if (!phalcon_valid_foreach(columns TSRMLS_CC)) {
						return;
					}
	
					ah0 = Z_ARRVAL_P(columns);
					zend_hash_internal_pointer_reset_ex(ah0, &hp0);
	
					ph_cycle_start_0:
	
						if (zend_hash_get_current_data_ex(ah0, (void**) &hd, &hp0) != SUCCESS) {
							goto ph_cycle_end_0;
						}
	
						PHALCON_GET_FOREACH_VALUE(column);
	
						PHALCON_INIT_NVAR(field_name);
						PHALCON_CALL_METHOD(field_name, column, "getname", PH_NO_CHECK);
						phalcon_array_append(&attributes, field_name, PH_SEPARATE TSRMLS_CC);
	
						PHALCON_INIT_NVAR(feature);
						PHALCON_CALL_METHOD(feature, column, "isprimary", PH_NO_CHECK);
						if (PHALCON_IS_TRUE(feature)) {
							phalcon_array_append(&primary_keys, field_name, PH_SEPARATE TSRMLS_CC);
						} else {
							phalcon_array_append(&non_primary_keys, field_name, PH_SEPARATE TSRMLS_CC);
						}
	
						PHALCON_INIT_NVAR(feature);
						PHALCON_CALL_METHOD(feature, column, "isnumeric", PH_NO_CHECK);
						if (PHALCON_IS_TRUE(feature)) {
							phalcon_array_update_zval_bool(&numeric_typed, field_name, 1, PH_SEPARATE TSRMLS_CC);
						}
	
						PHALCON_INIT_NVAR(feature);
						PHALCON_CALL_METHOD(feature, column, "isnotnull", PH_NO_CHECK);
						if (PHALCON_IS_TRUE(feature)) {
							phalcon_array_append(&not_null, field_name, PH_SEPARATE TSRMLS_CC);
						}
	
						PHALCON_INIT_NVAR(feature);
						PHALCON_CALL_METHOD(feature, column, "isautoincrement", PH_NO_CHECK);
						if (PHALCON_IS_TRUE(feature)) {
							PHALCON_CPY_WRT(identity_field, field_name);
						}
	
						PHALCON_INIT_NVAR(type);
						PHALCON_CALL_METHOD(type, column, "gettype", PH_NO_CHECK);
						phalcon_array_update_zval(&field_types, field_name, &type, PH_COPY | PH_SEPARATE TSRMLS_CC);
	
						PHALCON_INIT_NVAR(bind_type);
						PHALCON_CALL_METHOD(bind_type, column, "getbindtype", PH_NO_CHECK);
						phalcon_array_update_zval(&field_bind_types, field_name, &bind_type, PH_COPY | PH_SEPARATE TSRMLS_CC);
	
						zend_hash_move_forward_ex(ah0, &hp0);
						goto ph_cycle_start_0;
	
					ph_cycle_end_0:
	
					/** 
					 * Create an array using the MODELS_* constants as indexes
					 */
					PHALCON_INIT_NVAR(table_metadata);
					array_init(table_metadata);
					phalcon_array_update_long(&table_metadata, 0, &attributes, PH_COPY | PH_SEPARATE TSRMLS_CC);
					phalcon_array_update_long(&table_metadata, 1, &primary_keys, PH_COPY | PH_SEPARATE TSRMLS_CC);
					phalcon_array_update_long(&table_metadata, 2, &non_primary_keys, PH_COPY | PH_SEPARATE TSRMLS_CC);
					phalcon_array_update_long(&table_metadata, 3, &not_null, PH_COPY | PH_SEPARATE TSRMLS_CC);
					phalcon_array_update_long(&table_metadata, 4, &field_types, PH_COPY | PH_SEPARATE TSRMLS_CC);
					phalcon_array_update_long(&table_metadata, 5, &numeric_typed, PH_COPY | PH_SEPARATE TSRMLS_CC);
					phalcon_array_update_long(&table_metadata, 8, &identity_field, PH_COPY | PH_SEPARATE TSRMLS_CC);
					phalcon_array_update_long(&table_metadata, 9, &field_bind_types, PH_COPY | PH_SEPARATE TSRMLS_CC);
					phalcon_array_update_long(&table_metadata, 10, &automatic_default, PH_COPY | PH_SEPARATE TSRMLS_CC);
					phalcon_array_update_long(&table_metadata, 11, &automatic_default, PH_COPY | PH_SEPARATE TSRMLS_CC);
				}
	
				PHALCON_INIT_VAR(t0);
				phalcon_read_property(&t0, this_ptr, SL("_metaData"), PH_NOISY_CC);
				phalcon_array_update_zval(&t0, key, &table_metadata, PH_COPY TSRMLS_CC);
				phalcon_update_property_zval(this_ptr, SL("_metaData"), t0 TSRMLS_CC);
				PHALCON_CALL_METHOD_PARAMS_2_NORETURN(this_ptr, "write", key, table_metadata, PH_NO_CHECK);
			}
		}
	}
	
	/** 
	 * Check for a column map, store in _columnMap in order and reversed order
	 */
	PHALCON_INIT_NVAR(class_name);
	phalcon_get_class(class_name, model TSRMLS_CC);
	
	PHALCON_INIT_VAR(column_map);
	phalcon_read_property(&column_map, this_ptr, SL("_columnMap"), PH_NOISY_CC);
	eval_int = phalcon_array_isset(column_map, class_name);
	if (!eval_int) {
		if (Z_TYPE_P(column_map) != IS_ARRAY) { 
			PHALCON_INIT_NVAR(column_map);
			array_init(column_map);
		}
	
		PHALCON_INIT_NVAR(data);
		PHALCON_CALL_METHOD_PARAMS_1(data, this_ptr, "read", class_name, PH_NO_CHECK);
		if (Z_TYPE_P(data) != IS_NULL) {
			phalcon_array_update_zval(&column_map, class_name, &data, PH_COPY | PH_SEPARATE TSRMLS_CC);
			phalcon_update_property_zval(this_ptr, SL("_columnMap"), column_map TSRMLS_CC);
		} else {
			PHALCON_INIT_VAR(ordered_column_map);
	
			PHALCON_INIT_VAR(reversed_column_map);
	
			/** 
			 * Check for a columnMap() method on the model
			 */
			if (phalcon_method_exists_ex(model, SS("columnmap") TSRMLS_CC) == SUCCESS) {
				PHALCON_INIT_VAR(user_column_map);
				PHALCON_CALL_METHOD(user_column_map, model, "columnmap", PH_NO_CHECK);
				if (Z_TYPE_P(user_column_map) != IS_ARRAY) { 
					PHALCON_THROW_EXCEPTION_STR(phalcon_mvc_model_exception_ce, "columnMap() not returned an array");
					return;
				}
	
				array_init(reversed_column_map);
				PHALCON_CPY_WRT(ordered_column_map, user_column_map);
	
				if (!phalcon_valid_foreach(user_column_map TSRMLS_CC)) {
					return;
				}
	
				ah1 = Z_ARRVAL_P(user_column_map);
				zend_hash_internal_pointer_reset_ex(ah1, &hp1);
	
				ph_cycle_start_1:
	
					if (zend_hash_get_current_data_ex(ah1, (void**) &hd, &hp1) != SUCCESS) {
						goto ph_cycle_end_1;
					}
	
					PHALCON_GET_FOREACH_KEY(name, ah1, hp1);
					PHALCON_GET_FOREACH_VALUE(user_name);
	
					phalcon_array_update_zval(&reversed_column_map, user_name, &name, PH_COPY | PH_SEPARATE TSRMLS_CC);
	
					zend_hash_move_forward_ex(ah1, &hp1);
					goto ph_cycle_start_1;
	
				ph_cycle_end_1:
				if(0){}
	
			}
	
			PHALCON_INIT_VAR(model_column_map);
			array_init(model_column_map);
			phalcon_array_update_long(&model_column_map, 0, &ordered_column_map, PH_COPY | PH_SEPARATE TSRMLS_CC);
			phalcon_array_update_long(&model_column_map, 1, &reversed_column_map, PH_COPY | PH_SEPARATE TSRMLS_CC);
			phalcon_array_update_zval(&column_map, class_name, &model_column_map, PH_COPY | PH_SEPARATE TSRMLS_CC);
			phalcon_update_property_zval(this_ptr, SL("_columnMap"), column_map TSRMLS_CC);
			PHALCON_CALL_METHOD_PARAMS_2_NORETURN(this_ptr, "write", class_name, model_column_map, PH_NO_CHECK);
		}
	}
	
	PHALCON_MM_RESTORE();
}