Views hook_views_query_alter JOIN - join

In hook_views_query_alter i build join query like this:
$join = new views_join();
$join->table = 'table_2';
$join->field = 'field_2';
$join->left_table = 'table_1';
$join->left_field = 'field_1';
$join->type = 'LEFT';
$join->extra = array(
array(
'table' => 'table_2',
'field' => 'field_3',
'value' => 'table_1.field_4',
),
);
The query should looks like this:
LEFT JOIN {table_2} table_2 ON table_1.field_1 = table_2.field_2 AND table_1.field_3 = table_2.field_4
My problem is here 'value' => 'table_1.field_4'. I can't set value as field. It treat it as string value. So at the and in my query i have unwanted single quotes.
So at the end my query looks like this:
LEFT JOIN {table_2} table_2 ON table_1.field_1 = table_2.field_2 AND table_1.field_3 = 'table_2.field_4'

I'm not sure if it's the best solution but this:
$join = new views_join();
$join->table = 'table_2';
$join->field = 'field_2';
$join->left_table = 'table_1';
$join->left_field = 'field_1';
$join->type = 'LEFT';
$join->extra = 'table_1.field_3 = table_2.field_4';
works for me.

Related

How to get value from custom profile field in moodle in code?

How to get value from custom profile field in moodle?
i want to include the profile field in user.php
If you want to load all the user profile fields, then you can start with a user object
$user = $DB->get_record('user', array('id' => $userid));
and then call
profile_load_custom_fields($user);
to fill in the $user->profile with an array of all the custom fields.
Alternatively, you can get the contents of a single profile field via:
$sql = "SELECT ud.data
FROM {user_info_data} ud
JOIN {user_info_field} uf ON uf.id = ud.fieldid
WHERE ud.userid = :userid AND uf.shortname = :fieldname";
$params = array('userid' => $userid, 'fieldname' => 'NameOfCustomField');
$fieldvalue = $DB->get_field_sql($sql, $params);
Note this code is written off the top of my head and untested, so it may have some typos in it.
You can use:
$Data = $USER->profile['YourCustomField'];
I finally made it!!!
$sql = 'SELECT DISTINCT u.id AS userid, u.username, uif.shortname, uid.data FROM prefix_user u JOIN prefix_user_info_data as uid on uid.userid = u.id JOIN prefix_user_info_field as uif on uid.fieldid = uif.id JOIN prefix_user_enrolments ue ON ue.userid = u.id JOIN prefix_enrol e
ON e.id = ue.enrolid JOIN prefix_role_assignments ra ON ra.userid = u.id JOIN prefix_context ct ON ct.id = ra.contextid AND
ct.contextlevel = 50 JOIN prefix_course c ON c.id = ct.instanceid AND e.courseid = c.id JOIN prefix_role r ON r.id = ra.roleid
where uif.shortname = \'fieldname\' and u.id ='.$userid;
$records = $DB->get_recordset_sql($sql, array(), $params=null, $limitfrom=0, $limitnum=0);
foreach($records as $record){
$user['fieldname'] = $record->data;
}
custom fields are stored in 'user_info_field' table and data for each field and user are stored in 'user_info_data' table then you should know your fieldid in database and select data based on this id:
$field = $DB->get_record('user_info_field', array('shortname' => 'customfieldshortname'));
$fieldvalues = $DB->get_records('user_info_data',array('fieldid' => $field->id));
i hope my solutions help anybody:
need this lib
require_once($CFG->dirroot.'/user/profile/lib.php');
and now you can use:
profile_load_data($USER);
$USER->profile_field_%your_field_name%
example $USER->profile_field_HaveAnotherGradeBook;

rally_api query for getting defects based on iteration

I am using rally_api gem in Ruby. Could any one suggest me how to write query that gets all the defects under a particular iteration?
require 'rally_api'
headers = RallyAPI::CustomHttpHeader.new()
headers.version = "1.0"
config = {:base_url => "https://rally1.rallydev.com/slm"}
config[:username] = "mvcmxb"
config[:password] = "kjkjk"
config[:workspace] = "Persons"
config[:project] = "Business he does"
config[:headers] = headers
#rally = RallyAPI::RallyRestJson.new(config)
test_query = RallyAPI::RallyQuery.new()
test_query.type = "defect"
test_query.fetch = true
results = #rally.find(test_query)
Here is a code example when defects are queried by iteration and their description is updated. You may either query by iteration name:
query.query_string = "(Iteration.Name = \"i10\")"
or iteration ref:
query.query_string="(Iteration = /iteration/13589769934)"
Names are not unique, but the query is also bound by the project:
rally = RallyAPI::RallyRestJson.new(config)
query = RallyAPI::RallyQuery.new()
query.type = :defect
query.fetch = "Name,FormattedID,Iteration,Description"
query.project = {"_ref" => "https://rally1.rallydev.com/slm/webservice/v2.0/project/12352608219.js" }
query.project_scope_up = false
query.project_scope_down = false
query.order = "Name Asc"
#query.query_string = "(Iteration.Name = \"i10\")"
query.query_string="(Iteration = /iteration/13589769934)"
results = rally.find(query)
results.each do |d|
puts "FormattedID: #{d["FormattedID"]}, Iteration: #{d["Iteration"]["Name"]}"
d.read
fieldUpdates = { "Description" => "bad defect"}
d.update(fieldUpdates)
end

