How to export lines db table to csv file with phpspreadsheet - laravel-5

In laravel 5.7 with "phpoffice/phpspreadsheet": "^1.6" app I need to export lines db table to csv file, like :
$dataLines= [
['field1'=>'000000000', 'field2'=>1111111111111],
['field1'=>'11000000000', 'field2'=>221111111111111],
['field1'=>'31000000000', 'field2'=>321111111111111],
];
$spreadsheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet();
$writer = new \PhpOffice\PhpSpreadsheet\Writer\Csv($spreadsheet);
$writer->save('/path/12345.csv');
But with code above I have empty file and I did find not way to write content of $dataLines array.
Also as I need to write 1 row as fields name and next rows from db,
have I to prepare 1st row with fields name and rest rows (only values without fields name) manually ?
Are there some mothods to make it automatically ?

Related

API Platform Raw Query

Anyone knows how to get raw query working in API platform controller/handler,
I am trying to do something like,
$temp_table_query = $this->entityManager->createQuery('CREATE TEMPORARY TABLE {temp_validation_code_table} LIKE {validation_code}');
$qb = $this->entityManager->createQueryBuilder();
$qb = $qb->select('LOAD DATA LOCAL INFILE {$file} INTO TABLE {temp_validation_code_table} (code) SET offer = {$offer}');
Trying to load file data into a temporary table for validation before inserting in actual db table
This needs to be done using the underlying Doctrine PDO abstraction (Doctrine\DBAL\Connection), which can be obtained via $entityManager->getConnection();.
First however, you need to make sure that the underlying PDO connection is configured to allow LOAD LOCAL INFILE (by setting PDO::MYSQL_ATTR_LOCAL_INFILE to true). This can be done by providing the following Doctrine configuration (in doctrine.yaml):
doctrine:
dbal:
#...
options:
1001: true # This sets PDO::MYSQL_ATTR_LOCAL_INFILE to true
Be warned that allowing LOAD LOCAL INFILE possibly introduces security vulnurabilities.
Given I have a file named example.csv containing the following CSV data:
1,"foo"
2,"bar"
3,"baz"
I can then insert this data using the Doctrine PDO abstraction:
<?php
//...
// Local path to the CSV file
$file = __DIR__ . '/example.csv';
// Obtaining the PDO connection
$connection = $this->entityManager->getConnection();
// Creating the temporary table
$connection->executeStatement('CREATE TEMPORARY TABLE foobar (id int AUTO_INCREMENT PRIMARY KEY , name VARCHAR(256) NOT NULL)');
// Inserting the CSV data
$connection->executeStatement("LOAD DATA LOCAL INFILE '$file' INTO TABLE foobar FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n'");

Perform Incremental Load On Qlik Sense

