Export products from Mysql Magento - parsing

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

Related

sum total column of a table

I have a this query in rails:
def self.nominas(params)
query = select("payroll_employees.*,payroll_employees.total, concat(e.name, ' ', e.surname, ' ', e.other_surname) as nombre_empleado, ec.code as contrato, CASE WHEN payroll_employees.is_wage_integral = TRUE THEN 'Si' WHEN payroll_employees.is_wage_integral = FALSE THEN 'No' END AS salario_es_integral, pc.integral_wage AS p_c_integral_wage")
query = query.joins("inner join payrolls p on (p.id = payroll_employees.payroll_id) inner join employee_contracts ec on (ec.id = payroll_employees.employee_contract_id) inner join employees e on (e.id = ec.employee_id) inner join payroll_companies pc on (pc.company_id = p.company_id) ")
query = query.where('p.id = :nomina', {nomina: params[:id] })
query = query.group(:id, 'e.name', 'e.surname', 'e.other_surname', 'ec.code', 'pc.integral_wage')
query = query.having("(lower(concat(e.name, ' ', e.surname, ' ', e.other_surname)) LIKE :campo_busqueda or :campo_busqueda = '') OR (lower(ec.code) LIKE :campo_busqueda or :campo_busqueda = '')", {campo_busqueda: "%#{params[:buscador].try(:downcase)}%"})
query = query.order('nombre_empleado')
end
in which I have a column "total", and I need to do the summation of the column "total" of all the records that the query brings me, my problem comes at the time of doing this:
#payroll_employees = PayrollEmployee.nominas(params)####
#sum_total = #payroll_employees.sum(:total)
it brings me something like this:
{[345, "Angel", "BONILLA", "MONTAÑO", "2010", true]=>0.106215575500000000000001e7, [079, "Bill f", "CABRERA", "RICO", "1846", true]=>0.1330346e7, ...
it seems to me that it is because my query has a group. Is it possible to do the summation and get a single number instead of a grouped array of totals?
Try to use sum function on total sum(payroll_employees.total)
def self.nominas(params)
query = select("payroll_employees.*,sum(payroll_employees.total) as total, concat(e.name, ' ', e.surname, ' ', e.other_surname) as nombre_empleado, ec.code as contrato, CASE WHEN payroll_employees.is_wage_integral = TRUE THEN 'Si' WHEN payroll_employees.is_wage_integral = FALSE THEN 'No' END AS salario_es_integral, pc.integral_wage AS p_c_integral_wage")
query = query.joins("inner join payrolls p on (p.id = payroll_employees.payroll_id) inner join employee_contracts ec on (ec.id = payroll_employees.employee_contract_id) inner join employees e on (e.id = ec.employee_id) inner join payroll_companies pc on (pc.company_id = p.company_id) ")
query = query.where('p.id = :nomina', {nomina: params[:id] })
query = query.group(:id, 'e.name', 'e.surname', 'e.other_surname', 'ec.code', 'pc.integral_wage')
query = query.having("(lower(concat(e.name, ' ', e.surname, ' ', e.other_surname)) LIKE :campo_busqueda or :campo_busqueda = '') OR (lower(ec.code) LIKE :campo_busqueda or :campo_busqueda = '')", {campo_busqueda: "%#{params[:buscador].try(:downcase)}%"})
query = query.order('nombre_empleado')
end

LINQ query with sub-query list

The query below should be selecting a list of information from different tables, and should also bring back a list of VehicleNames that are related to each ID coming back from db.Reservations
The query brings back data, but the list of VehicleNames only has one record, and it should bring back anywhere up to 5 records, depending on how many vehicles were reserved for that specific instance. I have tried adding a foreach to the VehicleName line in the select but I don't think I am using it right. Does anyone have any ideas as to how I can retrieve the list of values I need?
var query = from r in db.Reservations
let e = db.Employees.Where(x => r.RequestorID == x.ColleagueID).FirstOrDefault()
let rtv = db.ReservationToVehicle.Where(x => r.ID == x.ReservationID).FirstOrDefault()
let rs = db.ReservationStatus.Where(x => r.ID == x.ReservationID).FirstOrDefault()
let rst = db.ReservationStatusTypes.Where(x => rs.ReservationStatusTypeID == x.ID).FirstOrDefault()
select new
{
StartDate = r.StartDate,
EndDate = r.EndDate,
Destination = r.Destination,
PurposeOfTrip = r.PurposeOfTrip,
TransportingStudents = r.TransportStudentsFG,
EmployeeName = e.FirstName + " " + e.LastName,
ApprovalStatus = rst.StatusType,
ThemeColor = r.ThemeColor,
VehicleName = (from v in db.Vehicles
where v.ID == rtv.VehicleID
select v.VehicleName).ToList()
};
EDIT: Updated query, still uncertain how to get back list of Vehicles that have been reserved
var query = from r in db.Reservations
let e = db.Employees.Where(x => r.RequestorID == x.ColleagueID).FirstOrDefault()
let rtv = db.ReservationToVehicle.Where(x => r.ID == x.ReservationID).Select(y => y.VehicleID).ToList()
let rs = db.ReservationStatus.Where(x => r.ID == x.ReservationID).FirstOrDefault()
let rst = db.ReservationStatusTypes.Where(x => rs.ReservationStatusTypeID == x.ID).FirstOrDefault()
select new
{
StartDate = r.StartDate,
EndDate = r.EndDate,
Destination = r.Destination,
PurposeOfTrip = r.PurposeOfTrip,
TransportingStudents = r.TransportStudentsFG,
EmployeeName = e.FirstName + " " + e.LastName,
ApprovalStatus = rst.StatusType,
ThemeColor = r.ThemeColor,
VehicleName = (from v in db.Vehicles
where v.ID == rtv.VehicleID
select v.VehicleName).ToList()
};
rtv.VehicleID does not exist anymore, now rtv comes back with a list of IDs....I need to be able to get into that list to find where v.ID is IN the rtv list
Looks like people already told you this in the comments, but here's the code that ought to work:
var query = from r in db.Reservations
let e = db.Employees.Where(x => r.RequestorID == x.ColleagueID).FirstOrDefault()
let rtv = db.ReservationToVehicle.Where(x => r.ID == x.ReservationID).Select(y => y.VehicleID)
let rs = db.ReservationStatus.Where(x => r.ID == x.ReservationID).FirstOrDefault()
let rst = db.ReservationStatusTypes.Where(x => rs.ReservationStatusTypeID == x.ID).FirstOrDefault()
select new
{
StartDate = r.StartDate,
EndDate = r.EndDate,
Destination = r.Destination,
PurposeOfTrip = r.PurposeOfTrip,
TransportingStudents = r.TransportStudentsFG,
EmployeeName = e.FirstName + " " + e.LastName,
ApprovalStatus = rst.StatusType,
ThemeColor = r.ThemeColor,
VehicleName = (from v in db.Vehicles
where rtv.Contains(v.ID)
select v.VehicleName).ToList()
};
I'd also recommend looking at setting up navigation properties between your entities. Your query could be much simpler, something like this:
var query = from r in db.Reservations
let e = r.Requestor
let rs = r.ReservationStatus
let rst = rs.ReservationStatusType
select new
{
StartDate = r.StartDate,
EndDate = r.EndDate,
Destination = r.Destination,
PurposeOfTrip = r.PurposeOfTrip,
TransportingStudents = r.TransportStudentsFG,
EmployeeName = e.FirstName + " " + e.LastName,
ApprovalStatus = rst.StatusType,
ThemeColor = r.ThemeColor,
VehicleName = r.Vehicles.Select(v => v.VehicleName).ToList()
};

Can I do multi mapping with redbeanphp?

can I load an object and it's relations with just a query?
so, if I have
select * from parent inner join child on parent.id = child.parent_id
could I get and array of parent beans with is ownChild informed just from that query?
something like multi mapping in dapper:
https://github.com/StackExchange/dapper-dot-net#multi-mapping
Yes, you can. This is documented as "Find Multiple" in the RedBean manual.
Example with you query:
<?php
require 'rb.php'; // 4.2.4, for the record
R::setup();
// --- Fixtures ---
$parent = R::dispense('parent');
$parent->name = "Foo";
$child = R::dispense('child');
$child->name = "Bar";
$childId = R::store($child);
$parent->ownChildList[] = $child;
$parentId = R::store($parent);
// --- Multi Mapping ---
$beans = R::findMulti( 'parent,child', '
SELECT parent.*, child.* FROM parent
INNER JOIN child ON parent.id = child.parent_id
WHERE parent.id = ?
', [$parentId], [
[
'a' => 'parent',
'b' => 'child',
'matcher' => function($parent, $c) {
return $parent->id == $c->parent_id;
},
'do' => function($p, $c) {
$p->noLoad()->ownChildList[$c->id] = $c;
}
]
]);
// Ensure we don't get lazily loaded results...
R::nuke();
echo $beans['parent'][$parentId]->ownChildList[$childId]->name, "\n";
Outputs:
Bar
We win.
$parent = R::dispense('parent');
$parent->name = "darth";
$child = R::dispense('child');
$child->name = "Luke";
$parent->ownChildList[] = $child;
$parent_id = R::store($parent);
// now fetch
$parent = R::load('parent' $parent_id);
echo "$parent has the following kids:";
foreach ($parent->ownChildList as $child){
echo $child->name . ", ";
}

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;

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

Resources