code igniter active records - help streamlining process - mysql

I am currently using the below code to get a list of uuid's then split them into groups of 1000, then insert those groups into the database.
This works fine except this has to work on at times, over a million uuid's
The issue is this uses a massive amount of memory, so I need help to streamline this process to use less memory...
public function send_daily_email($dealId) {
set_time_limit(0);
$deal = $this->ci->deal->get($dealId);
if ($deal == false)
throw new exception('Unknown Deal Specified');
$users = $this->db->select('uuid')->from('userRegionLink')->where('regionId', $deal->region)->get();
if ($users->num_rows() == 0)
throw new exception('No users in region');
$message = $this->ci->load->view('emails/daily', array('name' => $deal->title, 'content' => $deal->snippet), true);
$uuids = array();
foreach ($users->result() as $u)
$uuids[] = $u->uuid;
$uuids = array_chunk($uuids, 1000);
$sendId = 0;
foreach ($uuids as $batch) {
$count = count($batch);
$this->db->set('dealId', $dealId)->set('content', $message)->set('regionId', $deal->region)->set('recipients', $count)->set('created', 'NOW()', false)->set('status', 'Creating');
if ($sendId === 0) {
$this->db->insert('dealEmailParent');
$sendId = $this->db->insert_id();
$this->db->set('sendId', $sendId)->where('id', $sendId)->update('dealEmailParent');
}
else
$this->db->set('sendId', $sendId)->insert('dealEmailParent');
$insert = array();
foreach ($batch as $uuid)
$insert[] = array('parentId' => $sendId, 'uuid' => $uuid);
$this->db->insert_batch('dealEmailChild', $insert);
}
}

I hate to say this, but from what I know about CodeIgniter, the only way it knows to fetch results is to fetch the entire resultset at once, even if you only need one row, or even if you want to fetch a row at a time and do some processing. It doesn't operate with cursors as the native mysql(i)/PDO functionality does.
For this large a dataset, I'd suggest sticking to the native PHP database functions and foregoing CodeIgniter's active record database classes.

This reworking can insert 1,000,000 "users" in under a minute without any memory limits :)
public function create_daily_email($dealId)
{
$time_start = microtime(true);
set_time_limit(0);
$deal = $this->ci->deal->get($dealId);
if ($deal == false)
throw new exception('Unknown Deal Specified');
$message = $this->ci->load->view('emails/daily', array('name' => $deal->title, 'content' => $deal->snippet), true);
$start = 0;
$end = 50000;
$q = $this->db->select('uuid')->from('userRegionLink')->where('regionId', $deal->region)->limit($end, $start)->get();
$sendId = 0;
while ($q->num_rows() != 0) {
//do stuff
$uuids = array();
foreach ($q->result() as $u)
$uuids[] = $u->uuid;
$uuids = array_chunk($uuids, 1000);
foreach ($uuids as $batch) {
$count = count($batch);
$this->db->set('dealId', $dealId)->set('content', $message)->set('regionId', $deal->region)->set('recipients', $count)->set('created', 'NOW()', false)->set('status', 'Creating');
if ($sendId === 0) {
$this->db->insert('dealEmailParent');
$sendId = $this->db->insert_id();
$this->db->set('sendId', $sendId)->where('id', $sendId)->update('dealEmailParent');
$parentId = $sendId;
}
else {
$this->db->set('sendId', $sendId)->insert('dealEmailParent');
$parentId = $this->db->insert_id();
}
$insert = array();
foreach ($batch as $uuid) {
$insert[] = array(
'parentId' => $parentId,
'uuid' => $uuid
);
}
$this->db->insert_batch('dealEmailChild', $insert);
}
//stop stuff
unset($q);
unset($uuids);
unset($insert);
$start = $start + $end;
$q = $this->db->select('uuid')->from('userRegionLink')->where('regionId', $deal->region)->limit($end, $start)->get();
}
$this->db->set('status', 'Pending')->where('sendId', $sendId)->update('dealEmailParent');
$time_end = microtime(true);
$time = $time_end - $time_start;
die("Did nothing in $time seconds");
}

Related

Laravel Query builder returns a row from join instead of multiples row

