Пример #1
0
/**
 * Serializing a resultset will dump all related rows into a big array
 *
 * @return string
 */
PHP_METHOD(Phalcon_Mvc_Model_Resultset_Simple, serialize){

	zval *type = NULL, *result = NULL, *records = NULL, *row_count = NULL, *model = NULL;
	zval *cache = NULL, *data = NULL, *serialized = NULL;

	PHALCON_MM_GROW();
	PHALCON_INIT_VAR(type);
	phalcon_read_property(&type, this_ptr, SL("_type"), PH_NOISY_CC);
	if (zend_is_true(type)) {
		PHALCON_INIT_VAR(result);
		phalcon_read_property(&result, this_ptr, SL("_result"), PH_NOISY_CC);
		if (PHALCON_IS_NOT_FALSE(result)) {
			PHALCON_CALL_METHOD_NORETURN(result, "execute", PH_NO_CHECK);
			
			PHALCON_INIT_VAR(records);
			PHALCON_CALL_METHOD(records, result, "fetchall", PH_NO_CHECK);
		} else {
			PHALCON_INIT_VAR(records);
			array_init(records);
		}
	} else {
		PHALCON_INIT_VAR(records);
		phalcon_read_property(&records, this_ptr, SL("_rows"), PH_NOISY_CC);
		if (Z_TYPE_P(records) == IS_NULL) {
			PHALCON_INIT_VAR(result);
			phalcon_read_property(&result, this_ptr, SL("_result"), PH_NOISY_CC);
			if (PHALCON_IS_NOT_FALSE(result)) {
				PHALCON_INIT_VAR(records);
				PHALCON_CALL_METHOD(records, result, "fetchall", PH_NO_CHECK);
				
				PHALCON_INIT_VAR(row_count);
				phalcon_fast_count(row_count, records TSRMLS_CC);
				phalcon_update_property_zval(this_ptr, SL("_rows"), row_count TSRMLS_CC);
			}
		}
	}
	
	PHALCON_INIT_VAR(model);
	phalcon_read_property(&model, this_ptr, SL("_model"), PH_NOISY_CC);
	
	PHALCON_INIT_VAR(cache);
	phalcon_read_property(&cache, this_ptr, SL("_cache"), PH_NOISY_CC);
	
	PHALCON_INIT_VAR(data);
	array_init(data);
	phalcon_array_update_string(&data, SL("model"), &model, PH_COPY | PH_SEPARATE TSRMLS_CC);
	phalcon_array_update_string(&data, SL("cache"), &cache, PH_COPY | PH_SEPARATE TSRMLS_CC);
	phalcon_array_update_string(&data, SL("rows"), &records, PH_COPY | PH_SEPARATE TSRMLS_CC);
	
	PHALCON_INIT_VAR(serialized);
	PHALCON_CALL_FUNC_PARAMS_1(serialized, "serialize", data);
	
	RETURN_CCTOR(serialized);
}
Пример #2
0
/**
 * Returns a complete resultset as an array, if the resultset has a big number of rows
 * it could consume more memory than currently it does.
 *
 * @return array
 */
PHP_METHOD(Phalcon_Mvc_Model_Resultset_Complex, toArray){

	zval *records, *valid = NULL, *current = NULL;
	zval *r0 = NULL;

	PHALCON_MM_GROW();

	PHALCON_INIT_VAR(records);
	array_init(records);
	phalcon_call_method_noret(this_ptr, "rewind");
	
	while (1) {
	
		PHALCON_INIT_NVAR(r0);
		phalcon_call_method(r0, this_ptr, "valid");
		PHALCON_CPY_WRT(valid, r0);
		if (PHALCON_IS_NOT_FALSE(valid)) {
		} else {
			break;
		}
	
		PHALCON_INIT_NVAR(current);
		phalcon_call_method(current, this_ptr, "current");
		phalcon_array_append(&records, current, PH_SEPARATE);
		phalcon_call_method_noret(this_ptr, "next");
	}
	
	RETURN_CTOR(records);
}
Пример #3
0
/**
 * Phalcon\Mvc\Model\Resultset\Complex constructor
 *
 * @param array $columnsTypes
 * @param Phalcon\Db\ResultInterface $result
 * @param Phalcon\Cache\BackendInterface $cache
 */
PHP_METHOD(Phalcon_Mvc_Model_Resultset_Complex, __construct){

	zval *columns_types, *result, *cache = NULL, *fetch_assoc;

	PHALCON_MM_GROW();

	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zz|z", &columns_types, &result, &cache) == FAILURE) {
		PHALCON_MM_RESTORE();
		RETURN_NULL();
	}

	if (!cache) {
		PHALCON_INIT_NVAR(cache);
	}
	
	phalcon_update_property_zval(this_ptr, SL("_columnTypes"), columns_types TSRMLS_CC);
	phalcon_update_property_zval(this_ptr, SL("_result"), result TSRMLS_CC);
	phalcon_update_property_zval(this_ptr, SL("_cache"), cache TSRMLS_CC);
	phalcon_update_property_long(this_ptr, SL("_type"), 1 TSRMLS_CC);
	if (PHALCON_IS_NOT_FALSE(result)) {
		PHALCON_INIT_VAR(fetch_assoc);
		ZVAL_LONG(fetch_assoc, 1);
		PHALCON_CALL_METHOD_PARAMS_1_NORETURN(result, "setfetchmode", fetch_assoc, PH_NO_CHECK);
	}
	
	PHALCON_MM_RESTORE();
}
Пример #4
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;
}
Пример #5
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;
}
Пример #6
0
/**
 * Get last row in the resultset
 *
 * @return Phalcon\Mvc\ModelInterface
 */
PHP_METHOD(Phalcon_Mvc_Model_Resultset, getLast){

	zval *one, *count, *pre_count, *valid, *current;

	PHALCON_MM_GROW();

	PHALCON_INIT_VAR(one);
	ZVAL_LONG(one, 1);
	
	PHALCON_INIT_VAR(count);
	phalcon_call_method(count, this_ptr, "count");
	
	PHALCON_INIT_VAR(pre_count);
	sub_function(pre_count, count, one TSRMLS_CC);
	phalcon_call_method_p1_noret(this_ptr, "seek", pre_count);
	
	PHALCON_INIT_VAR(valid);
	phalcon_call_method(valid, this_ptr, "valid");
	if (PHALCON_IS_NOT_FALSE(valid)) {
		PHALCON_INIT_VAR(current);
		phalcon_call_method(current, this_ptr, "current");
		RETURN_CCTOR(current);
	}
	
	RETURN_MM_FALSE;
}
Пример #7
0
/**
 * Counts how many rows are in the resultset
 *
 * @return int
 */
PHP_METHOD(Phalcon_Mvc_Model_Resultset, count){

	zval *count = NULL, *type, *result = NULL, *number_rows, *rows = NULL;

	PHALCON_MM_GROW();

	PHALCON_OBS_VAR(count);
	phalcon_read_property_this(&count, this_ptr, SL("_count"), PH_NOISY_CC);
	
	/** 
	 * We only calculate the row number is it wasn't calculated before
	 */
	if (Z_TYPE_P(count) == IS_NULL) {
	
		PHALCON_INIT_NVAR(count);
		ZVAL_LONG(count, 0);
	
		PHALCON_OBS_VAR(type);
		phalcon_read_property_this(&type, this_ptr, SL("_type"), PH_NOISY_CC);
		if (zend_is_true(type)) {
	
			/** 
			 * Here, the resultset act as a result that is fetched one by one
			 */
			PHALCON_OBS_VAR(result);
			phalcon_read_property_this(&result, this_ptr, SL("_result"), PH_NOISY_CC);
			if (PHALCON_IS_NOT_FALSE(result)) {
				PHALCON_INIT_VAR(number_rows);
				phalcon_call_method(number_rows, result, "numrows");
	
				PHALCON_INIT_NVAR(count);
				ZVAL_LONG(count, phalcon_get_intval(number_rows));
			}
		} else {
			/** 
			 * Here, the resultset act as an array
			 */
			PHALCON_OBS_VAR(rows);
			phalcon_read_property_this(&rows, this_ptr, SL("_rows"), PH_NOISY_CC);
			if (Z_TYPE_P(rows) == IS_NULL) {
	
				PHALCON_OBS_NVAR(result);
				phalcon_read_property_this(&result, this_ptr, SL("_result"), PH_NOISY_CC);
				if (Z_TYPE_P(result) == IS_OBJECT) {
					PHALCON_INIT_NVAR(rows);
					phalcon_call_method(rows, result, "fetchall");
					phalcon_update_property_this(this_ptr, SL("_rows"), rows TSRMLS_CC);
				}
			}
	
			PHALCON_INIT_NVAR(count);
			phalcon_fast_count(count, rows TSRMLS_CC);
		}
	
		phalcon_update_property_this(this_ptr, SL("_count"), count TSRMLS_CC);
	}
	
	RETURN_CCTOR(count);
}
Пример #8
0
/**
 * Appends an array of messages to the group
 *
 *<code>
 * $messages->appendMessages($messagesArray);
 *</code>
 *
 * @param Phalcon\Validation\MessageInterface[] $messages
 */
PHP_METHOD(Phalcon_Validation_Message_Group, appendMessages){

	zval *messages, *current_messages, *final_messages = NULL;
	zval *message = NULL;
	zval *r0 = NULL;

	PHALCON_MM_GROW();

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

	if (Z_TYPE_P(messages) != IS_ARRAY) { 
		if (Z_TYPE_P(messages) != IS_OBJECT) {
			PHALCON_THROW_EXCEPTION_STR(phalcon_validation_exception_ce, "The messages must be array or object");
			return;
		}
	}
	
	PHALCON_OBS_VAR(current_messages);
	phalcon_read_property(&current_messages, this_ptr, SL("_messages"), PH_NOISY_CC);
	if (Z_TYPE_P(messages) == IS_ARRAY) { 
	
		/** 
		 * An array of messages is simply merged into the current one
		 */
		if (Z_TYPE_P(current_messages) == IS_ARRAY) { 
			PHALCON_INIT_VAR(final_messages);
			PHALCON_CALL_FUNC_PARAMS_2(final_messages, "array_merge", current_messages, messages);
		} else {
			PHALCON_CPY_WRT(final_messages, messages);
		}
		phalcon_update_property_zval(this_ptr, SL("_messages"), final_messages TSRMLS_CC);
	} else {
		/** 
		 * A group of messages is iterated and appended one-by-one to the current list
		 */
		PHALCON_CALL_METHOD_NORETURN(messages, "rewind");
	
		while (1) {
	
			PHALCON_INIT_NVAR(r0);
			PHALCON_CALL_METHOD(r0, messages, "valid");
			if (PHALCON_IS_NOT_FALSE(r0)) {
			} else {
				break;
			}
	
			PHALCON_INIT_NVAR(message);
			PHALCON_CALL_METHOD(message, messages, "current");
			PHALCON_CALL_METHOD_PARAMS_1_NORETURN(this_ptr, "appendmessage", message);
			PHALCON_CALL_METHOD_NORETURN(messages, "next");
		}
	}
	
	PHALCON_MM_RESTORE();
}
Пример #9
0
/**
 * Rewinds resultset to its beginning
 *
 */
PHP_METHOD(Phalcon_Mvc_Model_Resultset, rewind){

	zval *type, *result = NULL, *active_row, *zero, *rows = NULL;

	PHALCON_MM_GROW();

	PHALCON_OBS_VAR(type);
	phalcon_read_property_this(&type, this_ptr, SL("_type"), PH_NOISY_CC);
	if (zend_is_true(type)) {
	
		/** 
		 * Here, the resultset act as a result that is fetched one by one
		 */
		PHALCON_OBS_VAR(result);
		phalcon_read_property_this(&result, this_ptr, SL("_result"), PH_NOISY_CC);
		if (PHALCON_IS_NOT_FALSE(result)) {
	
			PHALCON_OBS_VAR(active_row);
			phalcon_read_property_this(&active_row, this_ptr, SL("_activeRow"), PH_NOISY_CC);
			if (Z_TYPE_P(active_row) != IS_NULL) {
				PHALCON_INIT_VAR(zero);
				ZVAL_LONG(zero, 0);
				phalcon_call_method_p1_noret(result, "dataseek", zero);
			}
		}
	} else {
		/** 
		 * Here, the resultset act as an array
		 */
		PHALCON_OBS_VAR(rows);
		phalcon_read_property_this(&rows, this_ptr, SL("_rows"), PH_NOISY_CC);
		if (Z_TYPE_P(rows) == IS_NULL) {
	
			PHALCON_OBS_NVAR(result);
			phalcon_read_property_this(&result, this_ptr, SL("_result"), PH_NOISY_CC);
			if (Z_TYPE_P(result) == IS_OBJECT) {
				PHALCON_INIT_NVAR(rows);
				phalcon_call_method(rows, result, "fetchall");
				phalcon_update_property_this(this_ptr, SL("_rows"), rows TSRMLS_CC);
			}
		}
	
		if (Z_TYPE_P(rows) == IS_ARRAY) { 
			Z_SET_ISREF_P(rows);
			phalcon_call_func_p1_noret("reset", rows);
			Z_UNSET_ISREF_P(rows);
		}
	}
	
	phalcon_update_property_long(this_ptr, SL("_pointer"), 0 TSRMLS_CC);
	
	PHALCON_MM_RESTORE();
}
Пример #10
0
/**
 * Rewinds resultset to its beginning
 *
 */
PHP_METHOD(Phalcon_Mvc_Model_Resultset, rewind){

	zval *type, *z_zero, *rows;

	z_zero = &PHALCON_GLOBAL(z_zero);

	type = phalcon_read_property(getThis(), SL("_type"), PH_NOISY);
	if (zend_is_true(type)) {

		/** 
		 * Here, the resultset act as a result that is fetched one by one
		 */
		zval *result = phalcon_read_property(getThis(), SL("_result"), PH_NOISY);
		if (PHALCON_IS_NOT_FALSE(result)) {

			zval *active_row = phalcon_read_property(getThis(), SL("_activeRow"), PH_NOISY);
			if (Z_TYPE_P(active_row) != IS_NULL) {
				PHALCON_MM_GROW();
				PHALCON_CALL_METHOD(NULL, result, "dataseek", z_zero);
				PHALCON_MM_RESTORE();
			}
		}
	} else {
		/** 
		 * Here, the resultset act as an array
		 */
		rows = phalcon_read_property(getThis(), SL("_rows"), PH_NOISY);
		if (Z_TYPE_P(rows) == IS_NULL) {

			zval *result = phalcon_read_property(getThis(), SL("_result"), PH_NOISY);
			if (Z_TYPE_P(result) == IS_OBJECT) {
				zval *r = NULL;
				PHALCON_CALL_METHODW(&r, result, "fetchall");
				if (likely(Z_TYPE_P(r) == IS_ARRAY)) {
					zend_hash_internal_pointer_reset(Z_ARRVAL_P(r));
				}

				phalcon_update_property_this(getThis(), SL("_rows"), r);
				zval_ptr_dtor(r);
			}
		}
		else if (Z_TYPE_P(rows) == IS_ARRAY) {
			zend_hash_internal_pointer_reset(Z_ARRVAL_P(rows));
		}
	}

	phalcon_update_property_this(getThis(), SL("_pointer"), z_zero);
}
Пример #11
0
/**
 * Gets row in a specific position of the resultset
 *
 * @param int $index
 * @return Phalcon\Mvc\ModelInterface
 */
