Exemplo n.º 1
0
/**
 * Serializing a resultset will dump all related rows into a big array
 *
 * @return string
 */
PHP_METHOD(Phalcon_Mvc_Model_Resultset_Complex, serialize){

	zval *records, *valid = NULL, *current = NULL, *cache, *data, *serialized;
	zval *r0 = NULL;

	PHALCON_MM_GROW();

	PHALCON_INIT_VAR(records);
	array_init(records);
	PHALCON_CALL_METHOD_NORETURN(this_ptr, "rewind", PH_NO_CHECK);
	ph_cycle_start_0:
	
		PHALCON_INIT_NVAR(r0);
		PHALCON_CALL_METHOD(r0, this_ptr, "valid", PH_NO_CHECK);
		PHALCON_CPY_WRT(valid, r0);
		if (PHALCON_IS_NOT_TRUE(valid)) {
			goto ph_cycle_end_0;
		}
		PHALCON_INIT_NVAR(current);
		PHALCON_CALL_METHOD(current, this_ptr, "current", PH_NO_CHECK);
		phalcon_array_append(&records, current, PH_SEPARATE TSRMLS_CC);
		PHALCON_CALL_METHOD_NORETURN(this_ptr, "next", PH_NO_CHECK);
		goto ph_cycle_start_0;
	ph_cycle_end_0:
	
	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("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);
}
Exemplo n.º 2
0
/**
 * Phalcon\Events\Event constructor
 *
 * @param string $type
 * @param object $source
 * @param mixed $data
 * @param boolean $cancelable
 */
PHP_METHOD(Phalcon_Events_Event, __construct){

	zval *type, *source, *data = NULL, *cancelable = NULL;

	phalcon_fetch_params(0, 2, 2, &type, &source, &data, &cancelable);
	
	if (!data) {
		data = PHALCON_GLOBAL(z_null);
	}
	
	if (!cancelable) {
		cancelable = PHALCON_GLOBAL(z_true);
	}
	
	phalcon_update_property_this(this_ptr, SL("_type"), type TSRMLS_CC);
	phalcon_update_property_this(this_ptr, SL("_source"), source TSRMLS_CC);
	if (Z_TYPE_P(data) != IS_NULL) {
		phalcon_update_property_this(this_ptr, SL("_data"), data TSRMLS_CC);
	}
	
	if (PHALCON_IS_NOT_TRUE(cancelable)) {
		phalcon_update_property_this(this_ptr, SL("_cancelable"), cancelable TSRMLS_CC);
	}
}
Exemplo n.º 3
0
/**
 * Phalcon\Db\Column constructor
 *
 * @param string $columnName
 * @param array $definition
 */
PHP_METHOD(Phalcon_Db_Column, __construct){

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

	PHALCON_MM_GROW();

	phalcon_fetch_params(1, 2, 0, &column_name, &definition);
	
	phalcon_update_property_this(this_ptr, SL("_columnName"), column_name TSRMLS_CC);
	
	/** 
	 * Get the column type, one of the TYPE_* constants
	 */
	if (phalcon_array_isset_string(definition, SS("type"))) {
		PHALCON_OBS_VAR(type);
		phalcon_array_fetch_string(&type, definition, SL("type"), PH_NOISY_CC);
		phalcon_update_property_this(this_ptr, SL("_type"), type TSRMLS_CC);
	} else {
		PHALCON_THROW_EXCEPTION_STR(phalcon_db_exception_ce, "Column type is required");
		return;
	}
	
	/** 
	 * Check if the field is nullable
	 */
	if (phalcon_array_isset_string(definition, SS("notNull"))) {
		PHALCON_OBS_VAR(not_null);
		phalcon_array_fetch_string(&not_null, definition, SL("notNull"), PH_NOISY_CC);
		phalcon_update_property_this(this_ptr, SL("_notNull"), not_null TSRMLS_CC);
	}
	
	/** 
	 * Check if the field is primary key
	 */
	if (phalcon_array_isset_string(definition, SS("primary"))) {
		PHALCON_OBS_VAR(primary);
		phalcon_array_fetch_string(&primary, definition, SL("primary"), PH_NOISY_CC);
		phalcon_update_property_this(this_ptr, SL("_primary"), primary TSRMLS_CC);
	}
	
	if (phalcon_array_isset_string(definition, SS("size"))) {
		PHALCON_OBS_VAR(size);
		phalcon_array_fetch_string(&size, definition, SL("size"), PH_NOISY_CC);
		phalcon_update_property_this(this_ptr, SL("_size"), size TSRMLS_CC);
	}
	
	/** 
	 * Check if the column has a decimal scale
	 */
	if (phalcon_array_isset_string(definition, SS("scale"))) {
	
		PHALCON_INIT_VAR(t0);
		ZVAL_LONG(t0, 3);
		PHALCON_INIT_VAR(is_numeric);
		is_equal_function(is_numeric, type, t0 TSRMLS_CC);
		if (PHALCON_IS_NOT_TRUE(is_numeric)) {
			PHALCON_INIT_VAR(t1);
			ZVAL_LONG(t1, 7);
			is_equal_function(is_numeric, type, t1 TSRMLS_CC);
		}
	
		if (PHALCON_IS_TRUE(is_numeric)) {
			PHALCON_OBS_VAR(scale);
			phalcon_array_fetch_string(&scale, definition, SL("scale"), PH_NOISY_CC);
			phalcon_update_property_this(this_ptr, SL("_scale"), scale TSRMLS_CC);
		} else {
			PHALCON_THROW_EXCEPTION_STR(phalcon_db_exception_ce, "Column type does not support scale parameter");
			return;
		}
	}
	
	/** 
	 * Check if the field is unsigned (only MySQL)
	 */
	if (phalcon_array_isset_string(definition, SS("unsigned"))) {
		PHALCON_OBS_VAR(dunsigned);
		phalcon_array_fetch_string(&dunsigned, definition, SL("unsigned"), PH_NOISY_CC);
		phalcon_update_property_this(this_ptr, SL("_unsigned"), dunsigned TSRMLS_CC);
	}
	
	/** 
	 * Check if the field is numeric
	 */
	if (phalcon_array_isset_string(definition, SS("isNumeric"))) {
		PHALCON_OBS_NVAR(is_numeric);
		phalcon_array_fetch_string(&is_numeric, definition, SL("isNumeric"), PH_NOISY_CC);
		phalcon_update_property_this(this_ptr, SL("_isNumeric"), is_numeric TSRMLS_CC);
	}
	
	/** 
	 * Check if the field is auto-increment/serial
	 */
	if (phalcon_array_isset_string(definition, SS("autoIncrement"))) {
		if (PHALCON_IS_LONG(type, 0)) {
			PHALCON_OBS_VAR(auto_increment);
			phalcon_array_fetch_string(&auto_increment, definition, SL("autoIncrement"), PH_NOISY_CC);
			phalcon_update_property_this(this_ptr, SL("_autoIncrement"), auto_increment TSRMLS_CC);
		} else {
			PHALCON_THROW_EXCEPTION_STR(phalcon_db_exception_ce, "Column type cannot be auto-increment");
			return;
		}
	}
	
	/** 
	 * Check if the field is placed at the first position of the table
	 */
	if (phalcon_array_isset_string(definition, SS("first"))) {
		PHALCON_OBS_VAR(first);
		phalcon_array_fetch_string(&first, definition, SL("first"), PH_NOISY_CC);
		phalcon_update_property_this(this_ptr, SL("_first"), first TSRMLS_CC);
	}
	
	/** 
	 * Name of the column which is placed before the current field
	 */
	if (phalcon_array_isset_string(definition, SS("after"))) {
		PHALCON_OBS_VAR(after);
		phalcon_array_fetch_string(&after, definition, SL("after"), PH_NOISY_CC);
		phalcon_update_property_this(this_ptr, SL("_after"), after TSRMLS_CC);
	}
	
	/** 
	 * The bind type to cast the field when passing it to PDO
	 */
	if (phalcon_array_isset_string(definition, SS("bindType"))) {
		PHALCON_OBS_VAR(bind_type);
		phalcon_array_fetch_string(&bind_type, definition, SL("bindType"), PH_NOISY_CC);
		phalcon_update_property_this(this_ptr, SL("_bindType"), bind_type TSRMLS_CC);
	}
	
	PHALCON_MM_RESTORE();
}
Exemplo n.º 4
0
/**
 * Listens for notifications from the models manager
 *
 * @param string $type
 * @param Phalcon\Mvc\ModelInterface $model
 */
PHP_METHOD(Phalcon_Mvc_Model_Behavior_Timestampable, notify){

	zval *type, *model, *take_action, *options, *timestamp = NULL;
	zval *format, *generator, *field, *single_field = NULL;
	HashTable *ah0;
	HashPosition hp0;
	zval **hd;

	PHALCON_MM_GROW();

	phalcon_fetch_params(1, 2, 0, &type, &model);
	
	/** 
	 * Check if the developer decided to take action here
	 */
	PHALCON_INIT_VAR(take_action);
	phalcon_call_method_p1(take_action, this_ptr, "musttakeaction", type);
	if (PHALCON_IS_NOT_TRUE(take_action)) {
		RETURN_MM_NULL();
	}
	
	PHALCON_INIT_VAR(options);
	phalcon_call_method_p1(options, this_ptr, "getoptions", type);
	if (Z_TYPE_P(options) == IS_ARRAY) { 
	
		/** 
		 * The field name is required in this behavior
		 */
		if (!phalcon_array_isset_string(options, SS("field"))) {
			PHALCON_THROW_EXCEPTION_STR(phalcon_mvc_model_exception_ce, "The option 'field' is required");
			return;
		}
	
		PHALCON_INIT_VAR(timestamp);
		if (phalcon_array_isset_string(options, SS("format"))) {
			/** 
			 * Format is a format for date()
			 */
			PHALCON_OBS_VAR(format);
			phalcon_array_fetch_string(&format, options, SL("format"), PH_NOISY_CC);
	
			phalcon_call_func_p1(timestamp, "date", format);
		} else {
			if (phalcon_array_isset_string(options, SS("generator"))) {
	
				/** 
				 * A generator is a closure that produce the correct timestamp value
				 */
				PHALCON_OBS_VAR(generator);
				phalcon_array_fetch_string(&generator, options, SL("generator"), PH_NOISY_CC);
				if (Z_TYPE_P(generator) == IS_OBJECT) {
					if (phalcon_is_instance_of(generator, SL("Closure") TSRMLS_CC)) {
						PHALCON_INIT_NVAR(timestamp);
						PHALCON_CALL_USER_FUNC(timestamp, generator);
					}
				}
			}
		}
	
		/** 
		 * Last resort call time()
		 */
		if (Z_TYPE_P(timestamp) == IS_NULL) {
			PHALCON_INIT_NVAR(timestamp);
			ZVAL_LONG(timestamp, (long) time(NULL));
		}
	
		PHALCON_OBS_VAR(field);
		phalcon_array_fetch_string(&field, options, SL("field"), PH_NOISY_CC);
	
		/** 
		 * Assign the value to the field, use writeattribute if the property is protected
		 */
		if (unlikely(Z_TYPE_P(field) == IS_ARRAY)) { 
	
			phalcon_is_iterable(field, &ah0, &hp0, 0, 0);
	
			while (zend_hash_get_current_data_ex(ah0, (void**) &hd, &hp0) == SUCCESS) {
	
				PHALCON_GET_HVALUE(single_field);
	
				phalcon_call_method_p2_noret(model, "writeattribute", single_field, timestamp);
	
				zend_hash_move_forward_ex(ah0, &hp0);
			}
	
		} else {
			phalcon_call_method_p2_noret(model, "writeattribute", field, timestamp);
		}
	}
	
	PHALCON_MM_RESTORE();
}