Example #1
0
/**
 * Check whether the relation is a 'many-to-many' relation or not
 *
 * @return boolean
 */
PHP_METHOD(Phalcon_Mvc_Model_Relation, isThrough) {

    zval *type;

    type = phalcon_read_property(getThis(), SL("_type"), PH_NOISY);
    if (PHALCON_IS_LONG(type, 3) || PHALCON_IS_LONG(type, 4)) {
        RETURN_TRUE;
    }

    RETURN_FALSE;
}
Example #2
0
/**
 * Get first row in the resultset
 *
 * @return Phalcon\Mvc\ModelInterface
 */
PHP_METHOD(Phalcon_Mvc_Model_Resultset, getFirst){

	zval *pointer, *valid = NULL;

	PHALCON_MM_GROW();

	/** 
	 * Check if the last record returned is the current requested
	 */
	pointer = phalcon_read_property(getThis(), SL("_pointer"), PH_NOISY);
	if (PHALCON_IS_LONG(pointer, 0)) {
		PHALCON_RETURN_CALL_METHOD(getThis(), "current");
		RETURN_MM();
	}

	/** 
	 * Otherwise re-execute the statement
	 */
	PHALCON_CALL_METHOD(NULL, getThis(), "rewind");

	PHALCON_CALL_METHOD(&valid, getThis(), "valid");
	if (PHALCON_IS_NOT_FALSE(valid)) {
		PHALCON_RETURN_CALL_METHOD(getThis(), "current");
		RETURN_MM();
	}

	RETURN_MM_FALSE;
}
Example #3
0
/**
 * Get first row in the resultset
 *
 * @return Phalcon\Mvc\ModelInterface
 */
PHP_METHOD(Phalcon_Mvc_Model_Resultset, getFirst){

	zval *pointer, *current = NULL, *valid;

	PHALCON_MM_GROW();

	/** 
	 * Check if the last record returned is the current requested
	 */
	PHALCON_OBS_VAR(pointer);
	phalcon_read_property_this(&pointer, this_ptr, SL("_pointer"), PH_NOISY_CC);
	if (PHALCON_IS_LONG(pointer, 0)) {
		PHALCON_INIT_VAR(current);
		phalcon_call_method(current, this_ptr, "current");
		RETURN_CCTOR(current);
	}
	
	/** 
	 * Otherwise re-execute the statement
	 */
	phalcon_call_method_noret(this_ptr, "rewind");
	
	PHALCON_INIT_VAR(valid);
	phalcon_call_method(valid, this_ptr, "valid");
	if (PHALCON_IS_NOT_FALSE(valid)) {
		PHALCON_INIT_NVAR(current);
		phalcon_call_method(current, this_ptr, "current");
		RETURN_CCTOR(current);
	}
	
	RETURN_MM_FALSE;
}
Example #4
0
/**
 * Phalcon\Db\Column constructor
 *
 * @param string $columnName
 * @param array $definition
 */
PHP_METHOD(Phalcon_Db_Column, __construct){

	zval *column_name, *definition, *type, *not_null;
	zval *primary, *size, *is_numeric = NULL, *scale, *dunsigned;
	zval *auto_increment, *first, *after, *bind_type;
	zval *t0 = NULL, *t1 = NULL;

	PHALCON_MM_GROW();

	phalcon_fetch_params(1, 2, 0, &column_name, &definition);
	
	phalcon_update_property_this(this_ptr, SL("_columnName"), column_name TSRMLS_CC);
	
	/** 
	 * Get the column type, one of the TYPE_* constants
	 */
	if (phalcon_array_isset_string(definition, SS("type"))) {
		PHALCON_OBS_VAR(type);
		phalcon_array_fetch_string(&type, definition, SL("type"), PH_NOISY_CC);
		phalcon_update_property_this(this_ptr, SL("_type"), type TSRMLS_CC);
	} else {
		PHALCON_THROW_EXCEPTION_STR(phalcon_db_exception_ce, "Column type is required");
		return;
	}
	
	/** 
	 * Check if the field is nullable
	 */
	if (phalcon_array_isset_string(definition, SS("notNull"))) {
		PHALCON_OBS_VAR(not_null);
		phalcon_array_fetch_string(&not_null, definition, SL("notNull"), PH_NOISY_CC);
		phalcon_update_property_this(this_ptr, SL("_notNull"), not_null TSRMLS_CC);
	}
	
	/** 
	 * Check if the field is primary key
	 */
	if (phalcon_array_isset_string(definition, SS("primary"))) {
		PHALCON_OBS_VAR(primary);
		phalcon_array_fetch_string(&primary, definition, SL("primary"), PH_NOISY_CC);
		phalcon_update_property_this(this_ptr, SL("_primary"), primary TSRMLS_CC);
	}
	
	if (phalcon_array_isset_string(definition, SS("size"))) {
		PHALCON_OBS_VAR(size);
		phalcon_array_fetch_string(&size, definition, SL("size"), PH_NOISY_CC);
		phalcon_update_property_this(this_ptr, SL("_size"), size TSRMLS_CC);
	}
	
	/** 
	 * Check if the column has a decimal scale
	 */
	if (phalcon_array_isset_string(definition, SS("scale"))) {
	
		PHALCON_INIT_VAR(t0);
		ZVAL_LONG(t0, 3);
		PHALCON_INIT_VAR(is_numeric);
		is_equal_function(is_numeric, type, t0 TSRMLS_CC);
		if (PHALCON_IS_NOT_TRUE(is_numeric)) {
			PHALCON_INIT_VAR(t1);
			ZVAL_LONG(t1, 7);
			is_equal_function(is_numeric, type, t1 TSRMLS_CC);
		}
	
		if (PHALCON_IS_TRUE(is_numeric)) {
			PHALCON_OBS_VAR(scale);
			phalcon_array_fetch_string(&scale, definition, SL("scale"), PH_NOISY_CC);
			phalcon_update_property_this(this_ptr, SL("_scale"), scale TSRMLS_CC);
		} else {
			PHALCON_THROW_EXCEPTION_STR(phalcon_db_exception_ce, "Column type does not support scale parameter");
			return;
		}
	}
	
	/** 
	 * Check if the field is unsigned (only MySQL)
	 */
	if (phalcon_array_isset_string(definition, SS("unsigned"))) {
		PHALCON_OBS_VAR(dunsigned);
		phalcon_array_fetch_string(&dunsigned, definition, SL("unsigned"), PH_NOISY_CC);
		phalcon_update_property_this(this_ptr, SL("_unsigned"), dunsigned TSRMLS_CC);
	}
	
	/** 
	 * Check if the field is numeric
	 */
	if (phalcon_array_isset_string(definition, SS("isNumeric"))) {
		PHALCON_OBS_NVAR(is_numeric);
		phalcon_array_fetch_string(&is_numeric, definition, SL("isNumeric"), PH_NOISY_CC);
		phalcon_update_property_this(this_ptr, SL("_isNumeric"), is_numeric TSRMLS_CC);
	}
	
	/** 
	 * Check if the field is auto-increment/serial
	 */
	if (phalcon_array_isset_string(definition, SS("autoIncrement"))) {
		if (PHALCON_IS_LONG(type, 0)) {
			PHALCON_OBS_VAR(auto_increment);
			phalcon_array_fetch_string(&auto_increment, definition, SL("autoIncrement"), PH_NOISY_CC);
			phalcon_update_property_this(this_ptr, SL("_autoIncrement"), auto_increment TSRMLS_CC);
		} else {
			PHALCON_THROW_EXCEPTION_STR(phalcon_db_exception_ce, "Column type cannot be auto-increment");
			return;
		}
	}
	
	/** 
	 * Check if the field is placed at the first position of the table
	 */
	if (phalcon_array_isset_string(definition, SS("first"))) {
		PHALCON_OBS_VAR(first);
		phalcon_array_fetch_string(&first, definition, SL("first"), PH_NOISY_CC);
		phalcon_update_property_this(this_ptr, SL("_first"), first TSRMLS_CC);
	}
	
	/** 
	 * Name of the column which is placed before the current field
	 */
	if (phalcon_array_isset_string(definition, SS("after"))) {
		PHALCON_OBS_VAR(after);
		phalcon_array_fetch_string(&after, definition, SL("after"), PH_NOISY_CC);
		phalcon_update_property_this(this_ptr, SL("_after"), after TSRMLS_CC);
	}
	
	/** 
	 * The bind type to cast the field when passing it to PDO
	 */
	if (phalcon_array_isset_string(definition, SS("bindType"))) {
		PHALCON_OBS_VAR(bind_type);
		phalcon_array_fetch_string(&bind_type, definition, SL("bindType"), PH_NOISY_CC);
		phalcon_update_property_this(this_ptr, SL("_bindType"), bind_type TSRMLS_CC);
	}
	
	PHALCON_MM_RESTORE();
}
Example #5
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);
}
Example #6
0
/**
 * Returns a slice of the resultset to show in the pagination
 *
 * @return \stdClass
 */