PHP_METHOD(Phalcon_Mvc_Model_Resultset, offsetGet){

	zval *index, *count, *exists, *pointer, *current = NULL, *valid;

	PHALCON_MM_GROW();

	phalcon_fetch_params(1, 1, 0, &index);
	
	PHALCON_INIT_VAR(count);
	phalcon_call_method(count, this_ptr, "count");
	
	PHALCON_INIT_VAR(exists);
	is_smaller_function(exists, index, count TSRMLS_CC);
	if (PHALCON_IS_TRUE(exists)) {
	
		/** 
		 * 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_EQUAL(pointer, index)) {
			PHALCON_INIT_VAR(current);
			phalcon_call_method(current, this_ptr, "current");
			RETURN_CCTOR(current);
		}
	
		/** 
		 * Move the cursor to the specific position
		 */
		phalcon_call_method_p1_noret(this_ptr, "seek", index);
	
		/** 
		 * Check if the last record returned is the requested
		 */
		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;
	}
	
	PHALCON_THROW_EXCEPTION_STR(phalcon_mvc_model_exception_ce, "The index does not exist in the cursor");
	return;
}
Пример #12
0
/**
 * Phalcon\Mvc\Model\Resultset\Simple constructor
 *
 * @param Phalcon\Mvc\Model $model
 * @param Phalcon\Db\Result\Pdo $result
 * @param Phalcon\Cache\Backend $cache
 */
PHP_METHOD(Phalcon_Mvc_Model_Resultset_Simple, __construct){

	zval *model = NULL, *result = NULL, *cache = NULL, *fetch_assoc = NULL, *limit = NULL;
	zval *row_count = NULL, *big_resultset = NULL;

	PHALCON_MM_GROW();
	
	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zz|z", &model, &result, &cache) == FAILURE) {
		PHALCON_MM_RESTORE();
		RETURN_NULL();
	}

	if (!cache) {
		PHALCON_ALLOC_ZVAL_MM(cache);
		ZVAL_NULL(cache);
	}
	
	phalcon_update_property_zval(this_ptr, SL("_model"), model TSRMLS_CC);
	phalcon_update_property_zval(this_ptr, SL("_result"), result TSRMLS_CC);
	phalcon_update_property_zval(this_ptr, SL("_cache"), cache TSRMLS_CC);
	if (PHALCON_IS_NOT_FALSE(result)) {
		PHALCON_INIT_VAR(fetch_assoc);
		ZVAL_LONG(fetch_assoc, 1);
		PHALCON_CALL_METHOD_PARAMS_1_NORETURN(result, "setfetchmode", fetch_assoc, PH_NO_CHECK);
		
		PHALCON_INIT_VAR(limit);
		ZVAL_LONG(limit, 32);
		
		PHALCON_INIT_VAR(row_count);
		PHALCON_CALL_METHOD(row_count, result, "numrows", PH_NO_CHECK);
		
		PHALCON_INIT_VAR(big_resultset);
		is_smaller_function(big_resultset, limit, row_count TSRMLS_CC);
		if (PHALCON_IS_TRUE(big_resultset)) {
			phalcon_update_property_long(this_ptr, SL("_type"), 1 TSRMLS_CC);
		} else {
			phalcon_update_property_long(this_ptr, SL("_type"), 0 TSRMLS_CC);
		}
		
		phalcon_update_property_zval(this_ptr, SL("_count"), row_count TSRMLS_CC);
	}
	
	PHALCON_MM_RESTORE();
}
Пример #13
0
/**
 * Gets row in a specific position of the resultset
 *
 * @param int $index
 * @return Phalcon\Mvc\ModelInterface
 */
PHP_METHOD(Phalcon_Mvc_Model_Resultset, offsetGet){

	zval *index, *count = NULL, *pointer, *valid = NULL;

	PHALCON_MM_GROW();

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

	PHALCON_CALL_METHOD(&count, getThis(), "count");
	if (PHALCON_LT(index, count)) {
		/** 
		 * Check if the last record returned is the current requested
		 */
		pointer = phalcon_read_property(getThis(), SL("_pointer"), PH_NOISY);
		if (PHALCON_IS_EQUAL(pointer, index)) {
			PHALCON_RETURN_CALL_METHOD(getThis(), "current");
			RETURN_MM();
		}

		/** 
		 * Move the cursor to the specific position
		 */
		PHALCON_CALL_METHOD(NULL, getThis(), "seek", index);

		/** 
		 * Check if the last record returned is the requested
		 */
		PHALCON_CALL_METHOD(&valid, getThis(), "valid");
		if (PHALCON_IS_NOT_FALSE(valid)) {
			PHALCON_RETURN_CALL_METHOD(getThis(), "current");
			RETURN_MM();
		}

		RETURN_MM_FALSE;
	}

	PHALCON_THROW_EXCEPTION_STR(phalcon_mvc_model_exception_ce, "The index does not exist in the cursor");
	return;
}
Пример #14
0
/**
 * Returns a complete resultset as an array, if the resultset has a big number of rows
 * it could consume more memory than currently it does.
 *
 * @return array
 */
PHP_METHOD(Phalcon_Mvc_Model_Resultset_Complex, toArray){

	zval *valid = NULL, *current = NULL;

	PHALCON_MM_GROW();

	array_init(return_value);
	PHALCON_CALL_METHOD(NULL, this_ptr, "rewind");
	
	while (1) {
		PHALCON_CALL_METHOD(&valid, this_ptr, "valid");
		if (!PHALCON_IS_NOT_FALSE(valid)) {
			break;
		}
	
		PHALCON_CALL_METHOD(&current, this_ptr, "current");
		phalcon_array_append(&return_value, current, 0);
		PHALCON_CALL_METHOD(NULL, this_ptr, "next");
	}
	
	PHALCON_MM_RESTORE();
}
Пример #15
0
/**
 * Get last row in the resultset
 *
 * @return Phalcon\Mvc\ModelInterface
 */
PHP_METHOD(Phalcon_Mvc_Model_Resultset, getLast){

	zval *z_one, *count = NULL, *pre_count, *valid = NULL;

	PHALCON_MM_GROW();

	z_one = &PHALCON_GLOBAL(z_one);

	PHALCON_CALL_METHOD(&count, getThis(), "count");

	PHALCON_INIT_VAR(pre_count);
	phalcon_sub_function(pre_count, count, z_one);
	PHALCON_CALL_METHOD(NULL, getThis(), "seek", pre_count);

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

	RETURN_MM_FALSE;
}
Пример #16
0
/**
 * Check whether internal resource has rows to fetch
 *
 * @return boolean
 */
PHP_METHOD(Phalcon_Mvc_Model_Resultset_Complex, valid){

	zval *type = NULL, *result, *row = NULL, *rows, *hydrate_mode, *underscore;
	zval *empty_str, *active_row = NULL, *columns_types;
	zval *dirty_state, *column = NULL, *alias = NULL, *source = NULL, *attributes = NULL;
	zval *column_map = NULL, *row_model = NULL, *attribute = NULL, *column_alias = NULL;
	zval *column_value = NULL, *keep_snapshots = NULL, *instance = NULL;
	zval *value = NULL, *sql_alias = NULL, *n_alias = NULL;
	HashTable *ah0, *ah1;
	HashPosition hp0, hp1;
	zval **hd;

	PHALCON_MM_GROW();

	PHALCON_OBS_VAR(type);
	phalcon_read_property_this(&type, this_ptr, SL("_type"), PH_NOISY_CC);
	if (zend_is_true(type)) {
	
		/** 
		 * The result is bigger than 32 rows so it's retrieved one by one
		 */
		PHALCON_OBS_VAR(result);
		phalcon_read_property_this(&result, this_ptr, SL("_result"), PH_NOISY_CC);
		if (PHALCON_IS_NOT_FALSE(result)) {
			PHALCON_INIT_VAR(row);
			PHALCON_CALL_METHOD_PARAMS_1(row, result, "fetch", result);
		} else {
			PHALCON_INIT_NVAR(row);
			ZVAL_BOOL(row, 0);
		}
	} else {
		/** 
		 * The full rows are dumped in this_ptr->rows
		 */
		PHALCON_OBS_VAR(rows);
		phalcon_read_property_this(&rows, this_ptr, SL("_rows"), PH_NOISY_CC);
		if (Z_TYPE_P(rows) == IS_ARRAY) { 
	
			PHALCON_INIT_NVAR(row);
			phalcon_array_get_current(row, rows TSRMLS_CC);
			if (Z_TYPE_P(row) == IS_OBJECT) {
				phalcon_array_next(rows);
			}
		} else {
			PHALCON_INIT_NVAR(row);
			ZVAL_BOOL(row, 0);
		}
	}
	
	/** 
	 * Valid records are arrays
	 */
	if (Z_TYPE_P(row) == IS_ARRAY || Z_TYPE_P(row) == IS_OBJECT) {
	
		/** 
		 * The result type=1 so we need to build every row
		 */
		if (zend_is_true(type)) {
	
			/** 
			 * Get current hydration mode
			 */
			PHALCON_OBS_VAR(hydrate_mode);
			phalcon_read_property_this(&hydrate_mode, this_ptr, SL("_hydrateMode"), PH_NOISY_CC);
	
			PHALCON_INIT_VAR(underscore);
			ZVAL_STRING(underscore, "_", 1);
	
			PHALCON_INIT_VAR(empty_str);
			ZVAL_STRING(empty_str, "", 1);
	
			/** 
			 * Each row in a complex result is a Phalcon\Mvc\Model\Row instance
			 */
	
			switch (phalcon_get_intval(hydrate_mode)) {
	
				case 0:
					PHALCON_INIT_VAR(active_row);
					object_init_ex(active_row, phalcon_mvc_model_row_ce);
					break;
	
				case 1:
					PHALCON_INIT_NVAR(active_row);
					array_init(active_row);
					break;
	
				case 2:
					PHALCON_INIT_NVAR(active_row);
					object_init(active_row);
					break;
	
			}
	
			/** 
			 * Create every record according to the column types
			 */
			PHALCON_OBS_VAR(columns_types);
			phalcon_read_property_this(&columns_types, this_ptr, SL("_columnTypes"), PH_NOISY_CC);
	
			/** 
			 * Set records as dirty state PERSISTENT by default
			 */
			PHALCON_INIT_VAR(dirty_state);
			ZVAL_LONG(dirty_state, 0);
	
			if (!phalcon_is_iterable(columns_types, &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);
	
				PHALCON_OBS_NVAR(type);
				phalcon_array_fetch_string(&type, column, SL("type"), PH_NOISY_CC);
				if (PHALCON_IS_STRING(type, "object")) {
	
					/** 
					 * Object columns are assigned column by column
					 */
					PHALCON_OBS_NVAR(source);
					phalcon_array_fetch_string(&source, column, SL("column"), PH_NOISY_CC);
	
					PHALCON_OBS_NVAR(attributes);
					phalcon_array_fetch_string(&attributes, column, SL("attributes"), PH_NOISY_CC);
	
					PHALCON_OBS_NVAR(column_map);
					phalcon_array_fetch_string(&column_map, column, SL("columnMap"), PH_NOISY_CC);
	
					/** 
					 * Assign the values from the _source_attribute notation to its real column name
					 */
					PHALCON_INIT_NVAR(row_model);
					array_init(row_model);
	
					if (!phalcon_is_iterable(attributes, &ah1, &hp1, 0, 0 TSRMLS_CC)) {
						return;
					}
	
					while (zend_hash_get_current_data_ex(ah1, (void**) &hd, &hp1) == SUCCESS) {
	
						PHALCON_GET_FOREACH_VALUE(attribute);
	
						/** 
						 * Columns are supposed to be in the form _table_field
						 */
						PHALCON_INIT_NVAR(column_alias);
						PHALCON_CONCAT_VVVV(column_alias, underscore, source, underscore, attribute);
	
						PHALCON_OBS_NVAR(column_value);
						phalcon_array_fetch(&column_value, row, column_alias, PH_NOISY_CC);
						phalcon_array_update_zval(&row_model, attribute, &column_value, PH_COPY | PH_SEPARATE TSRMLS_CC);
	
						zend_hash_move_forward_ex(ah1, &hp1);
					}
	
					/** 
					 * Generate the column value according to the hydration type
					 */
	
					switch (phalcon_get_intval(hydrate_mode)) {
	
						case 0:
							/** 
							 * Check if the resultset must keep snapshots
							 */
							if (phalcon_array_isset_string(column, SS("keepSnapshots"))) {
								PHALCON_OBS_NVAR(keep_snapshots);
								phalcon_array_fetch_string(&keep_snapshots, column, SL("keepSnapshots"), PH_NOISY_CC);
							} else {
								PHALCON_INIT_NVAR(keep_snapshots);
								ZVAL_BOOL(keep_snapshots, 0);
							}
	
							/** 
							 * Get the base instance
							 */
							PHALCON_OBS_NVAR(instance);
							phalcon_array_fetch_string(&instance, column, SL("instance"), PH_NOISY_CC);
	
							/** 
							 * Assign the values to the attributes using a column map
							 */
							PHALCON_INIT_NVAR(value);
							PHALCON_CALL_STATIC_PARAMS_5(value, "phalcon\\mvc\\model", "cloneresultmap", instance, row_model, column_map, dirty_state, keep_snapshots);
							break;
	
						default:
							/** 
							 * Other kinds of hydrations
							 */
							PHALCON_INIT_NVAR(value);
							PHALCON_CALL_STATIC_PARAMS_3(value, "phalcon\\mvc\\model", "cloneresultmaphydrate", row_model, column_map, hydrate_mode);
							break;
	
					}
	
					/** 
					 * The complete object is assigned to an attribute with the name of the alias or
					 * the model name
					 */
					PHALCON_OBS_NVAR(attribute);
					phalcon_array_fetch_string(&attribute, column, SL("balias"), PH_NOISY_CC);
				} else {
					/** 
					 * Scalar columns are simply assigned to the result object
					 */
					if (phalcon_array_isset_string(column, SS("sqlAlias"))) {
						PHALCON_OBS_NVAR(sql_alias);
						phalcon_array_fetch_string(&sql_alias, column, SL("sqlAlias"), PH_NOISY_CC);
	
						PHALCON_OBS_NVAR(value);
						phalcon_array_fetch(&value, row, sql_alias, PH_NOISY_CC);
					} else {
						PHALCON_OBS_NVAR(value);
						phalcon_array_fetch(&value, row, alias, PH_NOISY_CC);
					}
	
					/** 
					 * If a 'balias' is defined is not an unnamed scalar
					 */
					if (phalcon_array_isset_string(column, SS("balias"))) {
						PHALCON_CPY_WRT(attribute, alias);
					} else {
						PHALCON_INIT_NVAR(n_alias);
						phalcon_fast_str_replace(n_alias, underscore, empty_str, alias TSRMLS_CC);
						PHALCON_CPY_WRT(attribute, n_alias);
					}
				}
	
				/** 
				 * Assign the instance according to the hydration type
				 */
	
				switch (phalcon_get_intval(hydrate_mode)) {
	
					case 1:
						phalcon_array_update_zval(&active_row, attribute, &value, PH_COPY | PH_SEPARATE TSRMLS_CC);
						break;
	
					default:
						phalcon_update_property_zval_zval(active_row, attribute, value TSRMLS_CC);
						break;
	
				}
	
				zend_hash_move_forward_ex(ah0, &hp0);
			}
	
			/** 
			 * Store the generated row in this_ptr->activeRow to be retrieved by 'current'
			 */
			phalcon_update_property_this(this_ptr, SL("_activeRow"), active_row TSRMLS_CC);
		} else {
			/** 
			 * The row is already built so we just assign it to the activeRow
			 */
			phalcon_update_property_this(this_ptr, SL("_activeRow"), row TSRMLS_CC);
		}
		RETURN_MM_TRUE;
	}
	
	/** 
	 * There are no results to retrieve so we update this_ptr->activeRow as false
	 */
	phalcon_update_property_bool(this_ptr, SL("_activeRow"), 0 TSRMLS_CC);
	RETURN_MM_FALSE;
}
Пример #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);
}
Пример #18
0
/**
 * Changes internal pointer to a specific position in the resultset
 *
 * @param int $position
 */
