Exemple #1
0
/**
 * Reads the ordered/reversed column map for certain model
 *
 *<code>
 *	print_r($metaData->readColumnMap(new Robots()));
 *</code>
 *
 * @param Phalcon\Mvc\ModelInterface $model
 * @return array
 */
PHP_METHOD(Phalcon_Mvc_Model_MetaData, readColumnMap) {

    zval *model, *key_name, *column_map = NULL, *null_value;
    zval *data;

    PHALCON_MM_GROW();

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

    if (Z_TYPE_P(model) != IS_OBJECT) {
        PHALCON_THROW_EXCEPTION_STR(phalcon_mvc_model_exception_ce, "A model instance is required to retrieve the meta-data");
        return;
    }

    PHALCON_INIT_VAR(key_name);
    phalcon_get_class(key_name, model, 1 TSRMLS_CC);

    PHALCON_OBS_VAR(column_map);
    phalcon_read_property_this(&column_map, this_ptr, SL("_columnMap"), PH_NOISY_CC);
    if (!phalcon_array_isset(column_map, key_name)) {
        PHALCON_INIT_VAR(null_value);
        phalcon_call_method_p4_noret(this_ptr, "_initialize", model, null_value, null_value, null_value);

        PHALCON_OBS_NVAR(column_map);
        phalcon_read_property_this(&column_map, this_ptr, SL("_columnMap"), PH_NOISY_CC);
    }

    PHALCON_OBS_VAR(data);
    phalcon_array_fetch(&data, column_map, key_name, PH_NOISY);

    RETURN_CCTOR(data);
}
Exemple #2
0
/**
 * Set the attributes that must be ignored from the UPDATE SQL generation
 *
 *<code>
 *	$metaData->setAutomaticUpdateAttributes(new Robots(), array('modified_at' => true));
 *</code>
 *
 * @param  Phalcon\Mvc\ModelInterface $model
 * @param  array $attributes
 */
PHP_METHOD(Phalcon_Mvc_Model_MetaData, setAutomaticUpdateAttributes) {

    zval *model, *attributes, *replace, *create_index;

    PHALCON_MM_GROW();

    phalcon_fetch_params(1, 3, 0, &model, &attributes, &replace);

    PHALCON_INIT_VAR(create_index);
    ZVAL_LONG(create_index, 11);
    phalcon_call_method_p4_noret(this_ptr, "writemetadataindex", model, create_index, attributes, replace);

    PHALCON_MM_RESTORE();
}
Exemple #3
0
/**
 * Reads meta-data for certain model using a MODEL_* constant
 *
 *<code>
 *	print_r($metaData->writeColumnMapIndex(new Robots(), MetaData::MODELS_REVERSE_COLUMN_MAP, array('leName' => 'name')));
 *</code>
 *
 * @param Phalcon\Mvc\ModelInterface $model
 * @param int $index
 * @return array
 */
PHP_METHOD(Phalcon_Mvc_Model_MetaData, readMetaDataIndex) {

    zval *model, *index, *table, *schema, *class_name;
    zval *key, *meta_data = NULL, *meta_data_index, *attributes;

    PHALCON_MM_GROW();

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

    if (Z_TYPE_P(model) != IS_OBJECT) {
        PHALCON_THROW_EXCEPTION_STR(phalcon_mvc_model_exception_ce, "A model instance is required to retrieve the meta-data");
        return;
    }
    if (Z_TYPE_P(index) != IS_LONG) {
        PHALCON_THROW_EXCEPTION_STR(phalcon_mvc_model_exception_ce, "Index must be a valid integer constant");
        return;
    }

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

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

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

    /**
     * Unique key for meta-data is created using class-name-schema-table
     */
    PHALCON_INIT_VAR(key);
    PHALCON_CONCAT_VSVV(key, class_name, "-", schema, table);

    PHALCON_OBS_VAR(meta_data);
    phalcon_read_property_this(&meta_data, this_ptr, SL("_metaData"), PH_NOISY_CC);
    if (!phalcon_array_isset(meta_data, key)) {
        phalcon_call_method_p4_noret(this_ptr, "_initialize", model, key, table, schema);

        PHALCON_OBS_NVAR(meta_data);
        phalcon_read_property_this(&meta_data, this_ptr, SL("_metaData"), PH_NOISY_CC);
    }

    PHALCON_OBS_VAR(meta_data_index);
    phalcon_array_fetch(&meta_data_index, meta_data, key, PH_NOISY);

    PHALCON_OBS_VAR(attributes);
    phalcon_array_fetch(&attributes, meta_data_index, index, PH_NOISY);

    RETURN_CCTOR(attributes);
}
Exemple #4
0
/**
 * This method is automatically called in Phalcon\Db\Adapter\Pdo constructor.
 * Call it when you need to restore a database connection
 *
 *<code>
 * //Make a connection
 * $connection = new Phalcon\Db\Adapter\Pdo\Mysql(array(
 *  'host' => '192.168.0.11',
 *  'username' => 'sigma',
 *  'password' => 'secret',
 *  'dbname' => 'blog',
 * ));
 *
 * //Reconnect
 * $connection->connect();
 * </code>
 *
 * @param 	array $descriptor
 * @return 	boolean
 */