PHP_METHOD(Phalcon_Paginator_Adapter_Model, getPaginate) {

    zval show = {}, config = {}, items = {}, page_number = {}, rowcount = {}, page = {}, last_show_page = {}, start = {}, possible_pages = {}, total_pages = {};
    zval page_items = {}, maximum_pages = {}, next = {}, additional_page = {}, before = {}, remainder = {}, pages_total = {};
    long int i, i_show;

    phalcon_return_property(&show, getThis(), SL("_limitRows"));
    phalcon_return_property(&config, getThis(), SL("_config"));
    phalcon_return_property(&page_number, getThis(), SL("_page"));

    i_show = phalcon_get_intval(&show);

    phalcon_array_fetch_str(&items, &config, SL("data"), PH_NOISY);

    if (Z_TYPE(page_number) == IS_NULL || PHALCON_LT(&show, &PHALCON_GLOBAL(z_zero))) {
        PHALCON_CPY_WRT_CTOR(&page_number, &PHALCON_GLOBAL(z_one));
    }

    phalcon_fast_count(&rowcount, &items);

    object_init(&page);

    phalcon_sub_function(&last_show_page, &page_number, &PHALCON_GLOBAL(z_one));

    mul_function(&start, &show, &last_show_page);
    phalcon_div_function(&possible_pages, &rowcount, &show);

    if (unlikely(Z_TYPE(possible_pages)) != IS_DOUBLE) {
        convert_to_double(&possible_pages);
    }

    ZVAL_LONG(&total_pages, (long int)ceil(Z_DVAL(possible_pages)));
    if (Z_TYPE(items) != IS_OBJECT) {
        PHALCON_THROW_EXCEPTION_STRW(phalcon_paginator_exception_ce, "Invalid data for paginator");
        return;
    }

    array_init(&page_items);
    if (PHALCON_GT(&rowcount, &PHALCON_GLOBAL(z_zero))) {
        /**
         * Seek to the desired position
         */
        if (PHALCON_LT(&start, &rowcount)) {
            PHALCON_CALL_METHODW(NULL, &items, "seek", &start);
        } else {
            PHALCON_CALL_METHODW(NULL, &items, "rewind");
            PHALCON_CPY_WRT_CTOR(&page_number, &PHALCON_GLOBAL(z_one));
            PHALCON_CPY_WRT_CTOR(&start, &PHALCON_GLOBAL(z_zero));
        }

        /**
         * The record must be iterable
         */
        for (i=1; ; ++i) {
            zval valid = {}, current = {};
            PHALCON_CALL_METHODW(&valid, &items, "valid");
            if (!PHALCON_IS_NOT_FALSE(&valid)) {
                break;
            }

            PHALCON_CALL_METHODW(&current, &items, "current");
            phalcon_array_append(&page_items, &current, PH_COPY);

            if (i >= i_show) {
                break;
            }
        }
    }

    phalcon_update_property_zval(&page, SL("items"), &page_items);

    phalcon_add_function(&maximum_pages, &start, &show);
    if (PHALCON_LT(&maximum_pages, &rowcount)) {
        phalcon_add_function(&next, &page_number, &PHALCON_GLOBAL(z_one));
    } else if (PHALCON_IS_EQUAL(&maximum_pages, &rowcount)) {
        PHALCON_CPY_WRT_CTOR(&next, &rowcount);
    } else {
        phalcon_div_function(&possible_pages, &rowcount, &show);

        phalcon_add_function(&additional_page, &possible_pages, &PHALCON_GLOBAL(z_one));

        ZVAL_LONG(&next, phalcon_get_intval(&additional_page));
    }

    if (PHALCON_GT(&next, &total_pages)) {
        PHALCON_CPY_WRT_CTOR(&next, &total_pages);
    }

    phalcon_update_property_zval(&page, SL("next"), &next);
    if (PHALCON_GT(&page_number, &PHALCON_GLOBAL(z_one))) {
        phalcon_sub_function(&before, &page_number, &PHALCON_GLOBAL(z_one));
    } else {
        PHALCON_CPY_WRT_CTOR(&before, &PHALCON_GLOBAL(z_one));
    }

    phalcon_update_property_zval(&page, SL("first"), &PHALCON_GLOBAL(z_one));
    phalcon_update_property_zval(&page, SL("before"), &before);
    phalcon_update_property_zval(&page, SL("current"), &page_number);

    mod_function(&remainder, &rowcount, &show);

    phalcon_div_function(&possible_pages, &rowcount, &show);
    if (!PHALCON_IS_LONG(&remainder, 0)) {
        phalcon_add_function(&next, &possible_pages, &PHALCON_GLOBAL(z_one));

        ZVAL_LONG(&pages_total, phalcon_get_intval(&next));
    } else {
        PHALCON_CPY_WRT_CTOR(&pages_total, &possible_pages);
    }

    phalcon_update_property_zval(&page, SL("last"), &pages_total);
    phalcon_update_property_zval(&page, SL("total_pages"), &pages_total);
    phalcon_update_property_zval(&page, SL("total_items"), &rowcount);

    RETURN_CTORW(&page);
}
Example #7
0
/**
 * Executes the validator
 *
 * @param Phalcon\Mvc\ModelInterface $record
 * @return boolean
 */