PHP_METHOD(Phalcon_Mvc_Model_Resultset, seek){

	long i;
	zval *type, *result, *rows, *position;
	zval *pointer, *is_different;
	HashTable *ah0;

	PHALCON_MM_GROW();

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

	PHALCON_OBS_VAR(pointer);
	phalcon_read_property(&pointer, this_ptr, SL("_pointer"), PH_NOISY_CC);

	/**
	 * We only seek the records if the current position is diferent than the passed one
	 */
	PHALCON_INIT_VAR(is_different);
	is_not_equal_function(is_different, pointer, position TSRMLS_CC);
	if (PHALCON_IS_TRUE(is_different)) {

		PHALCON_OBS_VAR(type);
		phalcon_read_property(&type, this_ptr, SL("_type"), PH_NOISY_CC);
		if (zend_is_true(type)) {

			/**
			 * Here, the resultset is fetched one by one because is large
			 */
			PHALCON_OBS_VAR(result);
			phalcon_read_property(&result, this_ptr, SL("_result"), PH_NOISY_CC);
			phalcon_call_method_p1_noret(result, "dataseek", position);

		} else {

			/**
			 * Here, the resultset is a small array
			 */
			PHALCON_OBS_VAR(rows);
			phalcon_read_property(&rows, this_ptr, SL("_rows"), PH_NOISY_CC);

			/**
			 * We need to fetch the records because rows is null
			 */
			if (Z_TYPE_P(rows) == IS_NULL) {
				PHALCON_OBS_VAR(result);
				phalcon_read_property(&result, this_ptr, SL("_result"), PH_NOISY_CC);
				if (PHALCON_IS_NOT_FALSE(result)) {
					PHALCON_INIT_NVAR(rows);
					phalcon_call_method(rows, result, "fetchall");
					phalcon_update_property_zval(this_ptr, SL("_rows"), rows TSRMLS_CC);
				}
			}

			convert_to_long(position);

			if(Z_TYPE_P(rows) == IS_ARRAY){

				ah0 = Z_ARRVAL_P(rows);
				zend_hash_internal_pointer_reset(ah0);

				i = 0;
				while (1) {

					if (i >= Z_LVAL_P(position)) {
						break;
					}

					zend_hash_move_forward(ah0);
					i++;
				}
			}

			phalcon_update_property_zval(this_ptr, SL("_pointer"), position TSRMLS_CC);
		}
	}

	PHALCON_MM_RESTORE();}
Пример #19
0
/**
 * Check whether internal resource has rows to fetch
 *
 * @return boolean
 */
PHP_METHOD(Phalcon_Mvc_Model_Resultset_Complex, valid){

	zval *type = NULL, *row = NULL, *underscore;
	zval *empty_str, *active_row = NULL;
	zval *dirty_state, *column = NULL, *alias = NULL, *source = NULL, *attributes = NULL;
	zval *column_map = NULL, *row_model = NULL, *attribute = NULL, *column_alias = NULL;
	zval *column_value = NULL;
	zval *value = NULL, *sql_alias = NULL, *n_alias = NULL;
	HashTable *ah0, *ah1;
	HashPosition hp0, hp1;
	zval **hd;
	int i_type, is_partial;

	PHALCON_MM_GROW();

	type       = phalcon_fetch_nproperty_this(this_ptr, SL("_type"), PH_NOISY TSRMLS_CC);
	i_type     = (Z_TYPE_P(type) == IS_LONG) ? Z_LVAL_P(type) : phalcon_get_intval(type);
	is_partial = (i_type == PHALCON_MVC_MODEL_RESULTSET_TYPE_PARTIAL);
	type       = NULL;

	PHALCON_INIT_VAR(row);
	if (is_partial) {
		/** 
		 * The result is bigger than 32 rows so it's retrieved one by one
		 */
		zval *result = phalcon_fetch_nproperty_this(this_ptr, SL("_result"), PH_NOISY TSRMLS_CC);
		if (PHALCON_IS_NOT_FALSE(result)) {
			PHALCON_CALL_METHOD(&row, result, "fetch", result);
		} else {
			ZVAL_FALSE(row);
		}
	} else {
		/** 
		 * The full rows are dumped in this_ptr->rows
		 */
		zval *rows = phalcon_fetch_nproperty_this(this_ptr, SL("_rows"), PH_NOISY TSRMLS_CC);
		if (Z_TYPE_P(rows) == IS_ARRAY) { 
			phalcon_array_get_current(row, rows);
			if (Z_TYPE_P(row) == IS_OBJECT) {
				zend_hash_move_forward(Z_ARRVAL_P(rows));
			}
		} else {
			ZVAL_FALSE(row);
		}
	}
	
	/** 
	 * Valid records are arrays
	 */
	if (Z_TYPE_P(row) == IS_ARRAY || Z_TYPE_P(row) == IS_OBJECT) {
	
		/** 
		 * The result type=1 so we need to build every row
		 */
		if (is_partial) {
	
			/** 
			 * Get current hydration mode
			 */
			zval *hydrate_mode  = phalcon_fetch_nproperty_this(this_ptr, SL("_hydrateMode"), PH_NOISY TSRMLS_CC);
			zval *columns_types = phalcon_fetch_nproperty_this(this_ptr, SL("_columnTypes"), PH_NOISY TSRMLS_CC);
			int i_hydrate_mode  = phalcon_get_intval(hydrate_mode);
	
			PHALCON_INIT_VAR(underscore);
			ZVAL_STRING(underscore, "_", 1);
	
			PHALCON_INIT_VAR(empty_str);
			ZVAL_EMPTY_STRING(empty_str);
	
			/** 
			 * Each row in a complex result is a Phalcon\Mvc\Model\Row instance
			 */
			PHALCON_INIT_VAR(active_row);
			switch (i_hydrate_mode) {
				case 0:
					object_init_ex(active_row, phalcon_mvc_model_row_ce);
					break;
	
				case 1:
					array_init(active_row);
					break;
	
				case 2:
				default:
					object_init(active_row);
					break;
			}
	
			/** 
			 * Create every record according to the column types
			 */
	
			/** 
			 * Set records as dirty state PERSISTENT by default
			 */
			PHALCON_INIT_VAR(dirty_state);
			ZVAL_LONG(dirty_state, 0);
	
			phalcon_is_iterable(columns_types, &ah0, &hp0, 0, 0);
	
			while (zend_hash_get_current_data_ex(ah0, (void**) &hd, &hp0) == SUCCESS) {
	
				PHALCON_GET_HKEY(alias, ah0, hp0);
				PHALCON_GET_HVALUE(column);
	
				PHALCON_OBS_NVAR(type);
				phalcon_array_fetch_string(&type, column, SL("type"), PH_NOISY);
				if (PHALCON_IS_STRING(type, "object")) {
	
					/** 
					 * Object columns are assigned column by column
					 */
					PHALCON_OBS_NVAR(source);
					phalcon_array_fetch_string(&source, column, SL("column"), PH_NOISY);
	
					PHALCON_OBS_NVAR(attributes);
					phalcon_array_fetch_string(&attributes, column, SL("attributes"), PH_NOISY);
	
					PHALCON_OBS_NVAR(column_map);
					phalcon_array_fetch_string(&column_map, column, SL("columnMap"), PH_NOISY);
	
					/** 
					 * Assign the values from the _source_attribute notation to its real column name
					 */
					PHALCON_INIT_NVAR(row_model);
					array_init(row_model);
	
					phalcon_is_iterable(attributes, &ah1, &hp1, 0, 0);
	
					while (zend_hash_get_current_data_ex(ah1, (void**) &hd, &hp1) == SUCCESS) {
	
						PHALCON_GET_HVALUE(attribute);
	
						/** 
						 * Columns are supposed to be in the form _table_field
						 */
						PHALCON_INIT_NVAR(column_alias);
						PHALCON_CONCAT_VVVV(column_alias, underscore, source, underscore, attribute);
	
						PHALCON_OBS_NVAR(column_value);
						phalcon_array_fetch(&column_value, row, column_alias, PH_NOISY);
						phalcon_array_update_zval(&row_model, attribute, column_value, PH_COPY | PH_SEPARATE);
	
						zend_hash_move_forward_ex(ah1, &hp1);
					}
	
					/** 
					 * Generate the column value according to the hydration type
					 */
					switch (phalcon_get_intval(hydrate_mode)) {
	
						case 0: {
							zval *keep_snapshots, *instance;

							/** 
							 * Check if the resultset must keep snapshots
							 */
							if (!phalcon_array_isset_string_fetch(&keep_snapshots, column, SS("keepSnapshots"))) {
								keep_snapshots = PHALCON_GLOBAL(z_false);
							}
	
							/** 
							 * Get the base instance
							 */
							if (!phalcon_array_isset_string_fetch(&instance, column, SS("instance"))) {
								php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Undefined index: instance");
								instance = PHALCON_GLOBAL(z_null);
							}
	
							/** 
							 * Assign the values to the attributes using a column map
							 */
							PHALCON_CALL_CE_STATIC(&value, phalcon_mvc_model_ce, "cloneresultmap", instance, row_model, column_map, dirty_state, keep_snapshots);
							break;
						}
	
						default:
							/** 
							 * Other kinds of hydrations
							 */
							PHALCON_CALL_CE_STATIC(&value, phalcon_mvc_model_ce, "cloneresultmaphydrate", row_model, column_map, hydrate_mode);
							break;
	
					}
	
					/** 
					 * The complete object is assigned to an attribute with the name of the alias or
					 * the model name
					 */
					PHALCON_OBS_NVAR(attribute);
					if (phalcon_array_isset_string(column, SS("balias"))) {
						phalcon_array_fetch_string(&attribute, column, SL("balias"), PH_NOISY);
					}
				} else {
					/** 
					 * Scalar columns are simply assigned to the result object
					 */
					if (phalcon_array_isset_string(column, SS("sqlAlias"))) {
						PHALCON_OBS_NVAR(sql_alias);
						phalcon_array_fetch_string(&sql_alias, column, SL("sqlAlias"), PH_NOISY);
	
						PHALCON_OBS_NVAR(value);
						phalcon_array_fetch(&value, row, sql_alias, PH_NOISY);
					} else {
						PHALCON_OBS_NVAR(value);
						if (phalcon_array_isset(row, alias)) {
							phalcon_array_fetch(&value, row, alias, PH_NOISY);
						}
					}
	
					/** 
					 * If a 'balias' is defined is not an unnamed scalar
					 */
					if (phalcon_array_isset_string(column, SS("balias"))) {
						PHALCON_CPY_WRT(attribute, alias);
					} else {
						PHALCON_INIT_NVAR(n_alias);
						phalcon_fast_str_replace(n_alias, underscore, empty_str, alias);
						PHALCON_CPY_WRT(attribute, n_alias);
					}

					assert(attribute != NULL);
				}
	
				/** 
				 * Assign the instance according to the hydration type
				 */
				if (unlikely(!attribute)) {
					zend_throw_exception_ex(phalcon_mvc_model_exception_ce, 0 TSRMLS_CC, "Unexpected inconsistency: attribute is NULL");
					RETURN_MM();
				}
	
				switch (phalcon_get_intval(hydrate_mode)) {
	
					case 1:
						phalcon_array_update_zval(&active_row, attribute, value, PH_COPY | PH_SEPARATE);
						break;
	
					default:
						phalcon_update_property_zval_zval(active_row, attribute, value TSRMLS_CC);
						break;
	
				}
	
				zend_hash_move_forward_ex(ah0, &hp0);
			}
	
			/** 
			 * Store the generated row in this_ptr->activeRow to be retrieved by 'current'
			 */
			phalcon_update_property_this(this_ptr, SL("_activeRow"), active_row TSRMLS_CC);
		} else {
			/** 
			 * The row is already built so we just assign it to the activeRow
			 */
			phalcon_update_property_this(this_ptr, SL("_activeRow"), row TSRMLS_CC);
		}
		RETURN_MM_TRUE;
	}
	
	/** 
	 * There are no results to retrieve so we update this_ptr->activeRow as false
	 */
	phalcon_update_property_bool(this_ptr, SL("_activeRow"), 0 TSRMLS_CC);
	RETURN_MM_FALSE;
}
Пример #20
0
/**
 * Check whether internal resource has rows to fetch
 *
 * @return boolean
 */
PHP_METHOD(Phalcon_Mvc_Model_Resultset_Simple, valid){

	zval *type = NULL, *result = NULL, *row = NULL, *rows = NULL, *model = NULL, *active_row = NULL;

	PHALCON_MM_GROW();
	PHALCON_INIT_VAR(type);
	phalcon_read_property(&type, this_ptr, SL("_type"), PH_NOISY_CC);
	if (zend_is_true(type)) {
		PHALCON_INIT_VAR(result);
		phalcon_read_property(&result, this_ptr, SL("_result"), PH_NOISY_CC);
		if (PHALCON_IS_NOT_FALSE(result)) {
			PHALCON_INIT_VAR(row);
			PHALCON_CALL_METHOD_PARAMS_1(row, result, "fetcharray", result, PH_NO_CHECK);
		} else {
			PHALCON_INIT_VAR(row);
			ZVAL_BOOL(row, 0);
		}
	} else {
		PHALCON_INIT_VAR(rows);
		phalcon_read_property(&rows, this_ptr, SL("_rows"), PH_NOISY_CC);
		if (Z_TYPE_P(rows) == IS_NULL) {
			PHALCON_INIT_VAR(result);
			phalcon_read_property(&result, this_ptr, SL("_result"), PH_NOISY_CC);
			if (PHALCON_IS_NOT_FALSE(result)) {
				PHALCON_INIT_VAR(rows);
				PHALCON_CALL_METHOD(rows, result, "fetchall", PH_NO_CHECK);
				phalcon_update_property_zval(this_ptr, SL("_rows"), rows TSRMLS_CC);
			}
		}
		
		if (Z_TYPE_P(rows) == IS_ARRAY) { 
			Z_SET_ISREF_P(rows);
			PHALCON_INIT_VAR(row);
			PHALCON_CALL_FUNC_PARAMS_1(row, "current", rows);
			Z_UNSET_ISREF_P(rows);
			if (PHALCON_IS_NOT_FALSE(row)) {
				Z_SET_ISREF_P(rows);
				PHALCON_CALL_FUNC_PARAMS_1_NORETURN("next", rows);
				Z_UNSET_ISREF_P(rows);
			}
		} else {
			PHALCON_INIT_VAR(row);
			ZVAL_BOOL(row, 0);
		}
	}
	
	if (PHALCON_IS_NOT_FALSE(row)) {
		PHALCON_INIT_VAR(model);
		phalcon_read_property(&model, this_ptr, SL("_model"), PH_NOISY_CC);
		
		PHALCON_INIT_VAR(active_row);
		PHALCON_CALL_STATIC_PARAMS_2(active_row, "phalcon\\mvc\\model", "dumpresult", model, row);
		phalcon_update_property_zval(this_ptr, SL("_activeRow"), active_row TSRMLS_CC);
		PHALCON_MM_RESTORE();
		RETURN_TRUE;
	} else {
		phalcon_update_property_bool(this_ptr, SL("_activeRow"), 0 TSRMLS_CC);
	}
	
	PHALCON_MM_RESTORE();
	RETURN_FALSE;
}
Пример #21
0
/**
 * Check whether internal resource has rows to fetch
 *
 * @return boolean
 */