PHP_METHOD(Phalcon_Db_Adapter_Pdo, connect){

	zval *descriptor = NULL, *username = NULL, *password = NULL, *dsn_parts;
	zval *value = NULL, *key = NULL, *dsn_attribute = NULL, *dsn_attributes = NULL;
	zval *pdo_type, *dsn, *options = NULL, *persistent, *pdo;
	zend_class_entry *ce;
	HashTable *ah0;
	HashPosition hp0;
	zval **hd;

	PHALCON_MM_GROW();

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

	if (!descriptor) {
		PHALCON_INIT_VAR(descriptor);
	} else {
		PHALCON_SEPARATE_PARAM(descriptor);
	}

	if (Z_TYPE_P(descriptor) == IS_NULL) {
		PHALCON_OBS_NVAR(descriptor);
		phalcon_read_property(&descriptor, this_ptr, SL("_descriptor"), PH_NOISY_CC);
	}

	/**
	 * Check for a username or use null as default
	 */
	if (phalcon_array_isset_string(descriptor, SS("username"))) {
		PHALCON_OBS_VAR(username);
		phalcon_array_fetch_string(&username, descriptor, SL("username"), PH_NOISY);
		phalcon_array_unset_string(&descriptor, SS("username"), PH_SEPARATE);
	} else {
		PHALCON_INIT_NVAR(username);
	}

	/**
	 * Check for a password or use null as default
	 */
	if (phalcon_array_isset_string(descriptor, SS("password"))) {
		PHALCON_OBS_VAR(password);
		phalcon_array_fetch_string(&password, descriptor, SL("password"), PH_NOISY);
		phalcon_array_unset_string(&descriptor, SS("password"), PH_SEPARATE);
	} else {
		PHALCON_INIT_NVAR(password);
	}

	/**
	 * Check if the developer has defined custom options or create one from scratch
	 */
	if (phalcon_array_isset_string(descriptor, SS("options"))) {
		PHALCON_OBS_VAR(options);
		phalcon_array_fetch_string(&options, descriptor, SL("options"), PH_NOISY);
		phalcon_array_unset_string(&descriptor, SS("options"), PH_SEPARATE);
	} else {
		PHALCON_INIT_NVAR(options);
		array_init(options);
	}

	/**
	 * Check if the user has defined a custom dsn
	 */
	if (!phalcon_array_isset_string(descriptor, SS("dsn"))) {

		PHALCON_INIT_VAR(dsn_parts);
		array_init(dsn_parts);

		if (!phalcon_is_iterable_ex(descriptor, &ah0, &hp0, 0, 0)) {
			return;
		}

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

			PHALCON_GET_FOREACH_KEY(key, ah0, hp0);
			PHALCON_GET_FOREACH_VALUE(value);

			PHALCON_INIT_NVAR(dsn_attribute);
			PHALCON_CONCAT_VSV(dsn_attribute, key, "=", value);
			phalcon_array_append(&dsn_parts, dsn_attribute, PH_SEPARATE);

			zend_hash_move_forward_ex(ah0, &hp0);
		}

		PHALCON_INIT_VAR(dsn_attributes);
		phalcon_fast_join_str(dsn_attributes, SL(";"), dsn_parts TSRMLS_CC);
	} else {
		PHALCON_OBS_NVAR(dsn_attributes);
		phalcon_array_fetch_string(&dsn_attributes, descriptor, SL("dsn"), PH_NOISY);
	}

	PHALCON_OBS_VAR(pdo_type);
	phalcon_read_property(&pdo_type, this_ptr, SL("_type"), PH_NOISY_CC);

	PHALCON_INIT_VAR(dsn);
	PHALCON_CONCAT_VSV(dsn, pdo_type, ":", dsn_attributes);

	/**
	 * Default options
	 */
	phalcon_array_update_long_long(&options, PDO_ATTR_ERRMODE, PDO_ERRMODE_EXCEPTION, PH_SEPARATE);
	//phalcon_array_update_long_long(&options, PDO_ATTR_CASE, PDO_CASE_LOWER, PH_SEPARATE);
	//phalcon_array_update_long_long(&options, PDO_ATTR_CURSOR, PDO_CURSOR_SCROLL, PH_SEPARATE);

	/**
	 * Check if the connection must be persistent
	 */
	if (phalcon_array_isset_string(descriptor, SS("persistent"))) {

		PHALCON_OBS_VAR(persistent);
		phalcon_array_fetch_string(&persistent, descriptor, SL("persistent"), PH_NOISY);
		if (zend_is_true(persistent)) {
			phalcon_array_update_long_bool(&options, PDO_ATTR_PERSISTENT, 1, PH_SEPARATE);
		}
	}

	/**
	 * Create the connection using PDO
	 */
	ce = zend_fetch_class(SL("PDO"), ZEND_FETCH_CLASS_AUTO TSRMLS_CC);

	PHALCON_INIT_VAR(pdo);
	object_init_ex(pdo, ce);
	phalcon_call_method_p4_noret(pdo, "__construct", dsn, username, password, options);

	phalcon_update_property_zval(this_ptr, SL("_pdo"), pdo TSRMLS_CC);

	PHALCON_MM_RESTORE();
}
Exemple #5
0
/**
 * Fires an event in the events manager causing that active listeners be notified about it
 *
 *<code>
 *	$eventsManager->fire('db', $connection);
 *</code>
 *
 * @param string $eventType
 * @param object $source
 * @param mixed  $data
 * @param int $cancelable
 * @return mixed
 */