PHP_METHOD(Phalcon_Mvc_Model_Validator_Uniqueness, validate){

	zval *record, *option = NULL, *field, *dependency_injector;
	zval *service, *meta_data, *bind_types, *bind_data_types;
	zval *column_map = NULL, *conditions, *bind_params;
	zval *number = NULL, *compose_field = NULL, *column_field = NULL;
	zval *exception_message = NULL, *value = NULL, *compose_condition = NULL;
	zval *bind_type = NULL, *condition = NULL, *operation_made;
	zval *primary_fields, *primary_field = NULL, *attribute_field = NULL;
	zval *join_conditions, *params, *class_name;
	zval *message = NULL, *join_fields, *type;
	HashTable *ah0, *ah1;
	HashPosition hp0, hp1;
	zval **hd;

	PHALCON_MM_GROW();

	phalcon_fetch_params(1, 1, 0, &record);
	
	PHALCON_INIT_VAR(option);
	ZVAL_STRING(option, "field", 1);
	
	PHALCON_INIT_VAR(field);
	phalcon_call_method_p1(field, this_ptr, "getoption", option);
	
	PHALCON_INIT_VAR(dependency_injector);
	phalcon_call_method(dependency_injector, record, "getdi");
	
	PHALCON_INIT_VAR(service);
	ZVAL_STRING(service, "modelsMetadata", 1);
	
	PHALCON_INIT_VAR(meta_data);
	phalcon_call_method_p1(meta_data, dependency_injector, "getshared", service);
	
	/** 
	 * PostgreSQL check if the compared constant has the same type as the column, so we
	 * make cast to the data passed to match those column types
	 */
	PHALCON_INIT_VAR(bind_types);
	array_init(bind_types);
	
	PHALCON_INIT_VAR(bind_data_types);
	phalcon_call_method_p1(bind_data_types, meta_data, "getbindtypes", record);
	if (PHALCON_GLOBAL(orm).column_renaming) {
		PHALCON_INIT_VAR(column_map);
		phalcon_call_method_p1(column_map, meta_data, "getreversecolumnmap", record);
	} else {
		PHALCON_INIT_NVAR(column_map);
	}
	
	PHALCON_INIT_VAR(conditions);
	array_init(conditions);
	
	PHALCON_INIT_VAR(bind_params);
	array_init(bind_params);
	
	PHALCON_INIT_VAR(number);
	ZVAL_LONG(number, 0);
	if (Z_TYPE_P(field) == IS_ARRAY) { 
	
		/** 
		 * The field can be an array of values
		 */
		phalcon_is_iterable(field, &ah0, &hp0, 0, 0);
	
		while (zend_hash_get_current_data_ex(ah0, (void**) &hd, &hp0) == SUCCESS) {
	
			PHALCON_GET_HVALUE(compose_field);
	
			/** 
			 * The reversed column map is used in the case to get real column name
			 */
			if (Z_TYPE_P(column_map) == IS_ARRAY) { 
				if (phalcon_array_isset(column_map, compose_field)) {
					PHALCON_OBS_NVAR(column_field);
					phalcon_array_fetch(&column_field, column_map, compose_field, PH_NOISY);
				} else {
					PHALCON_INIT_NVAR(exception_message);
					PHALCON_CONCAT_SVS(exception_message, "Column '", compose_field, "\" isn't part of the column map");
					PHALCON_THROW_EXCEPTION_ZVAL(phalcon_mvc_model_exception_ce, exception_message);
					return;
				}
			} else {
				PHALCON_CPY_WRT(column_field, compose_field);
			}
	
			/** 
			 * Some database systems require that we pass the values using bind casting
			 */
			if (!phalcon_array_isset(bind_data_types, column_field)) {
				PHALCON_INIT_NVAR(exception_message);
				PHALCON_CONCAT_SVS(exception_message, "Column '", column_field, "\" isn't part of the table columns");
				PHALCON_THROW_EXCEPTION_ZVAL(phalcon_mvc_model_exception_ce, exception_message);
				return;
			}
	
			/** 
			 * The attribute could be "protected" so we read using "readattribute"
			 */
			PHALCON_INIT_NVAR(value);
			phalcon_call_method_p1(value, record, "readattribute", compose_field);
	
			PHALCON_INIT_NVAR(compose_condition);
			PHALCON_CONCAT_SVSV(compose_condition, "[", compose_field, "] = ?", number);
			phalcon_array_append(&conditions, compose_condition, PH_SEPARATE);
			phalcon_array_append(&bind_params, value, PH_SEPARATE);
	
			PHALCON_OBS_NVAR(bind_type);
			phalcon_array_fetch(&bind_type, bind_data_types, column_field, PH_NOISY);
			phalcon_array_append(&bind_types, bind_type, PH_SEPARATE);
			phalcon_increment(number);
	
			zend_hash_move_forward_ex(ah0, &hp0);
		}
	
	} else {
		/** 
		 * The reversed column map is used in the case to get real column name
		 */
		if (Z_TYPE_P(column_map) == IS_ARRAY) { 
			if (phalcon_array_isset(column_map, field)) {
				PHALCON_OBS_NVAR(column_field);
				phalcon_array_fetch(&column_field, column_map, field, PH_NOISY);
			} else {
				PHALCON_INIT_NVAR(exception_message);
				PHALCON_CONCAT_SVS(exception_message, "Column '", field, "\" isn't part of the column map");
				PHALCON_THROW_EXCEPTION_ZVAL(phalcon_mvc_model_exception_ce, exception_message);
				return;
			}
		} else {
			PHALCON_CPY_WRT(column_field, field);
		}
	
		/** 
		 * Some database systems require that we pass the values using bind casting
		 */
		if (!phalcon_array_isset(bind_data_types, column_field)) {
			PHALCON_INIT_NVAR(exception_message);
			PHALCON_CONCAT_SVS(exception_message, "Column '", column_field, "\" isn't part of the table columns");
			PHALCON_THROW_EXCEPTION_ZVAL(phalcon_mvc_model_exception_ce, exception_message);
			return;
		}
	
		/** 
		 * We're checking the uniqueness with only one field
		 */
		PHALCON_INIT_NVAR(value);
		phalcon_call_method_p1(value, record, "readattribute", field);
	
		PHALCON_INIT_VAR(condition);
		PHALCON_CONCAT_SVS(condition, "[", field, "] = ?0");
		phalcon_array_append(&conditions, condition, PH_SEPARATE);
		phalcon_array_append(&bind_params, value, PH_SEPARATE);
	
		PHALCON_OBS_NVAR(bind_type);
		phalcon_array_fetch(&bind_type, bind_data_types, column_field, PH_NOISY);
		phalcon_array_append(&bind_types, bind_type, PH_SEPARATE);
		phalcon_increment(number);
	}
	
	/** 
	 * If the operation is update, there must be values in the object
	 */
	PHALCON_INIT_VAR(operation_made);
	phalcon_call_method(operation_made, record, "getoperationmade");
	if (PHALCON_IS_LONG(operation_made, 2)) {
	
		/** 
		 * We build a query with the primary key attributes
		 */
		if (PHALCON_GLOBAL(orm).column_renaming) {
			PHALCON_INIT_NVAR(column_map);
			phalcon_call_method_p1(column_map, meta_data, "getcolumnmap", record);
		} else {
			PHALCON_INIT_NVAR(column_map);
		}
	
		PHALCON_INIT_VAR(primary_fields);
		phalcon_call_method_p1(primary_fields, meta_data, "getprimarykeyattributes", record);
	
		phalcon_is_iterable(primary_fields, &ah1, &hp1, 0, 0);
	
		while (zend_hash_get_current_data_ex(ah1, (void**) &hd, &hp1) == SUCCESS) {
	
			PHALCON_GET_HVALUE(primary_field);
	
			if (!phalcon_array_isset(bind_data_types, primary_field)) {
				PHALCON_INIT_NVAR(exception_message);
				PHALCON_CONCAT_SVS(exception_message, "Column '", primary_field, "\" isn't part of the table columns");
				PHALCON_THROW_EXCEPTION_ZVAL(phalcon_mvc_model_exception_ce, exception_message);
				return;
			}
	
			/** 
			 * Rename the column if there is a column map
			 */
			if (Z_TYPE_P(column_map) == IS_ARRAY) { 
				if (phalcon_array_isset(column_map, primary_field)) {
					PHALCON_OBS_NVAR(attribute_field);
					phalcon_array_fetch(&attribute_field, column_map, primary_field, PH_NOISY);
				} else {
					PHALCON_INIT_NVAR(exception_message);
					PHALCON_CONCAT_SVS(exception_message, "Column '", primary_field, "\" 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, primary_field);
			}
	
			/** 
			 * Create a condition based on the renamed primary key
			 */
			PHALCON_INIT_NVAR(value);
			phalcon_call_method_p1(value, record, "readattribute", primary_field);
	
			PHALCON_INIT_NVAR(condition);
			PHALCON_CONCAT_SVSV(condition, "[", attribute_field, "] <> ?", number);
			phalcon_array_append(&conditions, condition, PH_SEPARATE);
			phalcon_array_append(&bind_params, value, PH_SEPARATE);
	
			PHALCON_OBS_NVAR(bind_type);
			phalcon_array_fetch(&bind_type, bind_data_types, primary_field, PH_NOISY);
			phalcon_array_append(&bind_types, bind_type, PH_SEPARATE);
			phalcon_increment(number);
	
			zend_hash_move_forward_ex(ah1, &hp1);
		}
	
	}
	
	PHALCON_INIT_VAR(join_conditions);
	phalcon_fast_join_str(join_conditions, SL(" AND "), conditions TSRMLS_CC);
	
	/** 
	 * We don't trust the user, so we pass the parameters as bound parameters
	 */
	PHALCON_INIT_VAR(params);
	array_init(params);
	phalcon_array_update_string(&params, SL("di"), &dependency_injector, PH_COPY | PH_SEPARATE);
	phalcon_array_update_string(&params, SL("conditions"), &join_conditions, PH_COPY | PH_SEPARATE);
	phalcon_array_update_string(&params, SL("bind"), &bind_params, PH_COPY | PH_SEPARATE);
	phalcon_array_update_string(&params, SL("bindTypes"), &bind_types, PH_COPY | PH_SEPARATE);
	
	PHALCON_INIT_VAR(class_name);
	phalcon_get_class(class_name, record, 0 TSRMLS_CC);
	
	/** 
	 * Check using a standard count
	 */
	phalcon_call_zval_str_static_p1(number, class_name, "count", params);
	if (!PHALCON_IS_LONG(number, 0)) {
	
		/** 
		 * Check if the developer has defined a custom message
		 */
		PHALCON_INIT_NVAR(option);
		ZVAL_STRING(option, "message", 1);
	
		PHALCON_INIT_VAR(message);
		phalcon_call_method_p1(message, this_ptr, "getoption", option);
		if (!zend_is_true(message)) {
			if (Z_TYPE_P(field) == IS_ARRAY) { 
				PHALCON_INIT_VAR(join_fields);
				phalcon_fast_join_str(join_fields, SL(", "), field TSRMLS_CC);
	
				PHALCON_INIT_NVAR(message);
				PHALCON_CONCAT_SVS(message, "Value of fields: '", join_fields, "' are already present in another record");
			} else {
				PHALCON_INIT_NVAR(message);
				PHALCON_CONCAT_SVS(message, "Value of field: '", field, "' is already present in another record");
			}
		}
	
		/** 
		 * Append the message to the validator
		 */
		PHALCON_INIT_VAR(type);
		ZVAL_STRING(type, "Unique", 1);
		phalcon_call_method_p3_noret(this_ptr, "appendmessage", message, field, type);
		RETURN_MM_FALSE;
	}
	
	RETURN_MM_TRUE;
}
Example #8
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();
}
Example #9
0
/**
 * Rollbacks the active transaction in the connection
 *
 * @param boolean $nesting
 * @return boolean
 */
PHP_METHOD(Phalcon_Db_Adapter_Pdo, rollback){

	zval *nesting = NULL, *pdo, *transaction_level, *events_manager = NULL;
	zval *event_name = NULL, *ntw_savepoint = NULL, *savepoint_name = NULL;

	PHALCON_MM_GROW();

	phalcon_fetch_params(1, 0, 1, &nesting);

	if (!nesting) {
		nesting = PHALCON_GLOBAL(z_true);
	}

	pdo = phalcon_fetch_nproperty_this(this_ptr, SL("_pdo"), PH_NOISY TSRMLS_CC);
	if (Z_TYPE_P(pdo) != IS_OBJECT) {
		RETURN_MM_FALSE;
	}

	/** 
	 * Check the transaction nesting level
	 */
	transaction_level = phalcon_fetch_nproperty_this(this_ptr, SL("_transactionLevel"), PH_NOISY TSRMLS_CC);
	if (!zend_is_true(transaction_level)) {
		PHALCON_THROW_EXCEPTION_STR(phalcon_db_exception_ce, "There is no active transaction");
		return;
	}

	if (PHALCON_IS_LONG(transaction_level, 1)) {
		events_manager = phalcon_fetch_nproperty_this(this_ptr, SL("_eventsManager"), PH_NOISY TSRMLS_CC);

		/** 
		 * Notify the events manager about the rollbacked transaction
		 */
		if (Z_TYPE_P(events_manager) == IS_OBJECT) {
			PHALCON_INIT_VAR(event_name);
			ZVAL_STRING(event_name, "db:rollbackTransaction", 1);
			PHALCON_CALL_METHOD(NULL, events_manager, "fire", event_name, this_ptr);
		}

		/** 
		 * Reduce the transaction nesting level
		 */
		phalcon_property_decr(this_ptr, SL("_transactionLevel") TSRMLS_CC);
		PHALCON_RETURN_CALL_METHOD(pdo, "rollback");
		RETURN_MM();
	}

	if (zend_is_true(transaction_level)) {
		if (zend_is_true(nesting)) {
			PHALCON_CALL_METHOD(&ntw_savepoint, this_ptr, "isnestedtransactionswithsavepoints");
			if (zend_is_true(ntw_savepoint)) {

				events_manager = phalcon_fetch_nproperty_this(this_ptr, SL("_eventsManager"), PH_NOISY TSRMLS_CC);

				PHALCON_CALL_METHOD(&savepoint_name, this_ptr, "getnestedtransactionsavepointname");

				/**
				 * Notify the events manager about the rollbacked savepoint
				 */
				if (Z_TYPE_P(events_manager) == IS_OBJECT) {
					PHALCON_INIT_NVAR(event_name);
					ZVAL_STRING(event_name, "db:rollbackSavepoint", 1);
					PHALCON_CALL_METHOD(NULL, events_manager, "fire", event_name, this_ptr, savepoint_name);
				}

				/**
				 * Reduce the transaction nesting level
				 */
				phalcon_property_decr(this_ptr, SL("_transactionLevel") TSRMLS_CC);
				PHALCON_RETURN_CALL_METHOD(this_ptr, "rollbacksavepoint", savepoint_name);
				RETURN_MM();
			}
		}
	}

	/** 
	 * Reduce the transaction nesting level
	 */
	if (PHALCON_GT_LONG(transaction_level, 0)) {
		phalcon_property_decr(this_ptr, SL("_transactionLevel") TSRMLS_CC);
	}

	RETURN_MM_FALSE;
}
Example #10
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 Phalcon\Mvc\Model\Criteria
 */
PHP_METHOD(Phalcon_Mvc_Model_Criteria, fromInput){

	zval *dependency_injector, *model_name, *data;
	zval *conditions, *service, *meta_data = NULL, *model;
	zval *data_types = NULL, *bind, *value = NULL, *field = NULL, *type, *condition = NULL;
	zval *value_pattern = NULL, *join_conditions;
	zval *column_map = NULL;
	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;
	}
	
	object_init_ex(return_value, phalcon_mvc_model_criteria_ce);

	if (zend_hash_num_elements(Z_ARRVAL_P(data))) {
	
		PHALCON_INIT_VAR(service);
		PHALCON_ZVAL_MAYBE_INTERNED_STRING(service, phalcon_interned_modelsMetadata);
	
		PHALCON_CALL_METHOD(&meta_data, dependency_injector, "getshared", service);
		PHALCON_VERIFY_INTERFACE(meta_data, phalcon_mvc_model_metadatainterface_ce);
		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(NULL, model, "__construct");
		}

		PHALCON_VERIFY_INTERFACE_EX(model, phalcon_mvc_modelinterface_ce, phalcon_mvc_model_exception_ce, 1);

		if (PHALCON_GLOBAL(orm).column_renaming) {
			PHALCON_CALL_METHOD(&column_map, meta_data, "getreversecolumnmap", model);
			if (Z_TYPE_P(column_map) != IS_ARRAY) {
				PHALCON_INIT_NVAR(column_map);
			}
		}
		else {
			column_map = PHALCON_GLOBAL(z_null);
		}

		PHALCON_CALL_METHOD(&data_types, meta_data, "getdatatypes", model);
	
		PHALCON_INIT_VAR(bind);
		array_init(bind);
	
		PHALCON_INIT_VAR(conditions);
		array_init(conditions);

		/** 
		 * 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) {
			zval *real_field;
	
			PHALCON_GET_HKEY(field, ah0, hp0);
			PHALCON_GET_HVALUE(value);

			if (Z_TYPE_P(column_map) != IS_ARRAY || !phalcon_array_isset_fetch(&real_field, column_map, field)) {
				real_field = field;
			}
	
			if (phalcon_array_isset_fetch(&type, data_types, real_field)) {
				if (Z_TYPE_P(value) != IS_NULL && !PHALCON_IS_STRING(value, "")) {
					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);
					} 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);
					}

					phalcon_array_append(&conditions, condition, 0);
				}
			}
	
			zend_hash_move_forward_ex(ah0, &hp0);
		}
	
		if (zend_hash_num_elements(Z_ARRVAL_P(conditions))) {
			PHALCON_INIT_VAR(join_conditions);
			phalcon_fast_join_str(join_conditions, SL(" AND "), conditions TSRMLS_CC);
			PHALCON_CALL_METHOD(NULL, return_value, "where", join_conditions, bind);
		}
	}
	
	PHALCON_CALL_METHOD(NULL, return_value, "setmodelname", model_name);
	RETURN_MM();
}
Example #11
0
/**
 * Commits the active transaction in the connection
 *
 * @param boolean $nesting
 * @return boolean
 */
PHP_METHOD(Phalcon_Db_Adapter_Pdo, commit){

	zval *nesting = NULL, *pdo, *transaction_level, *events_manager = NULL;
	zval *event_name = NULL, *ntw_savepoint, *savepoint_name;

	PHALCON_MM_GROW();

	phalcon_fetch_params(1, 0, 1, &nesting);
	
	if (!nesting) {
		PHALCON_INIT_VAR(nesting);
		ZVAL_BOOL(nesting, 1);
	}
	
	PHALCON_OBS_VAR(pdo);
	phalcon_read_property_this(&pdo, this_ptr, SL("_pdo"), PH_NOISY_CC);
	if (Z_TYPE_P(pdo) != IS_OBJECT) {
		RETURN_MM_FALSE;
	}
	
	/** 
	 * Check the transaction nesting level
	 */
	PHALCON_OBS_VAR(transaction_level);
	phalcon_read_property_this(&transaction_level, this_ptr, SL("_transactionLevel"), PH_NOISY_CC);
	if (!zend_is_true(transaction_level)) {
		PHALCON_THROW_EXCEPTION_STR(phalcon_db_exception_ce, "There is no active transaction");
		return;
	}
	
	if (PHALCON_IS_LONG(transaction_level, 1)) {
	
		PHALCON_OBS_VAR(events_manager);
		phalcon_read_property_this(&events_manager, this_ptr, SL("_eventsManager"), PH_NOISY_CC);
	
		/** 
		 * Notify the events manager about the commited transaction
		 */
		if (Z_TYPE_P(events_manager) == IS_OBJECT) {
			PHALCON_INIT_VAR(event_name);
			ZVAL_STRING(event_name, "db:commitTransaction", 1);
			phalcon_call_method_p2_noret(events_manager, "fire", event_name, this_ptr);
		}
	
		/** 
		 * Reduce the transaction nesting level
		 */
		phalcon_property_decr(this_ptr, SL("_transactionLevel") TSRMLS_CC);
		phalcon_call_method(return_value, pdo, "commit");
		RETURN_MM();
	} else {
		if (zend_is_true(transaction_level)) {
			if (zend_is_true(nesting)) {
	
				/** 
				 * Check if the current database system supports nested transactions
				 */
				PHALCON_INIT_VAR(ntw_savepoint);
				phalcon_call_method(ntw_savepoint, this_ptr, "isnestedtransactionswithsavepoints");
				if (zend_is_true(ntw_savepoint)) {
	
					PHALCON_OBS_NVAR(events_manager);
					phalcon_read_property_this(&events_manager, this_ptr, SL("_eventsManager"), PH_NOISY_CC);
	
					PHALCON_INIT_VAR(savepoint_name);
					phalcon_call_method(savepoint_name, this_ptr, "getnestedtransactionsavepointname");
	
					/** 
					 * Notify the events manager about the commited savepoint
					 */
					if (Z_TYPE_P(events_manager) == IS_OBJECT) {
						PHALCON_INIT_NVAR(event_name);
						ZVAL_STRING(event_name, "db:releaseSavepoint", 1);
						phalcon_call_method_p3_noret(events_manager, "fire", event_name, this_ptr, savepoint_name);
					}
	
					/** 
					 * Reduce the transaction nesting level
					 */
					phalcon_property_decr(this_ptr, SL("_transactionLevel") TSRMLS_CC);
					phalcon_call_method_p1(return_value, this_ptr, "releasesavepoint", savepoint_name);
					RETURN_MM();
				}
			}
		}
	}
	
	/** 
	 * Reduce the transaction nesting level
	 */
	if (PHALCON_GT_LONG(transaction_level, 0)) {
		phalcon_property_decr(this_ptr, SL("_transactionLevel") TSRMLS_CC);
	}
	
	RETURN_MM_FALSE;
}
Example #12
0
/**
 * Starts a transaction in the connection
 *
 * @param boolean $nesting
 * @return boolean
 */
PHP_METHOD(Phalcon_Db_Adapter_Pdo, begin){

	zval *nesting = NULL, *pdo, *transaction_level, *events_manager = NULL;
	zval *event_name = NULL, *ntw_savepoint, *savepoint_name;

	PHALCON_MM_GROW();

	phalcon_fetch_params(1, 0, 1, &nesting);
	
	if (!nesting) {
		PHALCON_INIT_VAR(nesting);
		ZVAL_BOOL(nesting, 1);
	}
	
	PHALCON_OBS_VAR(pdo);
	phalcon_read_property_this(&pdo, this_ptr, SL("_pdo"), PH_NOISY_CC);
	if (Z_TYPE_P(pdo) != IS_OBJECT) {
		RETURN_MM_FALSE;
	}
	
	/** 
	 * Increase the transaction nesting level
	 */
	phalcon_property_incr(this_ptr, SL("_transactionLevel") TSRMLS_CC);
	
	/** 
	 * Check the transaction nesting level
	 */
	PHALCON_OBS_VAR(transaction_level);
	phalcon_read_property_this(&transaction_level, this_ptr, SL("_transactionLevel"), PH_NOISY_CC);
	if (PHALCON_IS_LONG(transaction_level, 1)) {
	
		PHALCON_OBS_VAR(events_manager);
		phalcon_read_property_this(&events_manager, this_ptr, SL("_eventsManager"), PH_NOISY_CC);
	
		/** 
		 * Notify the events manager about the started transaction
		 */
		if (Z_TYPE_P(events_manager) == IS_OBJECT) {
			PHALCON_INIT_VAR(event_name);
			ZVAL_STRING(event_name, "db:beginTransaction", 1);
			phalcon_call_method_p2_noret(events_manager, "fire", event_name, this_ptr);
		}
	
		phalcon_call_method(return_value, pdo, "begintransaction");
		RETURN_MM();
	} else {
		if (zend_is_true(transaction_level)) {
			if (zend_is_true(nesting)) {
	
				PHALCON_INIT_VAR(ntw_savepoint);
				phalcon_call_method(ntw_savepoint, this_ptr, "isnestedtransactionswithsavepoints");
				if (zend_is_true(ntw_savepoint)) {
	
					PHALCON_OBS_NVAR(events_manager);
					phalcon_read_property_this(&events_manager, this_ptr, SL("_eventsManager"), PH_NOISY_CC);
	
					PHALCON_INIT_VAR(savepoint_name);
					phalcon_call_method(savepoint_name, this_ptr, "getnestedtransactionsavepointname");
	
					/** 
					 * Notify the events manager about the created savepoint
					 */
					if (Z_TYPE_P(events_manager) == IS_OBJECT) {
						PHALCON_INIT_NVAR(event_name);
						ZVAL_STRING(event_name, "db:createSavepoint", 1);
						phalcon_call_method_p3_noret(events_manager, "fire", event_name, this_ptr, savepoint_name);
					}
	
					phalcon_call_method_p1(return_value, this_ptr, "createsavepoint", savepoint_name);
					RETURN_MM();
				}
			}
		}
	}
	
	RETURN_MM_FALSE;
}
Example #13
0
/**
 * Prepare an HTML string of information about a single variable.
 */
PHP_METHOD(Phalcon_Debug_Dump, output){

	zval *variable, *name = NULL, *tab = NULL, *space, *tmp = NULL, *new_tab = NULL;
	zval *output = NULL, *str = NULL, *type = NULL, *style = NULL, *count = NULL, *key = NULL, *value = NULL, *replace_pairs = NULL;
	zval *class_name = NULL, *objects, *detailed = NULL, *properties = NULL, *methods = NULL, *method = NULL;
	HashTable *ah0;
	HashPosition hp0;
	zval **hd;

	PHALCON_MM_GROW();

	phalcon_fetch_params(1, 1, 2, &variable, &name, &tab);

	if (!name) {
		name = PHALCON_GLOBAL(z_null);
	} else if (!PHALCON_IS_EMPTY(name)) {
		PHALCON_CONCAT_SVS(return_value, "var ", name, " ");
	}

	if (!tab) {
		tab = PHALCON_GLOBAL(z_one);
	}

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

	if (Z_TYPE_P(variable) == IS_ARRAY) {
		PHALCON_INIT_NVAR(str);
		ZVAL_STRING(str, "<b style =':style'>Array</b> (<span style =':style'>:count</span>) (\n", 1);

		PHALCON_INIT_NVAR(type);
		ZVAL_STRING(type, "arr", 1);

		PHALCON_CALL_SELF(&style, "getstyle", type);

		PHALCON_INIT_NVAR(count);
		ZVAL_LONG(count, phalcon_fast_count_int(variable TSRMLS_CC));

		PHALCON_INIT_NVAR(replace_pairs);
		array_init(replace_pairs);

		phalcon_array_update_string(&replace_pairs, SL(":style"), style, PH_COPY);
		phalcon_array_update_string(&replace_pairs, SL(":count"), count, PH_COPY);

		PHALCON_INIT_NVAR(output);
		phalcon_strtr_array(output, str, replace_pairs TSRMLS_CC);

		phalcon_concat_self(&return_value, output TSRMLS_CC);

		phalcon_is_iterable(variable, &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_CALL_FUNCTION(&tmp, "str_repeat", space, tab);

			phalcon_concat_self(&return_value, tmp TSRMLS_CC);

			PHALCON_INIT_NVAR(str);
			ZVAL_STRING(str, "[<span style=':style'>:key</span>] => ", 1);

			PHALCON_INIT_NVAR(type);
			ZVAL_STRING(type, "arr", 1);

			PHALCON_CALL_SELF(&style, "getstyle", type);

			PHALCON_INIT_NVAR(replace_pairs);
			array_init(replace_pairs);

			phalcon_array_update_string(&replace_pairs, SL(":style"), style, PH_COPY);
			phalcon_array_update_string(&replace_pairs, SL(":key"), key, PH_COPY);

			PHALCON_INIT_NVAR(output);
			phalcon_strtr_array(output, str, replace_pairs TSRMLS_CC);

			phalcon_concat_self(&return_value, output TSRMLS_CC);

			if (PHALCON_IS_LONG(tab, 1) && !PHALCON_IS_EMPTY(name) && !phalcon_is_numeric(key) && PHALCON_IS_IDENTICAL(name, key)) {
				zend_hash_move_forward_ex(ah0, &hp0);
				continue;
			} else {
				PHALCON_INIT_NVAR(new_tab);
				ZVAL_LONG(new_tab, Z_LVAL_P(tab) + 1);

				PHALCON_CALL_SELF(&tmp, "output", value, PHALCON_GLOBAL(z_null), new_tab);
				PHALCON_SCONCAT_VS(return_value, tmp, "\n");
			}

			zend_hash_move_forward_ex(ah0, &hp0);
		}

		PHALCON_INIT_NVAR(new_tab);
		ZVAL_LONG(new_tab, Z_LVAL_P(tab) - 1);

		PHALCON_CALL_FUNCTION(&tmp, "str_repeat", space, tab);

		PHALCON_SCONCAT(return_value, tmp);
	} else if (Z_TYPE_P(variable) == IS_OBJECT) {

		PHALCON_INIT_NVAR(str);
		ZVAL_STRING(str, "<b style=':style'>Object</b> :class", 1);

		PHALCON_INIT_NVAR(type);
		ZVAL_STRING(type, "obj", 1);

		PHALCON_CALL_SELF(&style, "getstyle", type);

		PHALCON_INIT_NVAR(class_name);
		phalcon_get_class(class_name, variable, 0 TSRMLS_CC);

		PHALCON_INIT_NVAR(replace_pairs);
		array_init(replace_pairs);

		phalcon_array_update_string(&replace_pairs, SL(":style"), style, PH_COPY);
		phalcon_array_update_string(&replace_pairs, SL(":class"), class_name, PH_COPY);

		PHALCON_INIT_NVAR(output);
		phalcon_strtr_array(output, str, replace_pairs TSRMLS_CC);

		PHALCON_SCONCAT(return_value, output);

		PHALCON_INIT_NVAR(class_name);
		phalcon_get_parent_class(class_name, variable, 0 TSRMLS_CC);

		if (zend_is_true(class_name)) {
			PHALCON_INIT_NVAR(str);
			ZVAL_STRING(str, " <b style=':style'>extends</b> :parent", 1);

			PHALCON_INIT_NVAR(type);
			ZVAL_STRING(type, "obj", 1);

			PHALCON_CALL_SELF(&style, "getstyle", type);

			PHALCON_INIT_NVAR(replace_pairs);
			array_init(replace_pairs);

			phalcon_array_update_string(&replace_pairs, SL(":style"), style, PH_COPY);
			phalcon_array_update_string(&replace_pairs, SL(":parent"), class_name, PH_COPY);

			PHALCON_INIT_NVAR(output);
			phalcon_strtr_array(output, str, replace_pairs TSRMLS_CC);

			PHALCON_SCONCAT(return_value, output);
		}

		PHALCON_SCONCAT_STR(return_value, " (\n");

		objects  = phalcon_fetch_nproperty_this(this_ptr, SL("_objects"), PH_NOISY TSRMLS_CC);

		if (phalcon_fast_in_array(variable, objects TSRMLS_CC)) {
			
			PHALCON_CALL_FUNCTION(&tmp, "str_repeat", space, tab);
			PHALCON_SCONCAT_VS(return_value, tmp, "[already listed]\n");

			PHALCON_INIT_NVAR(new_tab);
			ZVAL_LONG(new_tab, Z_LVAL_P(tab) - 1);

			PHALCON_CALL_FUNCTION(&tmp, "str_repeat", space, tab);

			PHALCON_SCONCAT_VS(return_value, tmp, ")");

			RETURN_MM();
		}

		phalcon_update_property_array_append(this_ptr, SL("_objects"), variable TSRMLS_CC);

		detailed  = phalcon_fetch_nproperty_this(this_ptr, SL("_detailed"), PH_NOISY TSRMLS_CC);

		PHALCON_INIT_NVAR(properties);
		phalcon_get_object_vars(properties, variable, !zend_is_true(detailed) TSRMLS_CC);

		phalcon_is_iterable(properties, &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_CALL_FUNCTION(&tmp, "str_repeat", space, tab);

			PHALCON_SCONCAT(return_value, tmp);

			PHALCON_INIT_NVAR(str);
			ZVAL_STRING(str, "-><span style=':style'>:key</span> (<span style=':style'>:type</span>) = ", 1);

			PHALCON_INIT_NVAR(type);
			ZVAL_STRING(type, "obj", 1);

			PHALCON_CALL_SELF(&style, "getstyle", type);

			PHALCON_INIT_NVAR(replace_pairs);
			array_init(replace_pairs);

			phalcon_array_update_string(&replace_pairs, SL(":style"), style, PH_COPY);
			phalcon_array_update_string(&replace_pairs, SL(":key"), key, PH_COPY);

			if (PHALCON_PROPERTY_IS_PUBLIC_ZVAL(variable, key)) {
				phalcon_array_update_string_string(&replace_pairs, SL(":type"), SL("public"), PH_COPY);
			} else if (PHALCON_PROPERTY_IS_PRIVATE_ZVAL(variable, key)) {
				phalcon_array_update_string_string(&replace_pairs, SL(":type"), SL("private"), PH_COPY);
			} else if (PHALCON_PROPERTY_IS_PROTECTED_ZVAL(variable, key)) {
				phalcon_array_update_string_string(&replace_pairs, SL(":type"), SL("protected"), PH_COPY);
			}

			PHALCON_INIT_NVAR(output);
			phalcon_strtr_array(output, str, replace_pairs TSRMLS_CC);

			PHALCON_SCONCAT(return_value, output);

			PHALCON_INIT_NVAR(new_tab);
			ZVAL_LONG(new_tab, Z_LVAL_P(tab) + 1);

			PHALCON_CALL_SELF(&tmp, "output", value, PHALCON_GLOBAL(z_null), new_tab);
			PHALCON_SCONCAT_VS(return_value, tmp, ")\n");

			zend_hash_move_forward_ex(ah0, &hp0);
		}

		PHALCON_INIT_NVAR(methods);

		phalcon_get_class_methods(methods, variable, !zend_is_true(detailed) TSRMLS_CC);

		PHALCON_CALL_FUNCTION(&tmp, "str_repeat", space, tab);

		PHALCON_SCONCAT(return_value, tmp);

		PHALCON_INIT_NVAR(str);
		ZVAL_STRING(str, ":class <b style=':style'>methods</b>: (<span style=':style'>:count</span>) (\n", 1);

		PHALCON_INIT_NVAR(type);
		ZVAL_STRING(type, "obj", 1);

		PHALCON_CALL_SELF(&style, "getstyle", type);

		PHALCON_INIT_NVAR(class_name);
		phalcon_get_class(class_name, variable, 0 TSRMLS_CC);

		PHALCON_INIT_NVAR(count);
		ZVAL_LONG(count, phalcon_fast_count_int(methods TSRMLS_CC));

		PHALCON_INIT_NVAR(replace_pairs);
		array_init(replace_pairs);

		phalcon_array_update_string(&replace_pairs, SL(":style"), style, PH_COPY);
		phalcon_array_update_string(&replace_pairs, SL(":class"), class_name, PH_COPY);
		phalcon_array_update_string(&replace_pairs, SL(":count"), count, PH_COPY);

		PHALCON_INIT_NVAR(output);
		phalcon_strtr_array(output, str, replace_pairs TSRMLS_CC);

		PHALCON_SCONCAT(return_value, output);

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

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

			PHALCON_GET_HVALUE(method);

			PHALCON_INIT_NVAR(new_tab);
			ZVAL_LONG(new_tab, Z_LVAL_P(tab) + 1);

			PHALCON_CALL_FUNCTION(&tmp, "str_repeat", space, new_tab);

			PHALCON_SCONCAT(return_value, tmp);

			PHALCON_INIT_NVAR(str);
			ZVAL_STRING(str, "-><span style=':style'>:method</span>();\n", 1);

			PHALCON_INIT_NVAR(type);
			ZVAL_STRING(type, "obj", 1);

			PHALCON_CALL_SELF(&style, "getstyle", type);

			PHALCON_INIT_NVAR(replace_pairs);
			array_init(replace_pairs);

			phalcon_array_update_string(&replace_pairs, SL(":style"), style, PH_COPY);
			phalcon_array_update_string(&replace_pairs, SL(":method"), method, PH_COPY);

			PHALCON_INIT_NVAR(output);
			phalcon_strtr_array(output, str, replace_pairs TSRMLS_CC);

			PHALCON_SCONCAT(return_value, output);

			PHALCON_CALL_FUNCTION(&tmp, "str_repeat", space, tab);

			PHALCON_SCONCAT_VS(return_value, tmp, "\n");

			zend_hash_move_forward_ex(ah0, &hp0);
		}

		PHALCON_INIT_NVAR(new_tab);
		ZVAL_LONG(new_tab, Z_LVAL_P(tab) - 1);

		PHALCON_CALL_FUNCTION(&tmp, "str_repeat", space, tab);

		PHALCON_SCONCAT_VS(return_value, tmp, ")");
	} else if (Z_TYPE_P(variable) == IS_LONG) {
		PHALCON_INIT_NVAR(str);
		ZVAL_STRING(str, "<b style=':style'>Integer</b> (<span style=':style'>:var</span>)", 1);

		PHALCON_INIT_NVAR(type);
		ZVAL_STRING(type, "int", 1);

		PHALCON_CALL_SELF(&style, "getstyle", type);

		PHALCON_INIT_NVAR(replace_pairs);
		array_init(replace_pairs);

		phalcon_array_update_string(&replace_pairs, SL(":style"), style, PH_COPY);
		phalcon_array_update_string(&replace_pairs, SL(":var"), variable, PH_COPY);

		PHALCON_INIT_NVAR(output);
		phalcon_strtr_array(output, str, replace_pairs TSRMLS_CC);

		PHALCON_SCONCAT(return_value, output);
	} else if (Z_TYPE_P(variable) == IS_DOUBLE) {
		PHALCON_INIT_NVAR(str);
		ZVAL_STRING(str, "<b style=':style'>Float</b> (<span style=':style'>:var</span>)", 1);

		PHALCON_INIT_NVAR(type);
		ZVAL_STRING(type, "float", 1);

		PHALCON_CALL_SELF(&style, "getstyle", type);

		PHALCON_INIT_NVAR(replace_pairs);
		array_init(replace_pairs);

		phalcon_array_update_string(&replace_pairs, SL(":style"), style, PH_COPY);
		phalcon_array_update_string(&replace_pairs, SL(":var"), variable, PH_COPY);

		PHALCON_INIT_NVAR(output);
		phalcon_strtr_array(output, str, replace_pairs TSRMLS_CC);

		PHALCON_SCONCAT(return_value, output);
	} else if (phalcon_is_numeric_ex(variable)) {
		PHALCON_INIT_NVAR(str);
		ZVAL_STRING(str, "<b style=':style'>Numeric string</b> (<span style=':style'>:length</span>) \"<span style=':style'>:var</span>\"", 1);

		PHALCON_INIT_NVAR(type);
		ZVAL_STRING(type, "num", 1);

		PHALCON_CALL_SELF(&style, "getstyle", type);

		PHALCON_INIT_NVAR(replace_pairs);
		array_init(replace_pairs);

		phalcon_array_update_string(&replace_pairs, SL(":style"), style, PH_COPY);
		phalcon_array_update_string_long(&replace_pairs, SL(":length"), Z_STRLEN_P(variable), PH_COPY);
		phalcon_array_update_string(&replace_pairs, SL(":var"), variable, PH_COPY);

		PHALCON_INIT_NVAR(output);
		phalcon_strtr_array(output, str, replace_pairs TSRMLS_CC);

		PHALCON_SCONCAT(return_value, output);
	} else if (Z_TYPE_P(variable) == IS_STRING) {
		PHALCON_INIT_NVAR(str);
		ZVAL_STRING(str, "<b style=':style'>String</b> (<span style=':style'>:length</span>) \"<span style=':style'>:var</span>\"", 1);

		PHALCON_INIT_NVAR(type);
		ZVAL_STRING(type, "str", 1);

		PHALCON_CALL_SELF(&style, "getstyle", type);

		PHALCON_INIT_NVAR(replace_pairs);
		array_init(replace_pairs);

		phalcon_array_update_string(&replace_pairs, SL(":style"), style, PH_COPY);
		phalcon_array_update_string_long(&replace_pairs, SL(":length"), Z_STRLEN_P(variable), PH_COPY);
		phalcon_array_update_string(&replace_pairs, SL(":var"), variable, PH_COPY);

		PHALCON_INIT_NVAR(output);
		phalcon_strtr_array(output, str, replace_pairs TSRMLS_CC);

		PHALCON_SCONCAT(return_value, output);
	} else if (Z_TYPE_P(variable) == IS_BOOL) {
		PHALCON_INIT_NVAR(str);
		ZVAL_STRING(str, "<b style=':style'>Boolean</b> (<span style=':style'>:var</span>)", 1);

		PHALCON_INIT_NVAR(type);
		ZVAL_STRING(type, "bool", 1);

		PHALCON_CALL_SELF(&style, "getstyle", type);

		PHALCON_INIT_NVAR(replace_pairs);
		array_init(replace_pairs);

		phalcon_array_update_string(&replace_pairs, SL(":style"), style, PH_COPY);
		if (zend_is_true(variable)) {
			phalcon_array_update_string_string(&replace_pairs, SL(":var"), SL("TRUE") , PH_COPY);
		} else {
			phalcon_array_update_string_string(&replace_pairs, SL(":var"), SL("FALSE") , PH_COPY);
		}

		PHALCON_INIT_NVAR(output);
		phalcon_strtr_array(output, str, replace_pairs TSRMLS_CC);

		PHALCON_SCONCAT(return_value, output);
	} else if (Z_TYPE_P(variable) == IS_NULL) {
		PHALCON_INIT_NVAR(str);
		ZVAL_STRING(str, "<b style=':style'>NULL</b>", 1);

		PHALCON_INIT_NVAR(type);
		ZVAL_STRING(type, "null", 1);

		PHALCON_CALL_SELF(&style, "getstyle", type);

		PHALCON_INIT_NVAR(replace_pairs);
		array_init(replace_pairs);

		phalcon_array_update_string(&replace_pairs, SL(":style"), style, PH_COPY);

		PHALCON_INIT_NVAR(output);
		phalcon_strtr_array(output, str, replace_pairs TSRMLS_CC);

		PHALCON_SCONCAT(return_value, output);
	} else {
		PHALCON_INIT_NVAR(str);
		ZVAL_STRING(str, "(<span style=':style'>:var</span>)", 1);

		PHALCON_INIT_NVAR(type);
		ZVAL_STRING(type, "other", 1);

		PHALCON_CALL_SELF(&style, "getstyle", type);

		PHALCON_INIT_NVAR(replace_pairs);
		array_init(replace_pairs);

		phalcon_array_update_string(&replace_pairs, SL(":style"), style, PH_COPY);
		phalcon_array_update_string(&replace_pairs, SL(":var"), variable, PH_COPY);

		PHALCON_INIT_NVAR(output);
		phalcon_strtr_array(output, str, replace_pairs TSRMLS_CC);

		PHALCON_SCONCAT(return_value, output);
	}

	RETURN_MM();
}
Example #14
0
/**
 * Phalcon\Db\Column constructor
 *
 * @param string $columnName
 * @param array $definition
 */