PHP_METHOD(Phalcon_Mvc_Model_Resultset_Complex, valid){

	zval *type = NULL, *result, *row = NULL, *rows, *underscore, *empty_str;
	zval *active_row, *columns_types, *column = NULL, *alias = NULL;
	zval *source = NULL, *instance = NULL, *attributes = NULL, *column_map = NULL;
	zval *row_model = NULL, *attribute = NULL, *column_alias = NULL, *value = NULL;
	zval *model_attribute = NULL, *sql_alias = NULL, *n_alias = 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();

	PHALCON_INIT_VAR(type);
	phalcon_read_property(&type, this_ptr, SL("_type"), PH_NOISY_CC);
	if (zend_is_true(type)) {
		PHALCON_INIT_VAR(result);
		phalcon_read_property(&result, this_ptr, SL("_result"), PH_NOISY_CC);
		if (PHALCON_IS_NOT_FALSE(result)) {
			PHALCON_INIT_VAR(row);
			PHALCON_CALL_METHOD_PARAMS_1(row, result, "fetch", result, PH_NO_CHECK);
		} else {
			PHALCON_INIT_NVAR(row);
			ZVAL_BOOL(row, 0);
		}
	} else {
		PHALCON_INIT_VAR(rows);
		phalcon_read_property(&rows, this_ptr, SL("_rows"), PH_NOISY_CC);
		Z_SET_ISREF_P(rows);
	
		PHALCON_INIT_NVAR(row);
		PHALCON_CALL_FUNC_PARAMS_1(row, "current", rows);
		Z_UNSET_ISREF_P(rows);
		if (zend_is_true(row)) {
			Z_SET_ISREF_P(rows);
			PHALCON_CALL_FUNC_PARAMS_1_NORETURN("next", rows);
			Z_UNSET_ISREF_P(rows);
		}
	}
	
	if (PHALCON_IS_NOT_FALSE(row)) {
		PHALCON_INIT_VAR(underscore);
		ZVAL_STRING(underscore, "_", 1);
	
		PHALCON_INIT_VAR(empty_str);
		ZVAL_STRING(empty_str, "", 1);
	
		PHALCON_INIT_VAR(active_row);
		object_init_ex(active_row, phalcon_mvc_model_row_ce);
	
		PHALCON_INIT_VAR(columns_types);
		phalcon_read_property(&columns_types, this_ptr, SL("_columnTypes"), PH_NOISY_CC);
	
		if (!phalcon_valid_foreach(columns_types TSRMLS_CC)) {
			return;
		}
	
		ah0 = Z_ARRVAL_P(columns_types);
		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(alias, ah0, hp0);
			PHALCON_GET_FOREACH_VALUE(column);
	
			PHALCON_INIT_NVAR(type);
			phalcon_array_fetch_string(&type, column, SL("type"), PH_NOISY_CC);
			if (PHALCON_COMPARE_STRING(type, "object")) {
				/** 
				 * Object columns are assigned column by column
				 */
				PHALCON_INIT_NVAR(source);
				phalcon_array_fetch_string(&source, column, SL("column"), PH_NOISY_CC);
	
				PHALCON_INIT_NVAR(instance);
				phalcon_array_fetch_string(&instance, column, SL("instance"), PH_NOISY_CC);
	
				PHALCON_INIT_NVAR(attributes);
				phalcon_array_fetch_string(&attributes, column, SL("attributes"), PH_NOISY_CC);
	
				PHALCON_INIT_NVAR(column_map);
				phalcon_array_fetch_string(&column_map, column, SL("columnMap"), PH_NOISY_CC);
	
				/** 
				 * Assign the values from the _source_attribute notation to its real column name
				 */
				PHALCON_INIT_NVAR(row_model);
				array_init(row_model);
	
				if (!phalcon_valid_foreach(attributes TSRMLS_CC)) {
					return;
				}
	
				ah1 = Z_ARRVAL_P(attributes);
				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_VALUE(attribute);
	
					PHALCON_INIT_NVAR(column_alias);
					PHALCON_CONCAT_VVVV(column_alias, underscore, source, underscore, attribute);
	
					PHALCON_INIT_NVAR(value);
					phalcon_array_fetch(&value, row, column_alias, PH_NOISY_CC);
					phalcon_array_update_zval(&row_model, attribute, &value, PH_COPY | PH_SEPARATE TSRMLS_CC);
	
					zend_hash_move_forward_ex(ah1, &hp1);
					goto ph_cycle_start_1;
	
				ph_cycle_end_1:
	
				/** 
				 * Assign the values to the attributes using a column map
				 */
				PHALCON_INIT_NVAR(model_attribute);
				PHALCON_CALL_STATIC_PARAMS_3(model_attribute, "phalcon\\mvc\\model", "dumpresultmap", instance, row_model, column_map);
	
				/** 
				 * The complete object is assigned to an attribute with the name of the alias or
				 * the model name
				 */
				PHALCON_INIT_NVAR(attribute);
				phalcon_array_fetch_string(&attribute, column, SL("balias"), PH_NOISY_CC);
				phalcon_update_property_zval_zval(active_row, attribute, model_attribute TSRMLS_CC);
			} else {
				/** 
				 * Scalar columns are simply assigned to the result object
				 */
				eval_int = phalcon_array_isset_string(column, SS("sqlAlias"));
				if (eval_int) {
					PHALCON_INIT_NVAR(sql_alias);
					phalcon_array_fetch_string(&sql_alias, column, SL("sqlAlias"), PH_NOISY_CC);
	
					PHALCON_INIT_NVAR(value);
					phalcon_array_fetch(&value, row, sql_alias, PH_NOISY_CC);
				} else {
					PHALCON_INIT_NVAR(value);
					phalcon_array_fetch(&value, row, alias, PH_NOISY_CC);
				}
	
				/** 
				 * If a 'balias' is defined is not an unnamed scalar
				 */
				eval_int = phalcon_array_isset_string(column, SS("balias"));
				if (eval_int) {
					phalcon_update_property_zval_zval(active_row, alias, value TSRMLS_CC);
				} else {
					PHALCON_INIT_NVAR(n_alias);
					phalcon_fast_str_replace(n_alias, underscore, empty_str, alias TSRMLS_CC);
					phalcon_update_property_zval_zval(active_row, n_alias, value TSRMLS_CC);
				}
			}
	
			zend_hash_move_forward_ex(ah0, &hp0);
			goto ph_cycle_start_0;
	
		ph_cycle_end_0:
	
		phalcon_update_property_zval(this_ptr, SL("_activeRow"), active_row TSRMLS_CC);
		PHALCON_MM_RESTORE();
		RETURN_TRUE;
	} else {
		phalcon_update_property_bool(this_ptr, SL("_activeRow"), 0 TSRMLS_CC);
	}
	
	PHALCON_MM_RESTORE();
	RETURN_FALSE;
}
Пример #22
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);
}
Пример #23
0
/**
 * Handles a MVC request
 *
 * @param string $uri
 * @return Phalcon\Http\ResponseInterface
 */
PHP_METHOD(Phalcon_Mvc_Application, handle){

	zval *uri = NULL, *dependency_injector, *events_manager;
	zval *event_name = NULL, *status = NULL, *service = NULL, *router, *module_name = NULL;
	zval *module_object = NULL, *modules, *exception_msg = NULL;
	zval *module, *class_name = NULL, *path, *module_params;
	zval *implicit_view, *view, *namespace_name;
	zval *controller_name = NULL, *action_name = NULL, *params = NULL;
	zval *dispatcher, *controller, *returned_response = NULL;
	zval *possible_response, *render_status = NULL, *response = NULL;
	zval *content;

	PHALCON_MM_GROW();

	phalcon_fetch_params(1, 0, 1, &uri);
	
	if (!uri) {
		PHALCON_INIT_VAR(uri);
	}
	
	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_THROW_EXCEPTION_STR(phalcon_mvc_application_exception_ce, "A dependency injection object is required to access internal services");
		return;
	}
	
	PHALCON_OBS_VAR(events_manager);
	phalcon_read_property_this(&events_manager, this_ptr, SL("_eventsManager"), PH_NOISY_CC);
	
	/** 
	 * Call boot event, this allow the developer to perform initialization actions
	 */
	if (Z_TYPE_P(events_manager) == IS_OBJECT) {
	
		PHALCON_INIT_VAR(event_name);
		ZVAL_STRING(event_name, "application:boot", 1);
	
		PHALCON_INIT_VAR(status);
		phalcon_call_method_p2(status, events_manager, "fire", event_name, this_ptr);
		if (PHALCON_IS_FALSE(status)) {
			RETURN_MM_FALSE;
		}
	}
	
	PHALCON_INIT_VAR(service);
	ZVAL_STRING(service, "router", 1);
	
	PHALCON_INIT_VAR(router);
	phalcon_call_method_p1(router, dependency_injector, "getshared", service);
	
	/** 
	 * Handle the URI pattern (if any)
	 */
	phalcon_call_method_p1_noret(router, "handle", uri);
	
	/** 
	 * Load module config
	 */
	PHALCON_INIT_VAR(module_name);
	phalcon_call_method(module_name, router, "getmodulename");
	
	/** 
	 * If the router doesn't return a valid module we use the default module
	 */
	if (!zend_is_true(module_name)) {
		PHALCON_OBS_NVAR(module_name);
		phalcon_read_property_this(&module_name, this_ptr, SL("_defaultModule"), PH_NOISY_CC);
	}
	
	PHALCON_INIT_VAR(module_object);
	
	/** 
	 * Process the module definition
	 */
	if (zend_is_true(module_name)) {
		if (Z_TYPE_P(events_manager) == IS_OBJECT) {
	
			PHALCON_INIT_NVAR(event_name);
			ZVAL_STRING(event_name, "application:beforeStartModule", 1);
	
			PHALCON_INIT_NVAR(status);
			phalcon_call_method_p3(status, events_manager, "fire", event_name, this_ptr, module_name);
			if (PHALCON_IS_FALSE(status)) {
				RETURN_MM_FALSE;
			}
		}
	
		/** 
		 * Check if the module passed by the router is registered in the modules container
		 */
		PHALCON_OBS_VAR(modules);
		phalcon_read_property_this(&modules, this_ptr, SL("_modules"), PH_NOISY_CC);
		if (!phalcon_array_isset(modules, module_name)) {
			PHALCON_INIT_VAR(exception_msg);
			PHALCON_CONCAT_SVS(exception_msg, "Module '", module_name, "' isn't registered in the application container");
			PHALCON_THROW_EXCEPTION_ZVAL(phalcon_mvc_application_exception_ce, exception_msg);
			return;
		}
	
		/** 
		 * A module definition must ne an array or an object
		 */
		PHALCON_OBS_VAR(module);
		phalcon_array_fetch(&module, modules, module_name, PH_NOISY);
		if (Z_TYPE_P(module) != IS_ARRAY) { 
			if (Z_TYPE_P(module) != IS_OBJECT) {
				PHALCON_THROW_EXCEPTION_STR(phalcon_mvc_application_exception_ce, "Invalid module definition");
				return;
			}
		}
	
		/** 
		 * An array module definition contains a path to a module definition class
		 */
		if (Z_TYPE_P(module) == IS_ARRAY) { 
	
			/** 
			 * Class name used to load the module definition
			 */
			if (phalcon_array_isset_string(module, SS("className"))) {
				PHALCON_OBS_VAR(class_name);
				phalcon_array_fetch_string(&class_name, module, SL("className"), PH_NOISY);
			} else {
				PHALCON_INIT_NVAR(class_name);
				ZVAL_STRING(class_name, "Module", 1);
			}
	
			/** 
			 * If developer specify a path try to include the file
			 */
			if (phalcon_array_isset_string(module, SS("path"))) {
	
				PHALCON_OBS_VAR(path);
				phalcon_array_fetch_string(&path, module, SL("path"), PH_NOISY);
				if (!phalcon_class_exists(class_name, 0 TSRMLS_CC)) {
					if (phalcon_file_exists(path TSRMLS_CC) == SUCCESS) {
						if (phalcon_require(path TSRMLS_CC) == FAILURE) {
							return;
						}
					} else {
						PHALCON_INIT_NVAR(exception_msg);
						PHALCON_CONCAT_SVS(exception_msg, "Module definition path '", path, "' doesn't exist");
						PHALCON_THROW_EXCEPTION_ZVAL(phalcon_mvc_application_exception_ce, exception_msg);
						return;
					}
				}
			}
	
			phalcon_call_method_p1(module_object, dependency_injector, "get", class_name);
	
			/** 
			 * 'registerAutoloaders' and 'registerServices' are automatically called
			 */
			phalcon_call_method_p1_noret(module_object, "registerautoloaders", dependency_injector);
			phalcon_call_method_p1_noret(module_object, "registerservices", dependency_injector);
		} else {
			/** 
			 * A module definition object, can be a Closure instance
			 */
			if (phalcon_is_instance_of(module, SL("Closure") TSRMLS_CC)) {
				PHALCON_INIT_VAR(module_params);
				array_init_size(module_params, 1);
				phalcon_array_append(&module_params, dependency_injector, PH_SEPARATE);
	
				PHALCON_INIT_NVAR(status);
				PHALCON_CALL_USER_FUNC_ARRAY(status, module, module_params);
			} else {
				PHALCON_THROW_EXCEPTION_STR(phalcon_mvc_application_exception_ce, "Invalid module definition");
				return;
			}
		}
	
		/** 
		 * Calling afterStartModule event
		 */
		if (Z_TYPE_P(events_manager) == IS_OBJECT) {
			phalcon_update_property_this(this_ptr, SL("_moduleObject"), module_object TSRMLS_CC);
	
			PHALCON_INIT_NVAR(event_name);
			ZVAL_STRING(event_name, "application:afterStartModule", 1);
	
			PHALCON_INIT_NVAR(status);
			phalcon_call_method_p3(status, events_manager, "fire", event_name, this_ptr, module_name);
			if (PHALCON_IS_FALSE(status)) {
				RETURN_MM_FALSE;
			}
		}
	}
	
	/** 
	 * Check whether use implicit views or not
	 */
	PHALCON_OBS_VAR(implicit_view);
	phalcon_read_property_this(&implicit_view, this_ptr, SL("_implicitView"), PH_NOISY_CC);
	if (PHALCON_IS_TRUE(implicit_view)) {
		PHALCON_INIT_NVAR(service);
		ZVAL_STRING(service, "view", 1);
	
		PHALCON_INIT_VAR(view);
		phalcon_call_method_p1(view, dependency_injector, "getshared", service);
	}
	
	/** 
	 * We get the parameters from the router and assign them to the dispatcher
	 */
	PHALCON_INIT_NVAR(module_name);
	phalcon_call_method(module_name, router, "getmodulename");
	
	PHALCON_INIT_VAR(namespace_name);
	phalcon_call_method(namespace_name, router, "getnamespacename");
	
	PHALCON_INIT_VAR(controller_name);
	phalcon_call_method(controller_name, router, "getcontrollername");
	
	PHALCON_INIT_VAR(action_name);
	phalcon_call_method(action_name, router, "getactionname");
	
	PHALCON_INIT_VAR(params);
	phalcon_call_method(params, router, "getparams");
	
	PHALCON_INIT_NVAR(service);
	ZVAL_STRING(service, "dispatcher", 1);
	
	PHALCON_INIT_VAR(dispatcher);
	phalcon_call_method_p1(dispatcher, dependency_injector, "getshared", service);
	
	/** 
	 * Assign the values passed from the router
	 */
	phalcon_call_method_p1_noret(dispatcher, "setmodulename", module_name);
	phalcon_call_method_p1_noret(dispatcher, "setnamespacename", namespace_name);
	phalcon_call_method_p1_noret(dispatcher, "setcontrollername", controller_name);
	phalcon_call_method_p1_noret(dispatcher, "setactionname", action_name);
	phalcon_call_method_p1_noret(dispatcher, "setparams", params);
	
	/** 
	 * Start the view component (start output buffering)
	 */
	if (PHALCON_IS_TRUE(implicit_view)) {
		phalcon_call_method_noret(view, "start");
	}
	
	/** 
	 * Calling beforeHandleRequest
	 */
	if (Z_TYPE_P(events_manager) == IS_OBJECT) {
	
		PHALCON_INIT_NVAR(event_name);
		ZVAL_STRING(event_name, "application:beforeHandleRequest", 1);
	
		PHALCON_INIT_NVAR(status);
		phalcon_call_method_p3(status, events_manager, "fire", event_name, this_ptr, dispatcher);
		if (PHALCON_IS_FALSE(status)) {
			RETURN_MM_FALSE;
		}
	}
	
	/** 
	 * The dispatcher must return an object
	 */
	PHALCON_INIT_VAR(controller);
	phalcon_call_method(controller, dispatcher, "dispatch");
	
	PHALCON_INIT_VAR(returned_response);
	ZVAL_BOOL(returned_response, 0);
	
	/** 
	 * Get the latest value returned by an action
	 */
	PHALCON_INIT_VAR(possible_response);
	phalcon_call_method(possible_response, dispatcher, "getreturnedvalue");
	if (Z_TYPE_P(possible_response) == IS_OBJECT) {
		/** 
		 * Check if the returned object is already a response
		 */
		phalcon_instance_of(returned_response, possible_response, phalcon_http_responseinterface_ce TSRMLS_CC);
	}
	
	/** 
	 * Calling afterHandleRequest
	 */
	if (Z_TYPE_P(events_manager) == IS_OBJECT) {
		PHALCON_INIT_NVAR(event_name);
		ZVAL_STRING(event_name, "application:afterHandleRequest", 1);
		phalcon_call_method_p3_noret(events_manager, "fire", event_name, this_ptr, controller);
	}
	
	/** 
	 * If the dispatcher returns an object we try to render the view in auto-rendering
	 * mode
	 */
	if (PHALCON_IS_FALSE(returned_response)) {
		if (PHALCON_IS_TRUE(implicit_view)) {
	
			if (Z_TYPE_P(controller) == IS_OBJECT) {
	
				PHALCON_INIT_VAR(render_status);
				ZVAL_BOOL(render_status, 1);
	
				/** 
				 * This allows to make a custom view render
				 */
				if (Z_TYPE_P(events_manager) == IS_OBJECT) {
					PHALCON_INIT_NVAR(event_name);
					ZVAL_STRING(event_name, "application:viewRender", 1);
	
					phalcon_call_method_p3(render_status, events_manager, "fire", event_name, this_ptr, view);
				}
	
				/** 
				 * Check if the view process has been treated by the developer
				 */
				if (PHALCON_IS_NOT_FALSE(render_status)) {
					PHALCON_INIT_NVAR(controller_name);
					phalcon_call_method(controller_name, dispatcher, "getcontrollername");
	
					PHALCON_INIT_NVAR(action_name);
					phalcon_call_method(action_name, dispatcher, "getactionname");
	
					PHALCON_INIT_NVAR(params);
					phalcon_call_method(params, dispatcher, "getparams");
	
					/** 
					 * Automatic render based on the latest controller executed
					 */
					phalcon_call_method_p3_noret(view, "render", controller_name, action_name, params);
				}
			}
		}
	}
	
	/** 
	 * Finish the view component (stop output buffering)
	 */
	if (PHALCON_IS_TRUE(implicit_view)) {
		phalcon_call_method_noret(view, "finish");
	}
	
	if (PHALCON_IS_FALSE(returned_response)) {
	
		PHALCON_INIT_NVAR(service);
		ZVAL_STRING(service, "response", 1);
	
		PHALCON_INIT_VAR(response);
		phalcon_call_method_p1(response, dependency_injector, "getshared", service);
		if (PHALCON_IS_TRUE(implicit_view)) {
			/** 
			 * The content returned by the view is passed to the response service
			 */
			PHALCON_INIT_VAR(content);
			phalcon_call_method(content, view, "getcontent");
			phalcon_call_method_p1_noret(response, "setcontent", content);
		}
	} else {
		/** 
		 * We don't need to create a response because there is a one already created
		 */
		PHALCON_CPY_WRT(response, possible_response);
	}
	
	/** 
	 * Calling beforeSendResponse
	 */
	if (Z_TYPE_P(events_manager) == IS_OBJECT) {
		PHALCON_INIT_NVAR(event_name);
		ZVAL_STRING(event_name, "application:beforeSendResponse", 1);
		phalcon_call_method_p3_noret(events_manager, "fire", event_name, this_ptr, response);
	}
	
	/** 
	 * Headers are automatically send
	 */
	phalcon_call_method_noret(response, "sendheaders");
	
	/** 
	 * Cookies are automatically send
	 */
	phalcon_call_method_noret(response, "sendcookies");
	
	/** 
	 * Return the response
	 */
	
	RETURN_CCTOR(response);
}
Пример #24
0
/**
 * Executes render process from dispatching data
 *
 *<code>
 * $view->start();
 * //Shows recent posts view (app/views/posts/recent.phtml)
 * $view->render('posts', 'recent');
 * $view->finish();
 *</code>
 *
 * @param string $controllerName
 * @param string $actionName
 * @param array $params
 */