PHP_METHOD(Phalcon_Events_Manager, fire){

	zval *event_type, *source, *data = NULL, *cancelable = NULL, *events;
	zval *exception_message, *event_parts, *type;
	zval *event_name, *status = NULL, *collect, *event = NULL, *fire_events = NULL;

	PHALCON_MM_GROW();

	phalcon_fetch_params(1, 2, 2, &event_type, &source, &data, &cancelable);
	
	if (!data) {
		PHALCON_INIT_VAR(data);
	}
	
	if (!cancelable) {
		PHALCON_INIT_VAR(cancelable);
		ZVAL_BOOL(cancelable, 1);
	}
	
	if (unlikely(Z_TYPE_P(event_type) != IS_STRING)) {
		PHALCON_THROW_EXCEPTION_STR(phalcon_events_exception_ce, "Event type must be a string");
		return;
	}
	
	PHALCON_OBS_VAR(events);
	phalcon_read_property_this(&events, this_ptr, SL("_events"), PH_NOISY_CC);
	if (Z_TYPE_P(events) != IS_ARRAY) { 
		RETURN_MM_NULL();
	}
	
	/** 
	 * All valid events must have a colon separator
	 */
	if (!phalcon_memnstr_str(event_type, SL(":"))) {
		PHALCON_INIT_VAR(exception_message);
		PHALCON_CONCAT_SV(exception_message, "Invalid event type ", event_type);
		PHALCON_THROW_EXCEPTION_ZVAL(phalcon_events_exception_ce, exception_message);
		return;
	}
	
	PHALCON_INIT_VAR(event_parts);
	phalcon_fast_explode_str(event_parts, SL(":"), event_type);
	
	PHALCON_OBS_VAR(type);
	phalcon_array_fetch_long(&type, event_parts, 0, PH_NOISY);
	
	PHALCON_OBS_VAR(event_name);
	phalcon_array_fetch_long(&event_name, event_parts, 1, PH_NOISY);
	
	PHALCON_INIT_VAR(status);
	
	/** 
	 * Responses must be traced?
	 */
	PHALCON_OBS_VAR(collect);
	phalcon_read_property_this(&collect, this_ptr, SL("_collect"), PH_NOISY_CC);
	if (zend_is_true(collect)) {
		phalcon_update_property_null(this_ptr, SL("_responses") TSRMLS_CC);
	}
	
	PHALCON_INIT_VAR(event);
	
	/** 
	 * Check if events are grouped by type
	 */
	if (phalcon_array_isset(events, type)) {
	
		PHALCON_OBS_VAR(fire_events);
		phalcon_array_fetch(&fire_events, events, type, PH_NOISY);
		if (Z_TYPE_P(fire_events) == IS_ARRAY || Z_TYPE_P(fire_events) == IS_OBJECT) {
			/** 
			 * Create the event context
			 */
			object_init_ex(event, phalcon_events_event_ce);
			phalcon_call_method_p4_noret(event, "__construct", event_name, source, data, cancelable);
	
			/** 
			 * Call the events queue
			 */
			phalcon_call_method_p2(status, this_ptr, "firequeue", fire_events, event);
		}
	}
	
	/** 
	 * Check if there are listeners for the event type itself
	 */
	if (phalcon_array_isset(events, event_type)) {
	
		PHALCON_OBS_NVAR(fire_events);
		phalcon_array_fetch(&fire_events, events, event_type, PH_NOISY);
		if (Z_TYPE_P(fire_events) == IS_ARRAY || Z_TYPE_P(fire_events) == IS_OBJECT) {
	
			/** 
			 * Create the event if it wasn't created before
			 */
			if (Z_TYPE_P(event) == IS_NULL) {
				PHALCON_INIT_NVAR(event);
				object_init_ex(event, phalcon_events_event_ce);
				phalcon_call_method_p4_noret(event, "__construct", event_name, source, data, cancelable);
	
			}
	
			/** 
			 * Call the events queue
			 */
			PHALCON_INIT_NVAR(status);
			phalcon_call_method_p2(status, this_ptr, "firequeue", fire_events, event);
		}
	}
	
	RETURN_CCTOR(status);
}
Exemple #6
0
/**
 * Writes meta-data for certain model using a MODEL_* constant
 *
 *<code>
 *	print_r($metaData->writeColumnMapIndex(new Robots(), MetaData::MODELS_REVERSE_COLUMN_MAP, array('leName' => 'name')));
 *</code>
 *
 * @param Phalcon\Mvc\ModelInterface $model
 * @param int $index
 * @param mixed $data
 */