I am getting fewer results than expected , The data returns 57 rows instead of 70 rows. I need help to get all the rows please, I returned a collection using get(), and i joined it with foreach() to include the rows to existing query, Please any help?
public function getempAttendance(Request $request) {
$id = $request->id;
$department_id = $request->department_id;
if($id !== null){ //return based on type;
$emp = AsEmployee::where('id','=',$id)->orderBy('id','ASC')->get();
} else if($department_id != null){
$emp = AsEmployee::where('department_id','=',$department_id)->orderBy('id','ASC')->get();
} else{ //return all if nothing is given;
$emp = AsEmployee::orderBy('id','DESC')->get();
}
foreach($emp as $emp_data){
$department = AsDepartment::where('id','=',$emp_data->department_id)->get(['department'])->first();
if($department !== NULL){
$emp_data->department_name = $department->department;
}else{
$emp_data->department_name = '';
}
$position = AsEmployeePosition::where('id','=',$emp_data->position_id)->get(['position_name'])->first();
if($position !== NULL){
$emp_data->position_name = $position->position_name;
}else{
$emp_data->position_name = '';
}
$attendances = AsAttendanceLog::select('CHECK_IN_TIME','CHECK_OUT_TIME')
->where('EMPLOYEE_ID','=',$emp_data->employee_id)->get();
if($attendances !== NULL){
foreach($attendances as $attendance){
$emp_data->CHECK_IN_TIME = Carbon::parse($attendance->CHECK_IN_TIME)->toTimeString();
$emp_data->CHECK_OUT_TIME = Carbon::parse($attendance->CHECK_OUT_TIME)->toTimeString();
$emp_data->Date = Carbon::parse($attendance->CHECK_IN_TIME)->format('Y-m-d');
$hours = Carbon::parse($attendance->CHECK_OUT_TIME)->diffInSeconds(Carbon::parse($attendance->CHECK_IN_TIME));
$emp_data->Hours = gmdate('H:i', $hours);
}
}else {
$emp_data->CHECK_IN_TIME = '';
$emp_data->CHECK_OUT_TIME = '';
}
}
return $this->sendResponse($emp);
}
Meanwhile, this works but i need the query builder format to allow me use Carbon and do some operations
$attendanceData = DB::table('as_tbl_employee_master AS emp')
->leftJoin('as_tbl_department AS dept','emp.department_id','=', 'dept.id')
->leftJoin('as_tbl_employee_position AS pos', 'emp.position_id', '=', 'pos.id')
->leftJoin('as_tbl_emp_attendance_daily_log AS att', 'att.EMPLOYEE_ID', '=', 'emp.employee_id')
->select('emp.id','emp.employee_id','emp.english_name','dept.department','pos.position_name','att.CHECK_IN_TIME','att.CHECK_OUT_TIME')
->orderby('att.CHECK_IN_TIME', 'DESC')
->get();
return $this->sendResponse($attendanceData);
if the user's table is "one to many" to as_tbl_emp_attendance_daily_log's table.
You should select('as_tbl_emp_attendance_daily_log') first then left join to user's table.
I assumed that you want to show all as_tbl_emp_attendance_daily_log's row. If that right your Query builder should be like this.
$attendanceData = DB::table('as_tbl_emp_attendance_daily_log AS att')
->leftJoin('as_tbl_employee_master AS emp', 'att.EMPLOYEE_ID', '=', 'emp.employee_id')
->leftJoin('as_tbl_department AS dept','emp.department_id','=', 'dept.id')
->leftJoin('as_tbl_employee_position AS pos', 'emp.position_id', '=', 'pos.id')
->select('emp.id','emp.employee_id','emp.english_name','dept.department','pos.position_name','att.CHECK_IN_TIME','att.CHECK_OUT_TIME')
->orderby('att.CHECK_IN_TIME', 'DESC')
->get();
UPDATE
If you want to add the custom attribute to the model you should define it at the model.
This is for reference:
https://laravel.com/docs/5.1/eloquent-serialization#appending-values-to-json
But you can directly change the attribute if you change the collections to the array first.
I don't have your table, so I cannot test it, I just changed it based on your snippet.
public function getempAttendance(Request $request) {
$id = $request->id;
$department_id = $request->department_id;
if($id !== null){ //return based on type;
$emp = AsEmployee::where('id','=',$id)->orderBy('id','ASC')->toArray();
} else if($department_id != null){
$emp = AsEmployee::where('department_id','=',$department_id)->orderBy('id','ASC')->toArray();
} else{ //return all if nothing is given;
$emp = AsEmployee::orderBy('id','DESC')->toArray();
}
for($i=0;$i < count($emp); $i++){
$department = AsDepartment::where('id','=',$emp[$i]->department_id)->get(['department'])->first();
if($department !== NULL){
$emp[$i]['department_name'] = $department->department;
}else{
$emp[$i]['department_name'] = '';
}
$position = AsEmployeePosition::where('id','=',$emp[$i]['position_id'])->get(['position_name'])->first();
if($position !== NULL){
$emp[$i]['position_name'] = $position->position_name;
}else{
$emp[$i]['position_name'] = '';
}
$attendances = AsAttendanceLog::select('CHECK_IN_TIME','CHECK_OUT_TIME')
->where('EMPLOYEE_ID','=',$emp[$i]['employee_id'])->toArray();
$attendances_data = [];
if($attendances !== NULL){
for($j=0;$j<count($attendances);$j++){
$data = [];
$data['CHECK_IN_TIME'] = Carbon::parse($attendance['i']['CHECK_IN_TIME'])->toTimeString();
$data['CHECK_OUT_TIME'] = Carbon::parse($attendance['i']['CHECK_OUT_TIME'])->toTimeString();
$data['Date'] = Carbon::parse($attendance['i']['CHECK_IN_TIME'])->format('Y-m-d');
$hours = Carbon::parse($attendance['i']['CHECK_OUT_TIME'])->diffInSeconds(Carbon::parse($attendance['i']['CHECK_IN_TIME']));
$data['Hours'] = gmdate('H:i', $hours);
$attendances_data[] = $data
}
$emp[$i]['attendances'] = $attendances_data;
}else {
$emp[$i]['attendances'] = [];
$emp[$i]['attendances'] = [];
}
}
return $this->sendResponse($emp);
}
UPDATE
If you want to multiple row, I think you should try this:
$attendanceData = DB::table('as_tbl_emp_attendance_daily_log AS att')
->leftJoin('as_tbl_employee_master AS emp', 'att.EMPLOYEE_ID', '=', 'emp.employee_id')
->leftJoin('as_tbl_department AS dept','emp.department_id','=', 'dept.id')
->leftJoin('as_tbl_employee_position AS pos', 'emp.position_id', '=', 'pos.id')
->select('emp.id','emp.employee_id','emp.english_name','dept.department','pos.position_name','att.CHECK_IN_TIME','att.CHECK_OUT_TIME')
->orderby('att.CHECK_IN_TIME', 'DESC')
->toArray();
for($i=0;$i<count($attendaceData);$i++)
{
$attendaceData[$i]['CUSTOM VARIABLE'] = 'NEW VALUE CUSTOM VARIABLE AT HERE';
}
return $this->sendResponse($emp);