Zend Framework2 how to make Concat and Left Join

Does not work:
$sql = new Sql($this->adapter);
$select = $sql->select();
$select->from('request')
->columns(array('*', new Expression("CONCAT(up1.value,' ',up2.value) as display_name")))
->join(array('up1'=>'user_profile'), "up1.user_id = request.request_user_id AND up1.key = 'user_first_name'", array('up1.value'), 'left')
->join(array('up2'=>'user_profile'), "up2.user_id = request.request_user_id AND up2.key = 'user_last_name'", array('up2.value'), 'left')
;
return $select;
How to make the right?
You specified fields for both joins:
->join(array('up1'=>'user_profile'), "up1.user_id = request.request_user_id AND up1.key = 'user_first_name'", array('up1.value'), 'left')
->join(array('up2'=>'user_profile'), "up2.user_id = request.request_user_id AND up2.key = 'user_last_name'", array('up2.value'), 'left')
When query is converted to actual sql, these fields will be automatically namespaced, so you will get "up1.up1.value" in fields list.
Remove fields references from joins and it should work.
UPD. Right, there's more to it. You can't pass 'user_first_name' as a string value to the "on" parameter of join as it will be interpreted as a column name. So you have to pass an expression:
$select = $sql->select();
$select->from('request')
->columns(array('*', new Expression('CONCAT(up1.value,"#",up2.value) as display_name')));
$expressionString = '? = ? AND ? = ?';
$types = array(Expression::TYPE_IDENTIFIER, Expression::TYPE_IDENTIFIER, Expression::TYPE_IDENTIFIER, Expression::TYPE_VALUE);
$parameters = array('request.user_id', 'up1.user_id', 'up1.key', 'first_name');
$expression1 = new Expression($expressionString, $parameters, $types);
$parameters = array('request.user_id', 'up2.user_id', 'up2.key', 'last_name');
$expression2 = new Expression($expressionString, $parameters, $types);
$select->join(array('up1'=>'user_profile'), $expression1, array('value'), 'left')
->join(array('up2'=>'user_profile'), $expression2, array('value'), 'left');
Y tri to:
$select = $sql->select();
$select->from('request')
->columns(array('*', new Expression('CONCAT(up1.value,"#",up2.value) as display_user_name')));
$expressionString = '? = ? AND ? = ?';
$types = array(Expression::TYPE_IDENTIFIER, Expression::TYPE_IDENTIFIER, Expression::TYPE_IDENTIFIER, Expression::TYPE_VALUE);
$parameters1 = array('request.user_id', 'up1.user_id', 'up1.key', 'user_first_name');
$expression1 = new Expression($expressionString, $parameters1, $types);
$parameters2 = array('request.user_id', 'up2.user_id', 'up2.key', 'user_last_name');
$expression2 = new Expression($expressionString, $parameters2, $types);
$select->join(array('up1'=>'user_profile'), $expression1, array('value'), 'left')
->join(array('up2'=>'user_profile'), $expression2, array('value'), 'left');
does not work, where the error

ActiveRecord: PostgreSQL-specific update with join

The line like this (split into multiple lines for readability):
ValueMapping
.joins(:variation)
.where(
:mapper_id => 1,
:variations => {:name => 'de'}
)
.update_all(:value => 'Beispiel')
produces following SQL statement:
UPDATE value_mappings
SET value = 'Beispiel'
WHERE value_mappings.id IN (
SELECT value_mappings.id
FROM
value_mappings
INNER JOIN variations ON variations.id = value_mappings.variation_id
WHERE
value_mappings.mapper_id = 1
AND variations.name = 'de'
)
But I would like to get more PostgreSQL-specific request:
UPDATE value_mappings
SET value = 'Beispiel'
FROM
variations
WHERE
variations.id = value_mappings.variation_id
AND value_mappings.mapper_id = 1
AND variations.name = 'de'
)
Is it possible with ActiveRecord?

Export products from Mysql Magento