PHP_METHOD(Phalcon_Db_Column, __construct){

	zval *column_name, *definition, *type, *not_null;
	zval *primary, *size, *scale, *dunsigned, *is_numeric;
	zval *auto_increment, *first, *after, *bind_type, *default_value;

	phalcon_fetch_params(0, 2, 0, &column_name, &definition);
	
	phalcon_update_property_this(this_ptr, SL("_columnName"), column_name TSRMLS_CC);
	
	/** 
	 * Get the column type, one of the TYPE_* constants
	 */
	if (phalcon_array_isset_string_fetch(&type, definition, SS("type"))) {
		phalcon_update_property_this(this_ptr, SL("_type"), type TSRMLS_CC);
	} else {
		PHALCON_THROW_EXCEPTION_STRW(phalcon_db_exception_ce, "Column type is required");
		return;
	}
	
	/** 
	 * Check if the field is nullable
	 */
	if (phalcon_array_isset_string_fetch(&not_null, definition, SS("notNull"))) {
		phalcon_update_property_this(this_ptr, SL("_notNull"), not_null TSRMLS_CC);
	}
	
	/** 
	 * Check if the field is primary key
	 */
	if (phalcon_array_isset_string_fetch(&primary, definition, SS("primary"))) {
		phalcon_update_property_this(this_ptr, SL("_primary"), primary TSRMLS_CC);
	}
	
	if (phalcon_array_isset_string_fetch(&size, definition, SS("size"))) {
		phalcon_update_property_this(this_ptr, SL("_size"), size TSRMLS_CC);
	}
	
	/** 
	 * Check if the column has a decimal scale
	 */
	if (phalcon_array_isset_string_fetch(&scale, definition, SS("scale"))) {
		int i_type     = phalcon_get_intval(type);
		int is_numeric = (i_type == 3 || i_type == 7 || i_type == 9);
	
		if (is_numeric) {
			phalcon_update_property_this(this_ptr, SL("_scale"), scale TSRMLS_CC);
		}
	}
	
	/** 
	 * Check if the field is unsigned (only MySQL)
	 */
	if (phalcon_array_isset_string_fetch(&dunsigned, definition, SS("unsigned"))) {
		phalcon_update_property_this(this_ptr, SL("_unsigned"), dunsigned TSRMLS_CC);
	}
	
	/** 
	 * Check if the field is numeric
	 */
	if (phalcon_array_isset_string_fetch(&is_numeric, definition, SS("isNumeric"))) {
		phalcon_update_property_this(this_ptr, SL("_isNumeric"), is_numeric TSRMLS_CC);
	}
	
	/** 
	 * Check if the field is auto-increment/serial
	 */
	if (phalcon_array_isset_string_fetch(&auto_increment, definition, SS("autoIncrement"))) {
		if (PHALCON_IS_LONG(type, 0)) {
			phalcon_update_property_this(this_ptr, SL("_autoIncrement"), auto_increment TSRMLS_CC);
		} else {
			PHALCON_THROW_EXCEPTION_STRW(phalcon_db_exception_ce, "Column type cannot be auto-increment");
			return;
		}
	}
	
	/** 
	 * Check if the field is placed at the first position of the table
	 */
	if (phalcon_array_isset_string_fetch(&first, definition, SS("first"))) {
		phalcon_update_property_this(this_ptr, SL("_first"), first TSRMLS_CC);
	}
	
	/** 
	 * Name of the column which is placed before the current field
	 */
	if (phalcon_array_isset_string_fetch(&after, definition, SS("after"))) {
		phalcon_update_property_this(this_ptr, SL("_after"), after TSRMLS_CC);
	}
	
	/** 
	 * The bind type to cast the field when passing it to PDO
	 */
	if (phalcon_array_isset_string_fetch(&bind_type, definition, SS("bindType"))) {
		phalcon_update_property_this(this_ptr, SL("_bindType"), bind_type TSRMLS_CC);
	}
	
	/** 
	 * Default values
	 */
	if (phalcon_array_isset_string_fetch(&default_value, definition, SS("default"))) {
		phalcon_update_property_this(this_ptr, SL("_default"), default_value TSRMLS_CC);
	}
}
Example #15
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();
}
Example #16
0
/**
 * Starts a transaction in the connection
 *
 * @param boolean $nesting
 * @return boolean
 */