Yii2 ActiveRecord add a new record with unique text field

I am using Yii2 and ActiveRecord. I have a field called "code" and for each record, it is meant to have a unique value like this: "REC0001", "REC0002", "REC0003" in a sequencial manner.
All works and I can generate a record code as described. However if I refresh my page request fast in a multiple manner (trying to test multiple requests at the same time in a very raw manner hehe), then some of the records end up with the same record code. In other words I found "REC007" a few times.
I generate the code looking at the last code and increase it by one, then I do a while foundFlag == true by checking to see if it already exists in the database.
I am suspecting there is a delay in writing to the database and hence it assumes that it is not there.
Here is a portion of the code:
static function createCode($rec){
if ($rec->code){
return $rec->code;
}
if ($rec->id){ // find it by id if one passed and record exists
$tmpRec = $rec->find()
->where([
'id' => $rec->id,
])
->one();
if ($tmpRec && $tmpRec->code){
return $tmpRec->code;
}
}
$prefix = 'REC';
if (!$prefix){
$prefix = 'REC';
}
$maxDecimals = 12;
$codeLength = $maxDecimals+strlen($prefix);
$query = $rec->find();
$query = $query->where([
'archived' => '0'
]);
// look under an organization if it exists in the model and there is one
if ($rec->hasField('organization_id') && $organization_id){
$query = addQueryWhere($query, [
'organization_id' => $organization_id,
]);
}
$query = addQueryWhere($query, [
'LENGTH(code)' => $codeLength*1,
]);
$query = $query->orderBy('code desc');
$lastRec = $query->one();
$tmpNumber = 0;
if ($lastRec && $lastRec->id){
// check what it returns
$tmpNumber = str_replace($prefix, '', $lastRec->code);
}
$tmpNumber++;
$leftDecimals = $maxDecimals - strlen($tmpNumber.'');
for ($k=0; $k <= $leftDecimals-1 ; $k++){
$tmpNumber = '0'. $tmpNumber;
}
$ret = $prefix . $tmpNumber;
return $ret;
}
public function generateCode($rec){
$foundFlag = true;
$break = 1000; // safe break point - no continuous loop
$cnt = 0;
$code = static::createCode($rec);
while ($foundFlag === true || $cnt < $break){
$tmpRec = $rec->find()
->where([
'code' => $code,
])
->one();
if (!$tmpRec->id){
$foundFlag = false;
break;
}
$time = getCurrentTimestamp();
$code = static::createCode($rec);
$cnt++;
}
$ret = $code;
return $ret;
}
So I simply call: $this->code = $this->generateCode();
Like I said it does work in generating the code, but it creates duplicates when it shouldn't!
Thank you for your assistance.

Get data into array using mysql prepared statements