I have some problems with Magento DB, i cannot match the attributes for my custom products export file.
I need help to get attributes, i have finished this script only with this header
But attribute_set, visibility and size_cloth is returning number not name. maybe this can be resolved with a match function.
I need to get more attributes, ex: category_ids, i cannot get this...
color, size_shoes, size_etc.
Please check my script.
'store', '_website', 'attribute_set', 'type', 'sku', 'name', 'description', 'short_description', 'visibility', 'has_option', 'price', 'special_price', 'size_cloth', 'link', 'image'
<?php
//Setup Connection information
$dbhost = 'localhost';
$dbuser = 'user';
$dbpass = 'pass';
//Connect to the database
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die('Error connecting to mysql');
//Point to specific DB
$dbname = 'database_name';
mysql_select_db($dbname);
//Create the Query to get the products
$sql = "SELECT DISTINCT P.attribute_set_id, P.type_id, P.sku, P.has_options, V.value AS Name, T1.value AS ProdDesc, T2.value AS ShortDesc,
T5.value AS visibility, D.value AS Price, S.value AS Special_Price, SIZE_CLOTH.value AS size_cloth, CONCAT('http://www.website.com/', V1.value) AS Link,
CASE
WHEN V2.Value IS NULL
THEN NULL
ELSE CONCAT('http://www.website.com/media/catalog/product', V2.value)
END AS Image
FROM catalog_product_entity AS P INNER JOIN
catalog_product_entity_varchar AS V ON P.entity_id = V.entity_id AND V.attribute_id = 60 LEFT JOIN
catalog_product_entity_varchar AS V1 ON P.entity_id = V1.entity_id AND V1.attribute_id = 87 LEFT JOIN
catalog_product_entity_varchar AS V2 ON P.entity_id = V2.entity_id AND V2.attribute_id = 74 LEFT JOIN
catalog_product_entity_text AS T1 ON P.entity_id = T1.entity_id AND T1.attribute_id = 61 LEFT JOIN
catalog_product_entity_text AS T2 ON P.entity_id = T2.entity_id AND T2.attribute_id = 62 LEFT JOIN
catalog_product_entity_int AS T5 ON P.entity_id = T5.entity_id AND T5.attribute_id = 91 LEFT JOIN
catalog_product_entity_decimal AS D ON P.entity_id = D.entity_id AND D.attribute_id = 64 LEFT JOIN
catalog_product_entity_int AS SIZE_CLOTH ON P.entity_id = SIZE_CLOTH.entity_id AND SIZE_CLOTH.attribute_id = 122 LEFT JOIN
catalog_product_entity_decimal AS S ON P.entity_id = S.entity_id AND S.attribute_id = 65";
//Run the query
$query = mysql_query($sql);
//But after this i will prepare the csv file for export.
$file = fopen('products_export.csv', 'w');
// Custom header for csv file
$header = array('store', '_website', 'attribute_set', 'type', 'sku', 'name', 'description', 'short_description', 'visibility', 'has_option', 'price', 'special_price', 'size_cloth', 'link', 'image');
// this will arrange all data by comma
fputcsv($file, $header, ',', '"');
//Loop through and print each products info
while($row = mysql_fetch_array($query))
{
$item = array();
$value1 = ("admin");
$value2 = ("base");
$value3 = ($row['attribute_set_id']);
$value4 = ($row['type_id']);
$value5 = ($row['sku']);
$value6 = ($row['Name']);
$value7 = ($row['ProdDesc']);
$value8 = ($row['ShortDesc']);
$value9 = ($row['visibility']);
$value10 = ($row['has_options']);
$value11 = ($row['Price']);
$value111 = ($row['Special_Price']);
$value112 = ($row['size_cloth']);
$value12 = ($row['Link']);
//$value13 = ($row['Image']);
$item[] = $value1;
$item[] = $value2;
$item[] = $value3;
$item[] = $value4;
$item[] = $value5;
$item[] = $value6;
$item[] = $value7;
$item[] = $value8;
$item[] = $value9;
$item[] = $value10;
$item[] = $value11;
$item[] = $value111;
$item[] = $value112;
$item[] = $value12;
// put all data in csv file
fputcsv($file, $item, ',', '"');
}
{
// close csv file and finish.
fclose($file);
}
?>
I think that the best solution will be the data.
Example:
if ( $value112 == need to array the list of numbers, 1, 2, 3, 4, 5) {
echo "S, M, L, XL";
} else {
echo " ";
}
But i dont know how to insert this to my array items...
Interacting directly with Magento's DB wouldn't be my 1st choice.
Alternatives that actually work:
Use Magento Import/Export module
Use Magento Dataflow Import/Export
Use Magento's product collection to generate the export file
http://www.magentocommerce.com/knowledge-base/entry/magento-for-dev-part-8-varien-data-collections

Resources