New to Qlik Sense.
I would like to perform incremental insert, update and delete. Through research i managed to write this script
//This fetches deleted records
SELECT `sale_detail_auto_id`
FROM `iprocure_ods.deleted_records` as dr
INNER JOIN `iprocure_ods.saledetail` sd ON sd.sale_detail_auto_id = dr.identifier AND dr.type = 2
WHERE dr.delete_date > TIMESTAMP('$(vSaleTransactionsRunTime)');
//This fetches new and updated records
[sale_transactions]:
SELECT *
FROM `iprocure_edw.sale_transactions`
WHERE `server_update_date` > TIMESTAMP('$(vSaleTransactionsRunTime)');
Concatenate([sale_transactions])
LOAD *
FROM [lib://qlikPath/saletransactions.qvd] (qvd) Where Not Exists(`sale_detail_auto_id`);
//This part updates runtime dates
MaxUpdateDate:
LOAD Timestamp(MAX(`server_update_date`), '$(TimestampFormat)') As maxServerUpdateDate
FROM [lib://qlikPath/saletransactions.qvd] (qvd);
Let vSaleTransactionsRunTime = peek('maxServerUpdateDate', 0, MaxUpdateDate);
DROP Table MaxUpdateDate;
New and update records works fine. The problem is with the deleted records are replaced with empty column except sale_detail_auto_id column.
How can i fetch data from saletransactions.qvd that are not in deleted records?
In first SELECT you select sale_detail_auto_id fields which is also exists under the same field name in new and updated records, so then you see deleted ids together with new ones. You need to rename that column to avoid conflict.
Please use AS, for example:
sale_detail_auto_id` AS `deleted_sale_detail_auto_id`
and then in EXISTS use that field:
Where Not Exists(deleted_sale_detail_auto_id, sale_detail_auto_id);
UPDATED:
Additionally I think it doesn't make sense to store deleted ids in data model so you can name that table:
[TEMP_deleted_ids]
SELECT sale_detail_auto_id` AS `deleted_sale_detail_auto_id`
and then in the end of the script remove it:
DROP Table [TEMP_deleted_ids];

Cannot Insert header using the before_import override function in django-import-export

I'm using django-import-export to upload csv files through django admin. I have the ability to override the before_import function to add functionality before the import. I have a csv file with no headers, and the actual data starts on line one. I need to add a header, or insert a row before my csv file is uploaded, so that it can be read properly.
class UpdateResource(resources.ModelResource):
def before_import(self, dataset, using_transactions, dry_run, **kwargs):
dataset.header = ['sku', 'quantity']
class Meta:
model = Upload
import_id_fields = ('sku',)
This code changes the value of the first row of my csv file to sku,quantity, but I need to insert one above that value, not replace it. Alternatively, if there is an option to ignore headers and just map the values to my model from left to right or something, that would be great too.
My fix was to store the first row as a variable, create the desired header and append the first row to end of file.
class UpdateResource(resources.ModelResource):
def before_import(self, dataset, using_transactions, dry_run, **kwargs):
first_row = dataset.header
dataset.header = ['sku', 'quantity']
dataset.append(first_row)

Add row name to cde table component

I have a table component in my Pentaho CDE dashboard and data source is sql. I want to make a table like this
enter image description here
I need to add a column as row names for this table and a row on the top of column header.
I found a method here:
Table Component SubColumns
to add header, but how can i add a column before other columns?
I think a clever way to do the extra column part would be by modifying the query result object after it's retrieved from the query, so that you add the columns and rows as needed. You can do this in the PostFetch callback function, which takes the query result object as first argument:
function yourTableComponentPostFetch(queryResult) {
// Let's say you have an array of row names
var rowNames = ['Title1', 'Title2', ... ];
// Add a "title" column for every row
queryResult.resultset.forEach(function (row, idx) {
// Push it at the beginning of the row array
row.unshift(rowNames[idx]);
});
// Change metadata description of columns to reflect this new structure
queryResult.metadata.unshift({
colName: 'Title',
colIndex: -1, // this makes sense when reindexing columns below ;)
colType: 'String'
});
// One last re-indexing of metadata column descriptions
queryResult.metadata.forEach(function (column, idx) {
// The title added column will be 0, and the rest will rearrange
column.colIndex++;
});
}
The only tricky part is the part about modifying the metadata since you are effectively changing the structure of the dataset, and making sure the queryResult object is updated in-place instead of just changing its reference (queryResult = myNewQueryResult would not work), but the code I proposed should do the trick.

Yii2- Not able to insert/delete rows using console controller

I am trying to implement email queue in my project which uses CRON.
I am adding a row in one table and deleting a row from another table using CreateCommand of yii2. Database is mysql.
The entire code is executed and it does not display any error. But the rows are not added or deleted from the database.
I have also tried doing the same thing using ActiveRecords but it does not work. Hence I have used CreateCommand.
Below is the code which I am using -
$connection = \Yii::$app->db;
$result = $connection->createCommand()->insert('email_queue_completed', $arrInsert)->execute();
if(!empty($result))
{
$connection->createCommand()
->delete('email_queue', ['id' => $mail->id])
->execute();
}
$arrInsert is an array containing key value pairs of values to be inserted.

Resources