I'm trying to get the function below to return an array of user_ids. Here is the function in php.
function users_following($follower_id)
{
include "dbconn.php";
$stmt = mysqli_prepare($con, "SELECT user_id FROM follo WHERE follower_id = ?");
mysqli_stmt_bind_param($stmt, "i", $follower_id);
mysqli_stmt_execute($stmt);
mysqli_stmt_bind_result($stmt, $following_user_id);
$count = 0;
$user_array = array();
while (mysqli_stmt_fetch($stmt) ) {
$user_array[] = $following_user_id;
$count = $count + 1;
}
mysqli_stmt_close($stmt);
if ($count > 0)
{
return $user_array;
} else {
return false;
}
}
The problem is that the above function just returns the output: 'Array' (without quotes), when I tested with the code below, not the list of user_ids.
$userid_array = users_following($_SESSION["user_id"]);
echo $userid_array;
Can anyone please help me out? If you need additional details, just comment below and I will try to clarify.

Memory Efficient reading + writing of .csv and .xlsx data

I am looking to implement memory efficient reading and writing of .xlsx/.csv files.
I am currently using phpExcel and am having trouble with large files. (Known issue)
I found the following library for reading: https://github.com/nuovo/spreadsheet-reader
This seems that it will work.
For writing files currently I create an array and then loop the array to output the file. Is this why it uses a lot of memory? What is the best way to not use a lot of memory when writing csv/xlsx when getting data from mysql database using code igniter?
This is what I do now:
/* added for excel expert */
function excel_export() {
$data = $this->Customer->get_all($this->Customer->count_all())->result_object();
$this->load->helper('report');
$rows = array();
$header_row = $this->_excel_get_header_row();
$header_row[] = lang('customers_customer_id');
$rows[] = $header_row;
foreach ($data as $r) {
$row = array(
$r->first_name,
$r->last_name,
$r->email,
$r->phone_number,
$r->address_1,
$r->address_2,
$r->city,
$r->state,
$r->zip,
$r->country,
$r->comments,
$r->account_number,
$r->taxable ? 'y' : '',
$r->company_name,
$r->person_id
);
$rows[] = $row;
}
$content = array_to_spreadsheet($rows);
force_download('customers_export.'.($this->config->item('spreadsheet_format') == 'XLSX' ? 'xlsx' : 'csv'), $content);
exit;
}
function array_to_spreadsheet($arr)
{
$CI =& get_instance();
$objPHPExcel = new PHPExcel();
//Default all cells to text
$objPHPExcel->getDefaultStyle()->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_TEXT);
for($k = 0;$k < count($arr);$k++)
{
for($j = 0;$j < count($arr[$k]); $j++)
{
$objPHPExcel->getActiveSheet()->setCellValueExplicitByColumnAndRow($j, $k+1, $arr[$k][$j]);
}
}
if ($CI->config->item('spreadsheet_format') == 'XLSX')
{
$objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
}
else
{
$objWriter = new PHPExcel_Writer_CSV($objPHPExcel);
}
ob_start();
$objWriter->save('php://output');
$excelOutput = ob_get_clean();
return $excelOutput;
}

codeigniter - convert html to pdf

I have a little problem. I have html page and I want to convert to pdf. My index page has a list that will get to the database and click on "Download PDF", I put this list in a PDF file.
My controller:
<?php
class pdf_c extends CI_Controller{
function __construct() {
parent::__construct();
$this->load->helper(array('url', 'mediatutorialpdf'));
}
function index($download_pdf = ''){
$ret = '';
$ID = 1;
$pdf_filename = 'user_info_'.$ID.'.pdf';
$link_download = ($download_pdf == TRUE)?'':anchor(base_url().'index.php/true', 'Download PDF');
$query = $this->db->query("SELECT * FROM `ci_pdf_user` WHERE `id` = '{$ID}' LIMIT 1");
if($query->num_rows() > 0)
{
$user_info = $query->row_array();
}
$data_header = array(
'title' => 'Convert codeigniter to pdf'
);
$data_userinfo = array(
'user_info' => $user_info,
'link_download' => $link_download
);
$header = $this->load->view('header',$data_header, true);
$user_info = $this->load->view('user_table', $data_userinfo, true);
$footer = $this->load->view('footer','', true);
$output = $header.$user_info.$footer;
if($download_pdf == TRUE)
{
generate_pdf($output, $pdf_filename);
}
else
{
echo $output;
}
}
}
?>
The problem is when I click the button "Download PDF" should redirect me to the function index () and get the $ download_pdf = true. And so called generate_pdf function () that will generate the PDF.
I think the problem is in the variable $ link_download, but can not solve the problem.
Thanks
I think that you could try with:
function index(pdf = 0)...
Then check that optional parameter with:
$pdf = $this->uri->segment(2, 0); //not sure, should be 2? try it...`
And then, if $pdf=='1' (send nummber rather than string 'true') ...etc,etc...