PHP_METHOD(Phalcon_Mvc_View, render){

	zval *controller_name, *action_name, *params = NULL;
	zval *disabled, *contents = NULL, *layouts_dir = NULL, *layout;
	zval *layout_name = NULL, *engines, *pick_view, *render_view = NULL;
	zval *pick_view_action, *cache = NULL, *cache_level;
	zval *events_manager, *event_name = NULL, *status, *must_clean;
	zval *silence = NULL, *disabled_levels, *render_level;
	zval *enter_level = NULL, *templates_before, *template_before = NULL;
	zval *view_temp_path = NULL, *templates_after, *template_after = NULL;
	zval *main_view, *is_started, *is_fresh;
	zval *t0 = NULL, *t1 = NULL, *t2 = NULL, *t3 = NULL, *t4 = NULL;
	HashTable *ah0, *ah1;
	HashPosition hp0, hp1;
	zval **hd;

	PHALCON_MM_GROW();

	phalcon_fetch_params(1, 2, 1, &controller_name, &action_name, &params);
	
	if (!params) {
		PHALCON_INIT_VAR(params);
	}
	
	/** 
	 * If the view is disabled we simply update the buffer from any output produced in
	 * the controller
	 */
	PHALCON_OBS_VAR(disabled);
	phalcon_read_property_this(&disabled, this_ptr, SL("_disabled"), PH_NOISY_CC);
	if (PHALCON_IS_NOT_FALSE(disabled)) {
		PHALCON_INIT_VAR(contents);
		PHALCON_CALL_FUNC(contents, "ob_get_contents");
		phalcon_update_property_this(this_ptr, SL("_content"), contents TSRMLS_CC);
		RETURN_MM_FALSE;
	}
	
	phalcon_update_property_this(this_ptr, SL("_controllerName"), controller_name TSRMLS_CC);
	phalcon_update_property_this(this_ptr, SL("_actionName"), action_name TSRMLS_CC);
	phalcon_update_property_this(this_ptr, SL("_params"), params TSRMLS_CC);
	
	/** 
	 * Check if there is a layouts directory set
	 */
	PHALCON_OBS_VAR(layouts_dir);
	phalcon_read_property_this(&layouts_dir, this_ptr, SL("_layoutsDir"), PH_NOISY_CC);
	if (!zend_is_true(layouts_dir)) {
		PHALCON_INIT_NVAR(layouts_dir);
		ZVAL_STRING(layouts_dir, "layouts/", 1);
	}
	
	/** 
	 * Check if the user has defined a custom layout
	 */
	PHALCON_OBS_VAR(layout);
	phalcon_read_property_this(&layout, this_ptr, SL("_layout"), PH_NOISY_CC);
	if (zend_is_true(layout)) {
		PHALCON_CPY_WRT(layout_name, layout);
	} else {
		PHALCON_CPY_WRT(layout_name, controller_name);
	}
	
	/** 
	 * Load the template engines
	 */
	PHALCON_INIT_VAR(engines);
	PHALCON_CALL_METHOD(engines, this_ptr, "_loadtemplateengines");
	
	/** 
	 * Check if the user has picked a view diferent than the automatic
	 */
	PHALCON_OBS_VAR(pick_view);
	phalcon_read_property_this(&pick_view, this_ptr, SL("_pickView"), PH_NOISY_CC);
	if (Z_TYPE_P(pick_view) == IS_NULL) {
		PHALCON_INIT_VAR(render_view);
		PHALCON_CONCAT_VSV(render_view, controller_name, "/", action_name);
	} else {
		/** 
		 * The 'picked' view is an array, where the first element is controller and the
		 * second the action
		 */
		PHALCON_OBS_NVAR(render_view);
		phalcon_array_fetch_long(&render_view, pick_view, 0, PH_NOISY_CC);
		if (phalcon_array_isset_long(pick_view, 1)) {
			PHALCON_OBS_VAR(pick_view_action);
			phalcon_array_fetch_long(&pick_view_action, pick_view, 1, PH_NOISY_CC);
			PHALCON_CPY_WRT(layout_name, pick_view_action);
		}
	}
	
	PHALCON_INIT_VAR(cache);
	
	/** 
	 * Start the cache if there is a cache level enabled
	 */
	PHALCON_OBS_VAR(cache_level);
	phalcon_read_property_this(&cache_level, this_ptr, SL("_cacheLevel"), PH_NOISY_CC);
	if (zend_is_true(cache_level)) {
		PHALCON_CALL_METHOD(cache, this_ptr, "getcache");
	}
	
	PHALCON_OBS_VAR(events_manager);
	phalcon_read_property_this(&events_manager, this_ptr, SL("_eventsManager"), PH_NOISY_CC);
	
	/** 
	 * Create a virtual symbol table
	 */
	phalcon_create_symbol_table(TSRMLS_C);
	
	/** 
	 * Call beforeRender if there is an events manager
	 */
	if (Z_TYPE_P(events_manager) == IS_OBJECT) {
	
		PHALCON_INIT_VAR(event_name);
		ZVAL_STRING(event_name, "view:beforeRender", 1);
	
		PHALCON_INIT_VAR(status);
		PHALCON_CALL_METHOD_PARAMS_2(status, events_manager, "fire", event_name, this_ptr);
		if (PHALCON_IS_FALSE(status)) {
			RETURN_MM_FALSE;
		}
	}
	
	/** 
	 * Get the current content in the buffer maybe some output from the controller
	 */
	PHALCON_INIT_NVAR(contents);
	PHALCON_CALL_FUNC(contents, "ob_get_contents");
	phalcon_update_property_this(this_ptr, SL("_content"), contents TSRMLS_CC);
	
	PHALCON_INIT_VAR(must_clean);
	ZVAL_BOOL(must_clean, 1);
	
	PHALCON_INIT_VAR(silence);
	ZVAL_BOOL(silence, 1);
	
	/** 
	 * Disabled levels allow to avoid an specific level of rendering
	 */
	PHALCON_OBS_VAR(disabled_levels);
	phalcon_read_property_this(&disabled_levels, this_ptr, SL("_disabledLevels"), PH_NOISY_CC);
	
	/** 
	 * Render level will tell use when to stop
	 */
	PHALCON_OBS_VAR(render_level);
	phalcon_read_property_this(&render_level, this_ptr, SL("_renderLevel"), PH_NOISY_CC);
	if (zend_is_true(render_level)) {
	
		/** 
		 * Inserts view related to action
		 */
		PHALCON_INIT_VAR(t0);
		ZVAL_LONG(t0, 1);
		PHALCON_INIT_VAR(enter_level);
		is_smaller_or_equal_function(enter_level, t0, render_level TSRMLS_CC);
		if (PHALCON_IS_TRUE(enter_level)) {
			if (!phalcon_array_isset_long(disabled_levels, 1)) {
				PHALCON_CALL_METHOD_PARAMS_5_NORETURN(this_ptr, "_enginerender", engines, render_view, silence, must_clean, cache);
			}
		}
	
		/** 
		 * Inserts templates before layout
		 */
		PHALCON_INIT_VAR(t1);
		ZVAL_LONG(t1, 2);
	
		PHALCON_INIT_NVAR(enter_level);
		is_smaller_or_equal_function(enter_level, t1, render_level TSRMLS_CC);
		if (PHALCON_IS_TRUE(enter_level)) {
			if (!phalcon_array_isset_long(disabled_levels, 2)) {
	
				PHALCON_OBS_VAR(templates_before);
				phalcon_read_property_this(&templates_before, this_ptr, SL("_templatesBefore"), PH_NOISY_CC);
	
				/** 
				 * Templates before must be an array
				 */
				if (Z_TYPE_P(templates_before) == IS_ARRAY) { 
	
					ZVAL_BOOL(silence, 0);
	
					if (!phalcon_is_iterable(templates_before, &ah0, &hp0, 0, 0 TSRMLS_CC)) {
						return;
					}
	
					while (zend_hash_get_current_data_ex(ah0, (void**) &hd, &hp0) == SUCCESS) {
	
						PHALCON_GET_FOREACH_VALUE(template_before);
	
						PHALCON_INIT_NVAR(view_temp_path);
						PHALCON_CONCAT_VV(view_temp_path, layouts_dir, template_before);
						PHALCON_CALL_METHOD_PARAMS_5_NORETURN(this_ptr, "_enginerender", engines, view_temp_path, silence, must_clean, cache);
	
						zend_hash_move_forward_ex(ah0, &hp0);
					}
	
					ZVAL_BOOL(silence, 1);
				}
			}
		}
	
		/** 
		 * Inserts controller layout
		 */
		PHALCON_INIT_VAR(t2);
		ZVAL_LONG(t2, 3);
	
		PHALCON_INIT_NVAR(enter_level);
		is_smaller_or_equal_function(enter_level, t2, render_level TSRMLS_CC);
		if (PHALCON_IS_TRUE(enter_level)) {
			if (!phalcon_array_isset_long(disabled_levels, 3)) {
				PHALCON_INIT_NVAR(view_temp_path);
				PHALCON_CONCAT_VV(view_temp_path, layouts_dir, layout_name);
				PHALCON_CALL_METHOD_PARAMS_5_NORETURN(this_ptr, "_enginerender", engines, view_temp_path, silence, must_clean, cache);
			}
		}
	
		/** 
		 * Inserts templates after layout
		 */
		PHALCON_INIT_VAR(t3);
		ZVAL_LONG(t3, 4);
	
		PHALCON_INIT_NVAR(enter_level);
		is_smaller_or_equal_function(enter_level, t3, render_level TSRMLS_CC);
		if (PHALCON_IS_TRUE(enter_level)) {
			if (!phalcon_array_isset_long(disabled_levels, 4)) {
	
				/** 
				 * Templates after must be an array
				 */
				PHALCON_OBS_VAR(templates_after);
				phalcon_read_property_this(&templates_after, this_ptr, SL("_templatesAfter"), PH_NOISY_CC);
				if (Z_TYPE_P(templates_after) == IS_ARRAY) { 
	
					ZVAL_BOOL(silence, 0);
	
					if (!phalcon_is_iterable(templates_after, &ah1, &hp1, 0, 0 TSRMLS_CC)) {
						return;
					}
	
					while (zend_hash_get_current_data_ex(ah1, (void**) &hd, &hp1) == SUCCESS) {
	
						PHALCON_GET_FOREACH_VALUE(template_after);
	
						PHALCON_INIT_NVAR(view_temp_path);
						PHALCON_CONCAT_VV(view_temp_path, layouts_dir, template_after);
						PHALCON_CALL_METHOD_PARAMS_5_NORETURN(this_ptr, "_enginerender", engines, view_temp_path, silence, must_clean, cache);
	
						zend_hash_move_forward_ex(ah1, &hp1);
					}
	
					ZVAL_BOOL(silence, 1);
				}
			}
		}
	
		/** 
		 * Inserts main view
		 */
		PHALCON_INIT_VAR(t4);
		ZVAL_LONG(t4, 5);
	
		PHALCON_INIT_NVAR(enter_level);
		is_smaller_or_equal_function(enter_level, t4, render_level TSRMLS_CC);
		if (PHALCON_IS_TRUE(enter_level)) {
			if (!phalcon_array_isset_long(disabled_levels, 5)) {
				PHALCON_OBS_VAR(main_view);
				phalcon_read_property_this(&main_view, this_ptr, SL("_mainView"), PH_NOISY_CC);
				PHALCON_CALL_METHOD_PARAMS_5_NORETURN(this_ptr, "_enginerender", engines, main_view, silence, must_clean, cache);
			}
		}
	
		/** 
		 * Store the data in the cache
		 */
		if (Z_TYPE_P(cache) == IS_OBJECT) {
	
			PHALCON_INIT_VAR(is_started);
			PHALCON_CALL_METHOD(is_started, cache, "isstarted");
			if (PHALCON_IS_TRUE(is_started)) {
	
				PHALCON_INIT_VAR(is_fresh);
				PHALCON_CALL_METHOD(is_fresh, cache, "isfresh");
				if (PHALCON_IS_TRUE(is_fresh)) {
					PHALCON_CALL_METHOD_NORETURN(cache, "save");
				} else {
					PHALCON_CALL_METHOD_NORETURN(cache, "stop");
				}
			} else {
				PHALCON_CALL_METHOD_NORETURN(cache, "stop");
			}
		}
	}
	
	/** 
	 * Call afterRender event
	 */
	if (Z_TYPE_P(events_manager) == IS_OBJECT) {
		PHALCON_INIT_NVAR(event_name);
		ZVAL_STRING(event_name, "view:afterRender", 1);
		PHALCON_CALL_METHOD_PARAMS_2_NORETURN(events_manager, "fire", event_name, this_ptr);
	}
	
	RETURN_MM_NULL();
}
Пример #25
0
/**
 * Returns an array of Phalcon\Db\Column objects describing a table
 *
 * <code>print_r($connection->describeColumns("posts")); ?></code>
 *
 * @param string $table
 * @param string $schema
 * @return Phalcon\Db\Column[]
 */