PHP_METHOD(Phalcon_Mvc_Model_MetaData, writeMetaDataIndex) {

    zval *model, *index, *data, *replace, *table, *schema, *class_name;
    zval *key, *meta_data = NULL, *arr, *value;
    HashTable *ah2;
    HashPosition hp2;
    zval **hd;

    PHALCON_MM_GROW();

    phalcon_fetch_params(1, 4, 0, &model, &index, &data, &replace);

    if (Z_TYPE_P(model) != IS_OBJECT) {
        PHALCON_THROW_EXCEPTION_STR(phalcon_mvc_model_exception_ce, "A model instance is required to retrieve the meta-data");
        return;
    }
    if (Z_TYPE_P(index) != IS_LONG) {
        PHALCON_THROW_EXCEPTION_STR(phalcon_mvc_model_exception_ce, "Index must be a valid integer constant");
        return;
    }

    if (Z_TYPE_P(data) != IS_ARRAY) {
        if (Z_TYPE_P(data) != IS_STRING) {
            if (Z_TYPE_P(data) != IS_BOOL) {
                PHALCON_THROW_EXCEPTION_STR(phalcon_mvc_model_exception_ce, "Invalid data for index");
                return;
            }
        }
    }

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

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

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

    /**
     * Unique key for meta-data is created using class-name-schema-table
     */
    PHALCON_INIT_VAR(key);
    PHALCON_CONCAT_VSVV(key, class_name, "-", schema, table);

    PHALCON_OBS_VAR(meta_data);
    phalcon_read_property_this(&meta_data, this_ptr, SL("_metaData"), PH_NOISY_CC);
    if (!phalcon_array_isset(meta_data, key)) {
        phalcon_call_method_p4_noret(this_ptr, "_initialize", model, key, table, schema);

        PHALCON_OBS_NVAR(meta_data);
        phalcon_read_property_this(&meta_data, this_ptr, SL("_metaData"), PH_NOISY_CC);
    } else if (!zend_is_true(replace)) {
        PHALCON_OBS_VAR(arr);
        phalcon_array_fetch(&arr, meta_data, key, PH_NOISY);

        PHALCON_OBS_VAR(value);
        phalcon_array_fetch(&value, arr, index, PH_NOISY);

        PHALCON_SEPARATE_PARAM(data);
        phalcon_is_iterable(value, &ah2, &hp2, 0, 0);

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

            zval key2 = phalcon_get_current_key_w(ah2, &hp2);

            if (!phalcon_array_isset(data, &key2)) {
                phalcon_array_update_zval(&data, &key2, hd, PH_COPY | PH_SEPARATE);
            }

            zend_hash_move_forward_ex(ah2, &hp2);
        }
    }

    phalcon_array_update_multi_2(&meta_data, key, index, &data, 0);
    phalcon_update_property_this(this_ptr, SL("_metaData"), meta_data TSRMLS_CC);

    PHALCON_MM_RESTORE();
}