PHP_METHOD(Phalcon_Db_Adapter_Pdo, begin){

	zval *nesting = NULL, *pdo, *transaction_level, *events_manager = NULL;
	zval *event_name = NULL, *ntw_savepoint = NULL, *savepoint_name = NULL;

	PHALCON_MM_GROW();

	phalcon_fetch_params(1, 0, 1, &nesting);

	if (!nesting) {
		nesting = PHALCON_GLOBAL(z_true);
	}

	pdo = phalcon_fetch_nproperty_this(this_ptr, SL("_pdo"), PH_NOISY TSRMLS_CC);
	if (Z_TYPE_P(pdo) != IS_OBJECT) {
		RETURN_MM_FALSE;
	}

	/** 
	 * Increase the transaction nesting level
	 */
	phalcon_property_incr(this_ptr, SL("_transactionLevel") TSRMLS_CC);

	/** 
	 * Check the transaction nesting level
	 */
	transaction_level = phalcon_fetch_nproperty_this(this_ptr, SL("_transactionLevel"), PH_NOISY TSRMLS_CC);
	if (PHALCON_IS_LONG(transaction_level, 1)) {

		events_manager = phalcon_fetch_nproperty_this(this_ptr, SL("_eventsManager"), PH_NOISY TSRMLS_CC);

		/** 
		 * Notify the events manager about the started transaction
		 */
		if (Z_TYPE_P(events_manager) == IS_OBJECT) {
			PHALCON_INIT_VAR(event_name);
			ZVAL_STRING(event_name, "db:beginTransaction", 1);
			PHALCON_CALL_METHOD(NULL, events_manager, "fire", event_name, this_ptr);
		}

		PHALCON_RETURN_CALL_METHOD(pdo, "begintransaction");
		RETURN_MM();
	}

	if (zend_is_true(transaction_level)) {
		if (zend_is_true(nesting)) {
			PHALCON_CALL_METHOD(&ntw_savepoint, this_ptr, "isnestedtransactionswithsavepoints");
			if (zend_is_true(ntw_savepoint)) {
				events_manager = phalcon_fetch_nproperty_this(this_ptr, SL("_eventsManager"), PH_NOISY TSRMLS_CC);

				PHALCON_CALL_METHOD(&savepoint_name, this_ptr, "getnestedtransactionsavepointname");

				/**
				 * Notify the events manager about the created savepoint
				 */
				if (Z_TYPE_P(events_manager) == IS_OBJECT) {
					PHALCON_INIT_NVAR(event_name);
					ZVAL_STRING(event_name, "db:createSavepoint", 1);
					PHALCON_CALL_METHOD(NULL, events_manager, "fire", event_name, this_ptr, savepoint_name);
				}

				PHALCON_RETURN_CALL_METHOD(this_ptr, "createsavepoint", savepoint_name);
				RETURN_MM();
			}
		}
	}

	RETURN_MM_FALSE;
}
Example #17
0
/**
 * Returns a slice of the resultset to show in the pagination
 *
 * @return stdClass
 */