PHP_METHOD(Phalcon_Db_Adapter_Pdo_Sqlite, describeColumns){

	zval *table, *schema = NULL, *columns, *dialect, *size_pattern;
	zval *sql, *fetch_assoc, *describe, *old_column = NULL;
	zval *field = NULL, *definition = NULL, *column_type = NULL, *pos = NULL, *attribute = NULL;
	zval *matches = NULL, *match_one = NULL, *column_name = NULL, *column = NULL;
	HashTable *ah0;
	HashPosition hp0;
	zval **hd;
	int eval_int;

	PHALCON_MM_GROW();

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

	if (!schema) {
		PHALCON_INIT_NVAR(schema);
	}
	
	PHALCON_INIT_VAR(columns);
	array_init(columns);
	
	PHALCON_INIT_VAR(dialect);
	phalcon_read_property(&dialect, this_ptr, SL("_dialect"), PH_NOISY_CC);
	
	PHALCON_INIT_VAR(size_pattern);
	ZVAL_STRING(size_pattern, "#\\(([0-9]+)(,[0-9]+)*\\)#", 1);
	
	PHALCON_INIT_VAR(sql);
	PHALCON_CALL_METHOD_PARAMS_2(sql, dialect, "describecolumns", table, schema, PH_NO_CHECK);
	
	PHALCON_INIT_VAR(fetch_assoc);
	phalcon_get_class_constant(fetch_assoc, phalcon_db_ce, SS("FETCH_ASSOC") TSRMLS_CC);
	
	PHALCON_INIT_VAR(describe);
	PHALCON_CALL_METHOD_PARAMS_2(describe, this_ptr, "fetchall", sql, fetch_assoc, PH_NO_CHECK);
	
	PHALCON_INIT_VAR(old_column);
	
	if (!phalcon_valid_foreach(describe TSRMLS_CC)) {
		return;
	}
	
	ah0 = Z_ARRVAL_P(describe);
	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(field);
		
		PHALCON_INIT_NVAR(definition);
		array_init(definition);
		add_assoc_long_ex(definition, SS("bindType"), 2);
		
		PHALCON_INIT_NVAR(column_type);
		phalcon_array_fetch_string(&column_type, field, SL("type"), PH_NOISY_CC);
		
		PHALCON_INIT_NVAR(pos);
		phalcon_fast_stripos_str(pos, column_type, SL("int") TSRMLS_CC);
		if (PHALCON_IS_NOT_FALSE(pos)) {
			phalcon_array_update_string_long(&definition, SL("type"), 0, PH_SEPARATE TSRMLS_CC);
			phalcon_array_update_string_bool(&definition, SL("isNumeric"), 1, PH_SEPARATE TSRMLS_CC);
			phalcon_array_update_string_long(&definition, SL("bindType"), 1, PH_SEPARATE TSRMLS_CC);
			
			PHALCON_INIT_NVAR(attribute);
			phalcon_array_fetch_string(&attribute, field, SL("pk"), PH_NOISY_CC);
			if (zend_is_true(attribute)) {
				phalcon_array_update_string_bool(&definition, SL("autoIncrement"), 1, PH_SEPARATE TSRMLS_CC);
			}
		} else {
			if (phalcon_memnstr_str(column_type, SL("varchar") TSRMLS_CC)) {
				phalcon_array_update_string_long(&definition, SL("type"), 2, PH_SEPARATE TSRMLS_CC);
			} else {
				if (phalcon_memnstr_str(column_type, SL("date") TSRMLS_CC)) {
					phalcon_array_update_string_long(&definition, SL("type"), 1, PH_SEPARATE TSRMLS_CC);
				} else {
					if (phalcon_memnstr_str(column_type, SL("decimal") TSRMLS_CC)) {
						phalcon_array_update_string_long(&definition, SL("type"), 3, PH_SEPARATE TSRMLS_CC);
						phalcon_array_update_string_bool(&definition, SL("isNumeric"), 1, PH_SEPARATE TSRMLS_CC);
						phalcon_array_update_string_long(&definition, SL("bindType"), 32, PH_SEPARATE TSRMLS_CC);
					} else {
						if (phalcon_memnstr_str(column_type, SL("char") TSRMLS_CC)) {
							phalcon_array_update_string_long(&definition, SL("type"), 5, PH_SEPARATE TSRMLS_CC);
						} else {
							if (phalcon_memnstr_str(column_type, SL("datetime") TSRMLS_CC)) {
								phalcon_array_update_string_long(&definition, SL("type"), 4, PH_SEPARATE TSRMLS_CC);
							} else {
								if (phalcon_memnstr_str(column_type, SL("text") TSRMLS_CC)) {
									phalcon_array_update_string_long(&definition, SL("type"), 6, PH_SEPARATE TSRMLS_CC);
								} else {
									if (phalcon_memnstr_str(column_type, SL("float") TSRMLS_CC)) {
										phalcon_array_update_string_long(&definition, SL("type"), 7, PH_SEPARATE TSRMLS_CC);
										phalcon_array_update_string_bool(&definition, SL("isNumeric"), 1, PH_SEPARATE TSRMLS_CC);
										phalcon_array_update_string_long(&definition, SL("bindType"), 32, PH_SEPARATE TSRMLS_CC);
									} else {
										if (phalcon_memnstr_str(column_type, SL("enum") TSRMLS_CC)) {
											phalcon_array_update_string_long(&definition, SL("type"), 5, PH_SEPARATE TSRMLS_CC);
										} else {
											phalcon_array_update_string_long(&definition, SL("type"), 2, PH_SEPARATE TSRMLS_CC);
										}
									}
								}
							}
						}
					}
				}
			}
		}
		
		if (phalcon_memnstr_str(column_type, SL("(") TSRMLS_CC)) {
			PHALCON_INIT_NVAR(matches);
			array_init(matches);
			Z_SET_ISREF_P(matches);
			
			PHALCON_INIT_NVAR(pos);
			PHALCON_CALL_FUNC_PARAMS_3(pos, "preg_match", size_pattern, column_type, matches);
			Z_UNSET_ISREF_P(matches);
			if (zend_is_true(pos)) {
				eval_int = phalcon_array_isset_long(matches, 1);
				if (eval_int) {
					PHALCON_INIT_NVAR(match_one);
					phalcon_array_fetch_long(&match_one, matches, 1, PH_NOISY_CC);
					phalcon_array_update_string(&definition, SL("size"), &match_one, PH_COPY | PH_SEPARATE TSRMLS_CC);
				}
			}
		}
		
		if (phalcon_memnstr_str(column_type, SL("unsigned") TSRMLS_CC)) {
			phalcon_array_update_string_bool(&definition, SL("unsigned"), 1, PH_SEPARATE TSRMLS_CC);
		}
		
		if (!zend_is_true(old_column)) {
			phalcon_array_update_string_bool(&definition, SL("first"), 1, PH_SEPARATE TSRMLS_CC);
		} else {
			phalcon_array_update_string(&definition, SL("after"), &old_column, PH_COPY | PH_SEPARATE TSRMLS_CC);
		}
		
		PHALCON_INIT_NVAR(attribute);
		phalcon_array_fetch_string(&attribute, field, SL("pk"), PH_NOISY_CC);
		if (zend_is_true(attribute)) {
			phalcon_array_update_string_bool(&definition, SL("primary"), 1, PH_SEPARATE TSRMLS_CC);
		}
		
		PHALCON_INIT_NVAR(attribute);
		phalcon_array_fetch_string(&attribute, field, SL("notnull"), PH_NOISY_CC);
		if (zend_is_true(attribute)) {
			phalcon_array_update_string_bool(&definition, SL("notNull"), 1, PH_SEPARATE TSRMLS_CC);
		}
		
		PHALCON_INIT_NVAR(column_name);
		phalcon_array_fetch_string(&column_name, field, SL("name"), PH_NOISY_CC);
		
		PHALCON_INIT_NVAR(column);
		object_init_ex(column, phalcon_db_column_ce);
		PHALCON_CALL_METHOD_PARAMS_2_NORETURN(column, "__construct", column_name, definition, PH_CHECK);
		phalcon_array_append(&columns, column, PH_SEPARATE TSRMLS_CC);
		PHALCON_CPY_WRT(old_column, column_name);
		
		zend_hash_move_forward_ex(ah0, &hp0);
		goto ph_cycle_start_0;
		
	ph_cycle_end_0:
	
	
	RETURN_CTOR(columns);
}
Пример #26
0
/**
 * Handles routing information received from the rewrite engine
 *
 * @param string $uri
 */
PHP_METHOD(Phalcon_Mvc_Router, handle) {

    zval *uri = NULL, *real_uri = NULL, *request = NULL, *route_found = NULL, *parts = NULL;
    zval *params = NULL, *matches = NULL, *routes = NULL, *reversed_routes = NULL;
    zval *route = NULL, *methods = NULL, *dependency_injector = NULL;
    zval *service = NULL, *match_method = NULL, *pattern = NULL, *have_parenthesis = NULL;
    zval *paths = NULL, *position = NULL, *part = NULL, *match_position = NULL;
    zval *module = NULL, *default_module = NULL, *controller = NULL, *default_controller = NULL;
    zval *action = NULL, *default_action = NULL, *params_str = NULL, *one = NULL;
    zval *str_params = NULL, *slash = NULL, *params_merge = NULL, *default_params = 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, "|z", &uri) == FAILURE) {
        PHALCON_MM_RESTORE();
        RETURN_NULL();
    }

    if (!uri) {
        PHALCON_ALLOC_ZVAL_MM(uri);
        ZVAL_NULL(uri);
    }

    if (!zend_is_true(uri)) {
        PHALCON_INIT_VAR(real_uri);
        PHALCON_CALL_METHOD(real_uri, this_ptr, "_getrewriteuri", PH_NO_CHECK);
    } else {
        PHALCON_CPY_WRT(real_uri, uri);
    }

    PHALCON_INIT_VAR(request);
    ZVAL_NULL(request);

    PHALCON_INIT_VAR(route_found);
    ZVAL_BOOL(route_found, 0);

    PHALCON_INIT_VAR(parts);
    array_init(parts);

    PHALCON_INIT_VAR(params);
    array_init(params);

    PHALCON_INIT_VAR(matches);
    ZVAL_NULL(matches);
    phalcon_update_property_bool(this_ptr, SL("_wasMatched"), 0 TSRMLS_CC);

    PHALCON_INIT_VAR(routes);
    phalcon_read_property(&routes, this_ptr, SL("_routes"), PH_NOISY_CC);

    PHALCON_INIT_VAR(reversed_routes);
    PHALCON_CALL_FUNC_PARAMS_1(reversed_routes, "array_reverse", routes);

    if (!phalcon_valid_foreach(reversed_routes TSRMLS_CC)) {
        return;
    }

    ah0 = Z_ARRVAL_P(reversed_routes);
    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(route);

    PHALCON_INIT_VAR(methods);
    PHALCON_CALL_METHOD(methods, route, "gethttpmethods", PH_NO_CHECK);
    if (Z_TYPE_P(methods) != IS_NULL) {
        if (Z_TYPE_P(request) == IS_NULL) {
            PHALCON_INIT_VAR(dependency_injector);
            phalcon_read_property(&dependency_injector, this_ptr, SL("_dependencyInjector"), PH_NOISY_CC);
            if (Z_TYPE_P(dependency_injector) != IS_OBJECT) {
                PHALCON_THROW_EXCEPTION_STR(phalcon_mvc_dispatcher_exception_ce, "A dependency injection container is required to access the 'request' service");
                return;
            }

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

            PHALCON_INIT_VAR(request);
            PHALCON_CALL_METHOD_PARAMS_1(request, dependency_injector, "getshared", service, PH_NO_CHECK);
        }

        PHALCON_INIT_VAR(match_method);
        PHALCON_CALL_METHOD_PARAMS_1(match_method, request, "ismethod", methods, PH_NO_CHECK);
        if (PHALCON_IS_FALSE(match_method)) {
            zend_hash_move_forward_ex(ah0, &hp0);
            goto ph_cycle_start_0;
        }
    }

    PHALCON_INIT_VAR(pattern);
    PHALCON_CALL_METHOD(pattern, route, "getcompiledpattern", PH_NO_CHECK);

    PHALCON_INIT_VAR(have_parenthesis);
    phalcon_fast_strpos_str(have_parenthesis, pattern, SL("(") TSRMLS_CC);
    if (PHALCON_IS_NOT_FALSE(have_parenthesis)) {
        Z_SET_ISREF_P(matches);
        PHALCON_INIT_VAR(route_found);
        PHALCON_CALL_FUNC_PARAMS_3(route_found, "preg_match", pattern, real_uri, matches);
        Z_UNSET_ISREF_P(matches);
    } else {
        PHALCON_INIT_VAR(route_found);
        is_equal_function(route_found, pattern, real_uri TSRMLS_CC);
    }

    if (zend_is_true(route_found)) {
        PHALCON_INIT_VAR(paths);
        PHALCON_CALL_METHOD(paths, route, "getpaths", PH_NO_CHECK);
        PHALCON_CPY_WRT(parts, paths);
        if (Z_TYPE_P(matches) == IS_ARRAY) {

            if (!phalcon_valid_foreach(paths TSRMLS_CC)) {
                return;
            }

            ah1 = Z_ARRVAL_P(paths);
            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_INIT_VAR(part);
            PHALCON_GET_FOREACH_KEY(part, ah1, hp1);
            PHALCON_GET_FOREACH_VALUE(position);

            eval_int = phalcon_array_isset(matches, position);
            if (eval_int) {
                PHALCON_INIT_VAR(match_position);
                phalcon_array_fetch(&match_position, matches, position, PH_NOISY_CC);
                phalcon_array_update_zval(&parts, part, &match_position, PH_COPY | PH_SEPARATE TSRMLS_CC);
            }

            zend_hash_move_forward_ex(ah1, &hp1);
            goto ph_cycle_start_1;

ph_cycle_end_1:

            phalcon_update_property_zval(this_ptr, SL("_matches"), matches TSRMLS_CC);
        }

        phalcon_update_property_zval(this_ptr, SL("_matchedRoute"), route TSRMLS_CC);
        goto ph_cycle_end_0;
    }

    zend_hash_move_forward_ex(ah0, &hp0);
    goto ph_cycle_start_0;

ph_cycle_end_0:

    if (zend_is_true(route_found)) {
        eval_int = phalcon_array_isset_string(parts, SL("module")+1);
        if (eval_int) {
            PHALCON_INIT_VAR(module);
            phalcon_array_fetch_string(&module, parts, SL("module"), PH_NOISY_CC);
            phalcon_update_property_zval(this_ptr, SL("_module"), module TSRMLS_CC);
            PHALCON_SEPARATE(parts);
            phalcon_array_unset_string(parts, SL("module")+1);
        } else {
            PHALCON_INIT_VAR(default_module);
            phalcon_read_property(&default_module, this_ptr, SL("_defaultModule"), PH_NOISY_CC);
            phalcon_update_property_zval(this_ptr, SL("_module"), default_module TSRMLS_CC);
        }
        eval_int = phalcon_array_isset_string(parts, SL("controller")+1);
        if (eval_int) {
            PHALCON_INIT_VAR(controller);
            phalcon_array_fetch_string(&controller, parts, SL("controller"), PH_NOISY_CC);
            phalcon_update_property_zval(this_ptr, SL("_controller"), controller TSRMLS_CC);
            PHALCON_SEPARATE(parts);
            phalcon_array_unset_string(parts, SL("controller")+1);
        } else {
            PHALCON_INIT_VAR(default_controller);
            phalcon_read_property(&default_controller, this_ptr, SL("_defaultController"), PH_NOISY_CC);
            phalcon_update_property_zval(this_ptr, SL("_controller"), default_controller TSRMLS_CC);
        }

        eval_int = phalcon_array_isset_string(parts, SL("action")+1);
        if (eval_int) {
            PHALCON_INIT_VAR(action);
            phalcon_array_fetch_string(&action, parts, SL("action"), PH_NOISY_CC);
            phalcon_update_property_zval(this_ptr, SL("_action"), action TSRMLS_CC);
            PHALCON_SEPARATE(parts);
            phalcon_array_unset_string(parts, SL("action")+1);
        } else {
            PHALCON_INIT_VAR(default_action);
            phalcon_read_property(&default_action, this_ptr, SL("_defaultAction"), PH_NOISY_CC);
            phalcon_update_property_zval(this_ptr, SL("_action"), default_action TSRMLS_CC);
        }

        eval_int = phalcon_array_isset_string(parts, SL("params")+1);
        if (eval_int) {
            PHALCON_INIT_VAR(params_str);
            phalcon_array_fetch_string(&params_str, parts, SL("params"), PH_NOISY_CC);

            PHALCON_INIT_VAR(one);
            ZVAL_LONG(one, 1);

            PHALCON_INIT_VAR(str_params);
            PHALCON_CALL_FUNC_PARAMS_2(str_params, "substr", params_str, one);
            if (zend_is_true(str_params)) {
                PHALCON_INIT_VAR(slash);
                ZVAL_STRING(slash, "/", 1);

                PHALCON_INIT_VAR(params);
                phalcon_fast_explode(params, slash, str_params TSRMLS_CC);
            }

            PHALCON_SEPARATE(parts);
            phalcon_array_unset_string(parts, SL("params")+1);
        }

        PHALCON_INIT_VAR(params_merge);
        PHALCON_CALL_FUNC_PARAMS_2(params_merge, "array_merge", params, parts);
        phalcon_update_property_zval(this_ptr, SL("_params"), params_merge TSRMLS_CC);
        phalcon_update_property_bool(this_ptr, SL("_wasMatched"), 1 TSRMLS_CC);
    } else {
        PHALCON_INIT_VAR(default_module);
        phalcon_read_property(&default_module, this_ptr, SL("_defaultModule"), PH_NOISY_CC);
        phalcon_update_property_zval(this_ptr, SL("_module"), default_module TSRMLS_CC);

        PHALCON_INIT_VAR(default_controller);
        phalcon_read_property(&default_controller, this_ptr, SL("_defaultController"), PH_NOISY_CC);
        phalcon_update_property_zval(this_ptr, SL("_controller"), default_controller TSRMLS_CC);

        PHALCON_INIT_VAR(default_action);
        phalcon_read_property(&default_action, this_ptr, SL("_defaultAction"), PH_NOISY_CC);
        phalcon_update_property_zval(this_ptr, SL("_action"), default_action TSRMLS_CC);

        PHALCON_INIT_VAR(default_params);
        phalcon_read_property(&default_params, this_ptr, SL("_defaultParams"), PH_NOISY_CC);
        phalcon_update_property_zval(this_ptr, SL("_params"), default_params TSRMLS_CC);
        phalcon_update_property_bool(this_ptr, SL("_wasMatched"), 0 TSRMLS_CC);
    }

    PHALCON_MM_RESTORE();
}
Пример #27
0
/**
 * Check whether internal resource has rows to fetch
 *
 * @return boolean
 */
PHP_METHOD(Phalcon_Mvc_Model_Resultset_Simple, valid){

	zval *type, *result = NULL, *row = NULL, *rows = NULL, *dirty_state, *hydrate_mode;
	zval *keep_snapshots, *column_map, *model, *active_row = NULL;

	PHALCON_MM_GROW();

	PHALCON_OBS_VAR(type);
	phalcon_read_property(&type, this_ptr, SL("_type"), PH_NOISY_CC);
	if (zend_is_true(type)) {
	
		PHALCON_OBS_VAR(result);
		phalcon_read_property(&result, this_ptr, SL("_result"), PH_NOISY_CC);
		if (Z_TYPE_P(result) == IS_OBJECT) {
			PHALCON_INIT_VAR(row);
			PHALCON_CALL_METHOD_PARAMS_1(row, result, "fetch", result);
		} else {
			PHALCON_INIT_NVAR(row);
			ZVAL_BOOL(row, 0);
		}
	} else {
		PHALCON_OBS_VAR(rows);
		phalcon_read_property(&rows, this_ptr, SL("_rows"), PH_NOISY_CC);
		if (Z_TYPE_P(rows) != IS_ARRAY) { 
	
			PHALCON_OBS_NVAR(result);
			phalcon_read_property(&result, this_ptr, SL("_result"), PH_NOISY_CC);
			if (Z_TYPE_P(result) == IS_OBJECT) {
				PHALCON_INIT_NVAR(rows);
				PHALCON_CALL_METHOD(rows, result, "fetchall");
				phalcon_update_property_zval(this_ptr, SL("_rows"), rows TSRMLS_CC);
			}
		}
	
		if (Z_TYPE_P(rows) == IS_ARRAY) { 
	
			PHALCON_INIT_NVAR(row);
			phalcon_array_get_current(row, rows TSRMLS_CC);
			if (PHALCON_IS_NOT_FALSE(row)) {
				phalcon_array_next(rows);
			}
		} else {
			PHALCON_INIT_NVAR(row);
			ZVAL_BOOL(row, 0);
		}
	}
	
	if (Z_TYPE_P(row) != IS_ARRAY) { 
		phalcon_update_property_bool(this_ptr, SL("_activeRow"), 0 TSRMLS_CC);
		RETURN_MM_FALSE;
	}
	
	/** 
	 * Set records as dirty state PERSISTENT by default
	 */
	PHALCON_INIT_VAR(dirty_state);
	ZVAL_LONG(dirty_state, 0);
	
	/** 
	 * Get current hydration mode
	 */
	PHALCON_OBS_VAR(hydrate_mode);
	phalcon_read_property(&hydrate_mode, this_ptr, SL("_hydrateMode"), PH_NOISY_CC);
	
	/** 
	 * Tell if the resultset is keeping snapshots
	 */
	PHALCON_OBS_VAR(keep_snapshots);
	phalcon_read_property(&keep_snapshots, this_ptr, SL("_keepSnapshots"), PH_NOISY_CC);
	
	/** 
	 * Get the resultset column map
	 */
	PHALCON_OBS_VAR(column_map);
	phalcon_read_property(&column_map, this_ptr, SL("_columnMap"), PH_NOISY_CC);
	
	/** 
	 * Hydrate based on the current hydration
	 */
	
	switch (phalcon_get_intval(hydrate_mode)) {
	
		case 0:
			/** 
			 * this_ptr->model is the base entity
			 */
			PHALCON_OBS_VAR(model);
			phalcon_read_property(&model, this_ptr, SL("_model"), PH_NOISY_CC);
	
			/** 
			 * Performs the standard hydration based on objects
			 */
			PHALCON_INIT_VAR(active_row);
			PHALCON_CALL_STATIC_PARAMS_5(active_row, "phalcon\\mvc\\model", "cloneresultmap", model, row, column_map, dirty_state, keep_snapshots);
			break;
	
		default:
			/** 
			 * Other kinds of hydrations
			 */
			PHALCON_INIT_NVAR(active_row);
			PHALCON_CALL_STATIC_PARAMS_3(active_row, "phalcon\\mvc\\model", "cloneresultmaphydrate", row, column_map, hydrate_mode);
			break;
	
	}
	phalcon_update_property_zval(this_ptr, SL("_activeRow"), active_row TSRMLS_CC);
	RETURN_MM_TRUE;
}
Пример #28
0
/**
 * Returns an array of Phalcon\Db\Column objects describing a table
 *
 * <code>print_r($connection->describeColumns("posts")); ?></code>
 *
 * @param string $table
 * @param string $schema
 * @return Phalcon\Db\Column[]
 */