PHP_METHOD(Phalcon_Paginator_Adapter_Model, getPaginate){

	zval *one, *zero, *show, *config, *items, *page_number = NULL;
	zval *smaller, *n, *page, *last_show_page, *start;
	zval *last_page, *possible_pages = NULL, *total_pages;
	zval *compare = NULL, *page_items, *i, *valid = NULL, *current = NULL, *maximum_pages;
	zval *next = NULL, *additional_page, *before = NULL, *remainder;
	zval *pages_total = NULL;
	zval *r0 = NULL;

	PHALCON_MM_GROW();

	PHALCON_INIT_VAR(one);
	ZVAL_LONG(one, 1);
	
	PHALCON_INIT_VAR(zero);
	ZVAL_LONG(zero, 0);
	
	PHALCON_OBS_VAR(show);
	phalcon_read_property(&show, this_ptr, SL("_limitRows"), PH_NOISY_CC);
	
	PHALCON_OBS_VAR(config);
	phalcon_read_property(&config, this_ptr, SL("_config"), PH_NOISY_CC);
	
	PHALCON_OBS_VAR(items);
	phalcon_array_fetch_string(&items, config, SL("data"), PH_NOISY_CC);
	
	PHALCON_OBS_VAR(page_number);
	phalcon_read_property(&page_number, this_ptr, SL("_page"), PH_NOISY_CC);
	if (Z_TYPE_P(page_number) == IS_NULL) {
		PHALCON_CPY_WRT(page_number, one);
	}
	
	PHALCON_INIT_VAR(smaller);
	is_smaller_function(smaller, show, zero TSRMLS_CC);
	if (PHALCON_IS_TRUE(smaller)) {
		PHALCON_THROW_EXCEPTION_STR(phalcon_paginator_exception_ce, "The start page number is zero or less");
		return;
	}
	
	PHALCON_INIT_VAR(n);
	phalcon_fast_count(n, items TSRMLS_CC);
	
	PHALCON_INIT_VAR(page);
	object_init(page);
	
	PHALCON_INIT_VAR(last_show_page);
	sub_function(last_show_page, page_number, one TSRMLS_CC);
	
	PHALCON_INIT_VAR(start);
	mul_function(start, show, last_show_page TSRMLS_CC);
	
	PHALCON_INIT_VAR(last_page);
	sub_function(last_page, n, one TSRMLS_CC);
	
	PHALCON_INIT_VAR(possible_pages);
	div_function(possible_pages, last_page, show TSRMLS_CC);
	
	PHALCON_INIT_VAR(total_pages);
	PHALCON_CALL_FUNC_PARAMS_1(total_pages, "ceil", possible_pages);
	if (Z_TYPE_P(items) != IS_OBJECT) {
		PHALCON_THROW_EXCEPTION_STR(phalcon_paginator_exception_ce, "Invalid data for paginator");
		return;
	}
	
	if (Z_TYPE_P(page_number) == IS_NULL) {
		PHALCON_CPY_WRT(page_number, zero);
	}
	
	PHALCON_INIT_VAR(compare);
	is_smaller_function(compare, start, zero TSRMLS_CC);
	if (PHALCON_IS_TRUE(compare)) {
		PHALCON_THROW_EXCEPTION_STR(phalcon_paginator_exception_ce, "The start page number is zero or less");
		return;
	}
	
	PHALCON_INIT_VAR(page_items);
	array_init(page_items);
	
	is_smaller_function(compare, zero, n TSRMLS_CC);
	if (PHALCON_IS_TRUE(compare)) {
	
		/** 
		 * Seek to the desired position
		 */
		is_smaller_or_equal_function(compare, start, n TSRMLS_CC);
		if (PHALCON_IS_TRUE(compare)) {
			PHALCON_CALL_METHOD_PARAMS_1_NORETURN(items, "seek", start);
		} else {
			PHALCON_CALL_METHOD_PARAMS_1_NORETURN(items, "seek", one);
			PHALCON_CPY_WRT(page_number, one);
		}
	
		/** 
		 * The record must be iterable
		 */
		PHALCON_INIT_VAR(i);
		ZVAL_LONG(i, 1);
	
		while (1) {
	
			PHALCON_INIT_NVAR(r0);
			PHALCON_CALL_METHOD(r0, items, "valid");
			PHALCON_CPY_WRT(valid, r0);
			if (PHALCON_IS_NOT_FALSE(valid)) {
			} else {
				break;
			}
	
			PHALCON_INIT_NVAR(current);
			PHALCON_CALL_METHOD(current, items, "current");
			phalcon_array_append(&page_items, current, PH_SEPARATE TSRMLS_CC);
	
			PHALCON_INIT_NVAR(compare);
			is_smaller_or_equal_function(compare, show, i TSRMLS_CC);
			if (PHALCON_IS_TRUE(compare)) {
				break;
			}
	
			PHALCON_SEPARATE(i);
			increment_function(i);
		}
	}
	
	phalcon_update_property_zval(page, SL("items"), page_items TSRMLS_CC);
	phalcon_update_property_zval(page, SL("first"), one TSRMLS_CC);
	
	PHALCON_INIT_VAR(maximum_pages);
	phalcon_add_function(maximum_pages, start, show TSRMLS_CC);
	
	is_smaller_function(compare, maximum_pages, n TSRMLS_CC);
	if (PHALCON_IS_TRUE(compare)) {
		PHALCON_INIT_VAR(next);
		phalcon_add_function(next, page_number, one TSRMLS_CC);
	} else {
		is_equal_function(compare, maximum_pages, n TSRMLS_CC);
		if (PHALCON_IS_TRUE(compare)) {
			PHALCON_CPY_WRT(next, n);
		} else {
			div_function(possible_pages, n, show TSRMLS_CC);
	
			PHALCON_INIT_VAR(additional_page);
			phalcon_add_function(additional_page, possible_pages, one TSRMLS_CC);
	
			PHALCON_INIT_NVAR(next);
			PHALCON_CALL_FUNC_PARAMS_1(next, "intval", additional_page);
		}
	}
	
	is_smaller_function(compare, total_pages, next TSRMLS_CC);
	if (PHALCON_IS_TRUE(compare)) {
		PHALCON_CPY_WRT(next, total_pages);
	}
	
	phalcon_update_property_zval(page, SL("next"), next TSRMLS_CC);
	
	is_smaller_function(compare, one, page_number TSRMLS_CC);
	if (PHALCON_IS_TRUE(compare)) {
		PHALCON_INIT_VAR(before);
		sub_function(before, page_number, one TSRMLS_CC);
	} else {
		PHALCON_CPY_WRT(before, one);
	}
	
	phalcon_update_property_zval(page, SL("before"), before TSRMLS_CC);
	phalcon_update_property_zval(page, SL("current"), page_number TSRMLS_CC);
	
	PHALCON_INIT_VAR(remainder);
	mod_function(remainder, n, show TSRMLS_CC);
	
	div_function(possible_pages, n, show TSRMLS_CC);
	if (!PHALCON_IS_LONG(remainder, 0)) {
		PHALCON_INIT_NVAR(next);
		phalcon_add_function(next, possible_pages, one TSRMLS_CC);
	
		PHALCON_INIT_VAR(pages_total);
		PHALCON_CALL_FUNC_PARAMS_1(pages_total, "intval", next);
	} else {
		PHALCON_CPY_WRT(pages_total, possible_pages);
	}
	
	phalcon_update_property_zval(page, SL("last"), pages_total TSRMLS_CC);
	phalcon_update_property_zval(page, SL("total_pages"), pages_total TSRMLS_CC);
	
	RETURN_CTOR(page);
}