PHP_METHOD(Phalcon_Db_Adapter_Pdo_Postgresql, describeColumns) {

    zval *table = NULL, *schema = NULL, *columns = NULL, *sql = NULL, *fetch_assoc = NULL;
    zval *describe = NULL, *old_column = NULL, *field = NULL, *definition = NULL;
    zval *char_size = NULL, *numeric_size = NULL, *column_type = NULL;
    zval *status = NULL, *attribute = NULL, *column_name = NULL, *column = NULL;
    zval *t0 = NULL;
    HashTable *ah0;
    HashPosition hp0;
    zval **hd;

    PHALCON_MM_GROW();

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

    if (!schema) {
        PHALCON_ALLOC_ZVAL_MM(schema);
        ZVAL_NULL(schema);
    }

    PHALCON_INIT_VAR(columns);
    array_init(columns);

    PHALCON_ALLOC_ZVAL_MM(t0);
    phalcon_read_property(&t0, this_ptr, SL("_dialect"), PH_NOISY_CC);

    PHALCON_INIT_VAR(sql);
    PHALCON_CALL_METHOD_PARAMS_2(sql, t0, "describecolumns", table, schema, PH_NO_CHECK);

    PHALCON_INIT_VAR(fetch_assoc);
    phalcon_get_class_constant(fetch_assoc, phalcon_db_ce, SL("FETCH_ASSOC") TSRMLS_CC);

    PHALCON_INIT_VAR(describe);
    PHALCON_CALL_METHOD_PARAMS_2(describe, this_ptr, "fetchall", sql, fetch_assoc, PH_NO_CHECK);

    PHALCON_INIT_VAR(old_column);
    ZVAL_NULL(old_column);

    if (!phalcon_valid_foreach(describe TSRMLS_CC)) {
        return;
    }

    ah0 = Z_ARRVAL_P(describe);
    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(field);

    PHALCON_INIT_VAR(definition);
    array_init(definition);

    PHALCON_INIT_VAR(char_size);
    phalcon_array_fetch_string(&char_size, field, SL("size"), PH_NOISY_CC);

    PHALCON_INIT_VAR(numeric_size);
    phalcon_array_fetch_string(&numeric_size, field, SL("numericsize"), PH_NOISY_CC);

    PHALCON_INIT_VAR(column_type);
    phalcon_array_fetch_string(&column_type, field, SL("type"), PH_NOISY_CC);

    PHALCON_INIT_VAR(status);
    phalcon_fast_strpos_str(status, column_type, SL("int") TSRMLS_CC);
    if (PHALCON_IS_NOT_FALSE(status)) {
        phalcon_array_update_string_long(&definition, SL("type"), 0, PH_SEPARATE TSRMLS_CC);
        phalcon_array_update_string_bool(&definition, SL("isNumeric"), 1, PH_SEPARATE TSRMLS_CC);
        phalcon_array_update_string(&definition, SL("size"), &numeric_size, PH_COPY | PH_SEPARATE TSRMLS_CC);
    } else {
        PHALCON_INIT_VAR(status);
        phalcon_fast_strpos_str(status, column_type, SL("varying") TSRMLS_CC);
        if (PHALCON_IS_NOT_FALSE(status)) {
            phalcon_array_update_string_long(&definition, SL("type"), 2, PH_SEPARATE TSRMLS_CC);
            phalcon_array_update_string(&definition, SL("size"), &char_size, PH_COPY | PH_SEPARATE TSRMLS_CC);
        } else {
            PHALCON_INIT_VAR(status);
            phalcon_fast_strpos_str(status, column_type, SL("date") TSRMLS_CC);
            if (PHALCON_IS_NOT_FALSE(status)) {
                phalcon_array_update_string_long(&definition, SL("type"), 1, PH_SEPARATE TSRMLS_CC);
                phalcon_array_update_string_long(&definition, SL("size"), 0, PH_SEPARATE TSRMLS_CC);
            } else {
                PHALCON_INIT_VAR(status);
                phalcon_fast_strpos_str(status, column_type, SL("numeric") TSRMLS_CC);
                if (PHALCON_IS_NOT_FALSE(status)) {
                    phalcon_array_update_string_long(&definition, SL("type"), 3, PH_SEPARATE TSRMLS_CC);
                    phalcon_array_update_string_bool(&definition, SL("isNumeric"), 1, PH_SEPARATE TSRMLS_CC);
                    phalcon_array_update_string(&definition, SL("size"), &numeric_size, PH_COPY | PH_SEPARATE TSRMLS_CC);
                } else {
                    PHALCON_INIT_VAR(status);
                    phalcon_fast_strpos_str(status, column_type, SL("char") TSRMLS_CC);
                    if (PHALCON_IS_NOT_FALSE(status)) {
                        phalcon_array_update_string_long(&definition, SL("type"), 5, PH_SEPARATE TSRMLS_CC);
                        phalcon_array_update_string(&definition, SL("size"), &char_size, PH_COPY | PH_SEPARATE TSRMLS_CC);
                    } else {
                        PHALCON_INIT_VAR(status);
                        phalcon_fast_strpos_str(status, column_type, SL("timestamp") TSRMLS_CC);
                        if (PHALCON_IS_NOT_FALSE(status)) {
                            phalcon_array_update_string_long(&definition, SL("type"), 4, PH_SEPARATE TSRMLS_CC);
                            phalcon_array_update_string_long(&definition, SL("size"), 0, PH_SEPARATE TSRMLS_CC);
                        } else {
                            PHALCON_INIT_VAR(status);
                            phalcon_fast_strpos_str(status, column_type, SL("text") TSRMLS_CC);
                            if (PHALCON_IS_NOT_FALSE(status)) {
                                phalcon_array_update_string_long(&definition, SL("type"), 6, PH_SEPARATE TSRMLS_CC);
                                phalcon_array_update_string(&definition, SL("size"), &char_size, PH_COPY | PH_SEPARATE TSRMLS_CC);
                            } else {
                                PHALCON_INIT_VAR(status);
                                phalcon_fast_strpos_str(status, column_type, SL("float") TSRMLS_CC);
                                if (PHALCON_IS_NOT_FALSE(status)) {
                                    phalcon_array_update_string_long(&definition, SL("type"), 7, PH_SEPARATE TSRMLS_CC);
                                    phalcon_array_update_string_bool(&definition, SL("isNumeric"), 1, PH_SEPARATE TSRMLS_CC);
                                    phalcon_array_update_string(&definition, SL("size"), &numeric_size, PH_COPY | PH_SEPARATE TSRMLS_CC);
                                } else {
                                    PHALCON_INIT_VAR(status);
                                    phalcon_fast_strpos_str(status, column_type, SL("uuid") TSRMLS_CC);
                                    if (PHALCON_IS_NOT_FALSE(status)) {
                                        phalcon_array_update_string_long(&definition, SL("type"), 5, PH_SEPARATE TSRMLS_CC);
                                        phalcon_array_update_string_long(&definition, SL("size"), 36, PH_SEPARATE TSRMLS_CC);
                                    } else {
                                        phalcon_array_update_string_long(&definition, SL("type"), 5, PH_SEPARATE TSRMLS_CC);
                                        phalcon_array_update_string(&definition, SL("size"), &char_size, PH_COPY | PH_SEPARATE TSRMLS_CC);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }

    PHALCON_INIT_VAR(status);
    phalcon_fast_strpos_str(status, column_type, SL("unsigned") TSRMLS_CC);
    if (PHALCON_IS_NOT_FALSE(status)) {
        phalcon_array_update_string_bool(&definition, SL("unsigned"), 1, PH_SEPARATE TSRMLS_CC);
    }

    if (Z_TYPE_P(old_column) == IS_NULL) {
        phalcon_array_update_string_bool(&definition, SL("first"), 1, PH_SEPARATE TSRMLS_CC);
    } else {
        phalcon_array_update_string(&definition, SL("after"), &old_column, PH_COPY | PH_SEPARATE TSRMLS_CC);
    }

    PHALCON_INIT_VAR(attribute);
    phalcon_array_fetch_string(&attribute, field, SL("key"), PH_NOISY_CC);
    if (PHALCON_COMPARE_STRING(attribute, "PRI")) {
        phalcon_array_update_string_bool(&definition, SL("primary"), 1, PH_SEPARATE TSRMLS_CC);
    }

    PHALCON_INIT_VAR(attribute);
    phalcon_array_fetch_string(&attribute, field, SL("null"), PH_NOISY_CC);
    if (PHALCON_COMPARE_STRING(attribute, "NO")) {
        phalcon_array_update_string_bool(&definition, SL("notNull"), 1, PH_SEPARATE TSRMLS_CC);
    }

    PHALCON_INIT_VAR(attribute);
    phalcon_array_fetch_string(&attribute, field, SL("extra"), PH_NOISY_CC);
    if (PHALCON_COMPARE_STRING(attribute, "auto_increment")) {
        phalcon_array_update_string_bool(&definition, SL("autoIncrement"), 1, PH_SEPARATE TSRMLS_CC);
    }

    PHALCON_INIT_VAR(column_name);
    phalcon_array_fetch_string(&column_name, field, SL("field"), PH_NOISY_CC);

    PHALCON_INIT_VAR(column);
    object_init_ex(column, phalcon_db_column_ce);
    PHALCON_CALL_METHOD_PARAMS_2_NORETURN(column, "__construct", column_name, definition, PH_CHECK);
    phalcon_array_append(&columns, column, PH_SEPARATE TSRMLS_CC);
    PHALCON_CPY_WRT(old_column, column_name);

    zend_hash_move_forward_ex(ah0, &hp0);
    goto ph_cycle_start_0;

ph_cycle_end_0:


    RETURN_CTOR(columns);
}
Пример #29
0
/**
 * Executes render process from dispatching data
 *
 *<code>
 * $view->start();
 * //Shows recent posts view (app/views/posts/recent.phtml)
 * $view->render('posts', 'recent');
 * $view->finish();
 *</code>
 *
 * @param string $controllerName
 * @param string $actionName
 * @param array $params
 */
PHP_METHOD(Phalcon_Mvc_View, render){

	zval *controller_name, *action_name, *params = NULL;
	zval *disabled, *layouts_dir = NULL, *engines, *pick_view;
	zval *render_view = NULL, *render_controller = NULL, *pick_view_action;
	zval *cache = NULL, *cache_level, *events_manager, *event_name = NULL;
	zval *status, *contents, *must_clean, *silence = NULL;
	zval *render_level, *enter_level = NULL, *templates_before;
	zval *template_before = NULL, *view_temp_path = NULL, *templates_after;
	zval *template_after = NULL, *main_view, *is_started;
	zval *is_fresh;
	zval *t0 = NULL, *t1 = NULL, *t2 = NULL, *t3 = NULL, *t4 = NULL;
	HashTable *ah0, *ah1;
	HashPosition hp0, hp1;
	zval **hd;
	int eval_int;

	PHALCON_MM_GROW();

	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zz|z", &controller_name, &action_name, &params) == FAILURE) {
		PHALCON_MM_RESTORE();
		RETURN_NULL();
	}

	if (!params) {
		PHALCON_INIT_NVAR(params);
		array_init(params);
	}
	
	PHALCON_INIT_VAR(disabled);
	phalcon_read_property(&disabled, this_ptr, SL("_disabled"), PH_NOISY_CC);
	if (PHALCON_IS_NOT_FALSE(disabled)) {
		PHALCON_MM_RESTORE();
		RETURN_FALSE;
	}
	
	PHALCON_INIT_VAR(layouts_dir);
	phalcon_read_property(&layouts_dir, this_ptr, SL("_layoutsDir"), PH_NOISY_CC);
	if (!zend_is_true(layouts_dir)) {
		PHALCON_INIT_NVAR(layouts_dir);
		ZVAL_STRING(layouts_dir, "layouts/", 1);
	}
	
	phalcon_update_property_zval(this_ptr, SL("_controllerName"), controller_name TSRMLS_CC);
	phalcon_update_property_zval(this_ptr, SL("_actionName"), action_name TSRMLS_CC);
	phalcon_update_property_zval(this_ptr, SL("_params"), params TSRMLS_CC);
	
	PHALCON_INIT_VAR(engines);
	PHALCON_CALL_METHOD(engines, this_ptr, "_loadtemplateengines", PH_NO_CHECK);
	
	PHALCON_INIT_VAR(pick_view);
	phalcon_read_property(&pick_view, this_ptr, SL("_pickView"), PH_NOISY_CC);
	if (Z_TYPE_P(pick_view) == IS_NULL) {
		PHALCON_INIT_VAR(render_view);
		PHALCON_CONCAT_VSV(render_view, controller_name, "/", action_name);
		PHALCON_CPY_WRT(render_controller, controller_name);
	} else {
		PHALCON_INIT_NVAR(render_view);
		phalcon_array_fetch_long(&render_view, pick_view, 0, PH_NOISY_CC);
		eval_int = phalcon_array_isset_long(pick_view, 1);
		if (eval_int) {
			PHALCON_INIT_VAR(pick_view_action);
			phalcon_array_fetch_long(&pick_view_action, pick_view, 1, PH_NOISY_CC);
			PHALCON_CPY_WRT(render_controller, pick_view_action);
		} else {
			PHALCON_CPY_WRT(render_controller, controller_name);
		}
	}
	
	PHALCON_INIT_VAR(cache);
	
	PHALCON_INIT_VAR(cache_level);
	phalcon_read_property(&cache_level, this_ptr, SL("_cacheLevel"), PH_NOISY_CC);
	if (zend_is_true(cache_level)) {
		PHALCON_CALL_METHOD(cache, this_ptr, "getcache", PH_NO_CHECK);
	}
	
	PHALCON_INIT_VAR(events_manager);
	phalcon_read_property(&events_manager, this_ptr, SL("_eventsManager"), PH_NOISY_CC);
	if (Z_TYPE_P(events_manager) == IS_OBJECT) {
		PHALCON_INIT_VAR(event_name);
		ZVAL_STRING(event_name, "view:beforeRender", 1);
		
		PHALCON_INIT_VAR(status);
		PHALCON_CALL_METHOD_PARAMS_2(status, events_manager, "fire", event_name, this_ptr, PH_NO_CHECK);
		if (PHALCON_IS_FALSE(status)) {
			PHALCON_MM_RESTORE();
			RETURN_FALSE;
		}
	}
	
	PHALCON_INIT_VAR(contents);
	PHALCON_CALL_FUNC(contents, "ob_get_contents");
	phalcon_update_property_zval(this_ptr, SL("_content"), contents TSRMLS_CC);
	
	PHALCON_INIT_VAR(must_clean);
	ZVAL_BOOL(must_clean, 1);
	
	PHALCON_INIT_VAR(silence);
	ZVAL_BOOL(silence, 1);
	
	PHALCON_INIT_VAR(render_level);
	phalcon_read_property(&render_level, this_ptr, SL("_renderLevel"), PH_NOISY_CC);
	if (zend_is_true(render_level)) {
		PHALCON_INIT_VAR(t0);
		ZVAL_LONG(t0, 1);
		PHALCON_INIT_VAR(enter_level);
		is_smaller_or_equal_function(enter_level, t0, render_level TSRMLS_CC);
		if (PHALCON_IS_TRUE(enter_level)) {
			PHALCON_CALL_METHOD_PARAMS_5_NORETURN(this_ptr, "_enginerender", engines, render_view, silence, must_clean, cache, PH_NO_CHECK);
		}
		
		PHALCON_INIT_VAR(t1);
		ZVAL_LONG(t1, 2);
		
		PHALCON_INIT_NVAR(enter_level);
		is_smaller_or_equal_function(enter_level, t1, render_level TSRMLS_CC);
		if (PHALCON_IS_TRUE(enter_level)) {
			PHALCON_INIT_VAR(templates_before);
			phalcon_read_property(&templates_before, this_ptr, SL("_templatesBefore"), PH_NOISY_CC);
			if (Z_TYPE_P(templates_before) == IS_ARRAY) { 
				ZVAL_BOOL(silence, 0);
				
				if (!phalcon_valid_foreach(templates_before TSRMLS_CC)) {
					return;
				}
				
				ah0 = Z_ARRVAL_P(templates_before);
				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(template_before);
					
					PHALCON_INIT_NVAR(view_temp_path);
					PHALCON_CONCAT_VV(view_temp_path, layouts_dir, template_before);
					PHALCON_CALL_METHOD_PARAMS_5_NORETURN(this_ptr, "_enginerender", engines, view_temp_path, silence, must_clean, cache, PH_NO_CHECK);
					
					zend_hash_move_forward_ex(ah0, &hp0);
					goto ph_cycle_start_0;
					
				ph_cycle_end_0:
				
				ZVAL_BOOL(silence, 1);
			}
		}
		
		PHALCON_INIT_VAR(t2);
		ZVAL_LONG(t2, 3);
		
		PHALCON_INIT_NVAR(enter_level);
		is_smaller_or_equal_function(enter_level, t2, render_level TSRMLS_CC);
		if (PHALCON_IS_TRUE(enter_level)) {
			PHALCON_INIT_NVAR(view_temp_path);
			PHALCON_CONCAT_VV(view_temp_path, layouts_dir, render_controller);
			PHALCON_CALL_METHOD_PARAMS_5_NORETURN(this_ptr, "_enginerender", engines, view_temp_path, silence, must_clean, cache, PH_NO_CHECK);
		}
		
		PHALCON_INIT_VAR(t3);
		ZVAL_LONG(t3, 4);
		
		PHALCON_INIT_NVAR(enter_level);
		is_smaller_or_equal_function(enter_level, t3, render_level TSRMLS_CC);
		if (PHALCON_IS_TRUE(enter_level)) {
			PHALCON_INIT_VAR(templates_after);
			phalcon_read_property(&templates_after, this_ptr, SL("_templatesAfter"), PH_NOISY_CC);
			if (Z_TYPE_P(templates_after) == IS_ARRAY) { 
				ZVAL_BOOL(silence, 0);
				
				if (!phalcon_valid_foreach(templates_after TSRMLS_CC)) {
					return;
				}
				
				ah1 = Z_ARRVAL_P(templates_after);
				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_VALUE(template_after);
					
					PHALCON_INIT_NVAR(view_temp_path);
					PHALCON_CONCAT_VV(view_temp_path, layouts_dir, template_after);
					PHALCON_CALL_METHOD_PARAMS_5_NORETURN(this_ptr, "_enginerender", engines, view_temp_path, silence, must_clean, cache, PH_NO_CHECK);
					
					zend_hash_move_forward_ex(ah1, &hp1);
					goto ph_cycle_start_1;
					
				ph_cycle_end_1:
				
				ZVAL_BOOL(silence, 1);
			}
		}
		
		PHALCON_INIT_VAR(t4);
		ZVAL_LONG(t4, 5);
		
		PHALCON_INIT_NVAR(enter_level);
		is_smaller_or_equal_function(enter_level, t4, render_level TSRMLS_CC);
		if (PHALCON_IS_TRUE(enter_level)) {
			PHALCON_INIT_VAR(main_view);
			phalcon_read_property(&main_view, this_ptr, SL("_mainView"), PH_NOISY_CC);
			PHALCON_CALL_METHOD_PARAMS_5_NORETURN(this_ptr, "_enginerender", engines, main_view, silence, must_clean, cache, PH_NO_CHECK);
		}
		
		if (Z_TYPE_P(cache) == IS_OBJECT) {
			PHALCON_INIT_VAR(is_started);
			PHALCON_CALL_METHOD(is_started, cache, "isstarted", PH_NO_CHECK);
			if (PHALCON_IS_TRUE(is_started)) {
				PHALCON_INIT_VAR(is_fresh);
				PHALCON_CALL_METHOD(is_fresh, cache, "isfresh", PH_NO_CHECK);
				if (PHALCON_IS_TRUE(is_fresh)) {
					PHALCON_CALL_METHOD_NORETURN(cache, "save", PH_NO_CHECK);
				} else {
					PHALCON_CALL_METHOD_NORETURN(cache, "stop", PH_NO_CHECK);
				}
			} else {
				PHALCON_CALL_METHOD_NORETURN(cache, "stop", PH_NO_CHECK);
			}
		}
	}
	
	if (Z_TYPE_P(events_manager) == IS_OBJECT) {
		PHALCON_INIT_NVAR(event_name);
		ZVAL_STRING(event_name, "view:afterRender", 1);
		PHALCON_CALL_METHOD_PARAMS_2_NORETURN(events_manager, "fire", event_name, this_ptr, PH_NO_CHECK);
	}
	
	PHALCON_MM_RESTORE();
}