Is it possible to shorten this query - sql

we have used loop for read between timestamps. This works but this way exhausting for server with million records.
The timestamps are determined by the $resolution variable and assigned to a new array. But we know this is poor way. I think we can do that without while. Maybe one query enough.
Route::get('/history', function (Request $request) {
//return response()->json([]);
$symbol = explode("-", $request->get("symbol"));
$coin = Coin::where("symbol", $symbol[1])->first();
$currency = Coin::where("symbol", $symbol[0])->first();
$resolution = $request->get("resolution");
$from = $request->get("from");
$to = $request->get("to");
$uniqueKey = sprintf("OHLCKEY%s%s%s%s",
$symbol[0], $symbol[1],
Carbon::createFromTimestamp($from)->second(0)->timestamp,
Carbon::createFromTimestamp($to)->second(0)->timestamp);
$redis=Redis::connection();
$cache = $redis->get($uniqueKey);
if($cache) {
$data = json_decode($cache);
return response()->json($data);
} else {
$t = [];
$o = [];
$h = [];
$l = [];
$c = [];
$v = [];
$s = "no_data";
$minMaxDates = TransactionDetail::where("type", "buy")
->whereBetween("created_at", [
Carbon::createFromTimestamp($from),
Carbon::createFromTimestamp($to)
])->where("coin_id", $coin->id)->where("currency_id", $currency->id)
->selectRaw("min(created_at) as minDate, max(created_at) as maxDate")->first();
if($minMaxDates->minDate) {
$minDate = Carbon::createFromTimeString($minMaxDates->minDate)->timestamp;
if($from < $minDate) {
$startDate = $minDate;
} else {
$startDate = (int)$from;
}
$maxDate = Carbon::createFromTimeString($minMaxDates->maxDate)->timestamp;
if($to > $maxDate) {
$to = $maxDate;
}
while ($startDate < $to):
switch ($resolution) {
case "30";
$endDate = Carbon::createFromTimestamp($startDate)->addMinutes(30)->timestamp;
break;
case "60";
$endDate = Carbon::createFromTimestamp($startDate)->addHour(1)->timestamp;
break;
case "180";
$endDate = Carbon::createFromTimestamp($startDate)->addHour(3)->timestamp;
break;
case "360";
$endDate = Carbon::createFromTimestamp($startDate)->addHour(6)->timestamp;
break;
case "720";
$endDate = Carbon::createFromTimestamp($startDate)->addHour(12)->timestamp;
break;
default:
$endDate = Carbon::createFromTimestamp($startDate)->addHour(1)->timestamp;
break;
}
if ($endDate > $to) $endDate = $to;
$transactions = TransactionDetail::where("type", "buy")
->whereBetween("created_at", [
Carbon::createFromTimestamp($startDate),
Carbon::createFromTimestamp($endDate)
])->where("coin_id", $coin->id)->where("currency_id", $currency->id)->get();
if (!$transactions->isEmpty()) {
$t[] = $transactions->first()->created_at->timestamp;
$o[] = floatval($transactions->first()->price);
$h[] = floatval($transactions->max("price"));
$l[] = floatval($transactions->min("price"));
$c[] = floatval($transactions->last()->price);
$v[] = floatval($transactions->sum("amount"));
}
$startDate = $endDate + 1;
endwhile;
}
if (!empty($t)) $s = "ok";
$data = ["t" => $t, "o" => $o, "h" => $h, "l" => $l, "c" => $c, "v" => $v, "s" => $s];
$redis->set($uniqueKey, json_encode($data));
return response()->json($data);
}
});
Can we reproduce this code with one mysql query?
thank you in advance.

Related

Segmentation fault in the below program

The below code is being called from a simple script like this.
$test.line-validation();
method line-validation is rw {
my $file-data = slurp($!FileName, enc => "iso-8859-1");
my #lines = $file-data.lines;
my $start = now;
for #lines -> $line {
state $i = 1;
my #splitLine = split('|', $line);
if ($line.starts-with("H|") || $line.starts-with("T|")) {
next;
}
my $lnObject = LineValidation.new( line => $line, FileType => $.FileType );
$lnObject.ColumnIds = %.ColumnIds;
my #promises;
my #validationIds;
for %.ValidationRules.keys -> $validationId {
if (%.ValidationRules{$validationId}<ValidationType> eq 'COLUMN') {
push #promises, start {$lnObject.ColumnValidationFunction(%.ValidationRules{$validationId}<ValidationFunction>, %.ValidationRules{$validationId}<Arguments>, $.ValidationRules{$validationId}<Description>); 1};
push #validationIds, $validationId;
}
}
my #promise-output = await #promises;
for #validationIds -> $valId {
state $j = 0;
my $result = #promise-output[$j];
if ($result.Bool == True) {
if (%.ResultSet{$valId}<count> :!exists) {
%.ResultSet{$valId}<count> = 1;
} else {
%.ResultSet{$valId}<count> = %.ResultSet{$valId}<count> + 1;
}
my #prCol = (%.ValidationRules{$valId}<Arguments><column>, #.printColumns);
if (%.ResultSet{$valId}<count> <= 10) {
%.ResultSet{$valId}.push: (sample => join('|', #splitLine[#prCol[*;*]].map: { if ($_.Bool == False ) { $_ = ''} else {$_ = $_;} }));
}
%.ResultSet{$valId}<ColumnList> = #prCol[*;*];
}
$j++;
}
$i++;
}
say "Line validation completed in {now - $start } time for $.lineCount lines";
}
The code was working fine earlier but when run using larger files, it just arbitrarily throws the error Segmentation fault and exists. I cannot determine where it is failing either.

TCPDF + FPDI : Ensure that all fonts are fully embedded

I'm using TCPDF + FPDI. I want to embed all fonts fully. Font embedding works great when using addTTFfont() and SetFont() methods. But when I try to open existing pdf file using FPDI, I can't get list of used fonts and can't determine which of them are already embedded. Here is what I'm trying to do:
Get the font info(name, type, etc) of source pdf file;
Determine which fonts are not fully embedded;
Embed those fonts.
So far I tried to make TCPDF class protected member fonts public. So, I changed tcpdf.php file:
/**
* Array of used fonts.
* #protected
*/
public $fonts = array();
In main.php
$pdf = new FPDI();
$pdf->setSourceFile('Simple.pdf');
$pdf->AddPage();
$tplIdx = $pdf->importPage(1);
$pdf->useTemplate($tplIdx, 10, 10, 200);
$pdf->setFontSubsetting(false);
var_dump($pdf->fonts);
Output:
array (size=2)
'helvetica' =>
array (size=17)
'fontkey' => string 'helvetica' (length=9)
'i' => int 1
'type' => string 'core' (length=4)
'name' => string 'Helvetica' (length=9)
'desc' =>
array (size=13)
'Flags' => int 32
'FontBBox' => string '[-166 -225 1000 931]' (length=20)
'ItalicAngle' => int 0
'Ascent' => int 931
'Descent' => int -225
'Leading' => int 0
'CapHeight' => int 718
'XHeight' => int 523
'StemV' => int 88
'StemH' => int 76
'AvgWidth' => int 513
'MaxWidth' => int 1015
'MissingWidth' => int 513
'up' => int -100
'ut' => int 50
'cw' =>
array (size=256)
0 => int 500
1 => int 500
2 => int 500
...
...
'cbbox' =>
array (size=0)
empty
'dw' => int 513
'enc' => string '' (length=0)
'cidinfo' =>
array (size=4)
'Registry' => string 'Adobe' (length=5)
'Ordering' => string 'Identity' (length=8)
'Supplement' => int 0
'uni2cid' =>
array (size=0)
empty
'file' => string '' (length=0)
'ctg' => string '' (length=0)
'subset' => boolean false
'subsetchars' =>
array (size=255)
0 => boolean true
1 => boolean true
2 => boolean true
3 => boolean true
...
...
'n' => int 3
'helveticaB' =>
array (size=17)
'fontkey' => string 'helveticaB' (length=10)
'i' => int 2
'type' => string 'core' (length=4)
'name' => string 'Helvetica-Bold' (length=14)
'desc' =>
array (size=13)
'Flags' => int 32
'FontBBox' => string '[-170 -228 1003 962]' (length=20)
'ItalicAngle' => int 0
'Ascent' => int 962
'Descent' => int -228
'Leading' => int 0
'CapHeight' => int 718
'XHeight' => int 532
'StemV' => int 140
'StemH' => int 118
'AvgWidth' => int 535
'MaxWidth' => int 1000
'MissingWidth' => int 535
'up' => int -100
'ut' => int 50
'cw' =>
array (size=256)
0 => int 278
1 => int 278
2 => int 278
3 => int 278
...
...
'cbbox' =>
array (size=0)
empty
'dw' => int 535
'enc' => string '' (length=0)
'cidinfo' =>
array (size=4)
'Registry' => string 'Adobe' (length=5)
'Ordering' => string 'Identity' (length=8)
'Supplement' => int 0
'uni2cid' =>
array (size=0)
empty
'file' => string '' (length=0)
'ctg' => string '' (length=0)
'subset' => boolean false
'subsetchars' =>
array (size=255)
0 => boolean true
1 => boolean true
2 => boolean true
3 => boolean true
...
...
'n' => int 5
There are info about 2 fonts: "helvetica"(which is the default font according to TCPDF documentacion) and "helveticaB". But source file contains other fonts too. They are not listed. How to list them?
Thanks in advance...
You will need to ensure that the imported document/pages have all fonts embedded in advance. It is impossible to ensure that fonts in imported pages are embedded through FPDI.
You also cannot access/list the fonts with FPDI.
After digging in .pdf file and reading some topics of pdf documentation, I managed to somehow solve the problem. The following code is only draft and needs a lot of improvements. I've tested several .pdf files and it worked, but there are a lot of things, that need a better approach.
While trying to solve the problem, I decided to leave TCPDF and FPDI libraries unchanged. I haven't used any method of those libraries, since I don't know the inner functionality of those libraries.
P.S: Unfortunately, I didn't have much time and my code is long (and not stable):
<?php
require_once('tcpdf.php');
require_once('fpdi.php');
class PDF extends FPDI
{
var $_tplIdx;
private $objects;
private $font_list;
private $raw_data;
private $raw_data_len;
private $font_read_offset;
private $embedded_font_addr;
private $is_descendant;
function __construct($filename)
{
$this->objects = array();
$this->font_list = array();
$this->raw_data = file_get_contents($filename);
$this->raw_data_len = strlen($this->raw_data);
$this->font_read_offset = 0;
$this->is_descendant = false;
parent::__construct();
}
private function EmbeddedFontRefs()
{
do
{
$f = $this->GetXObj('Subtype /CIDFontType2');
if($f == NULL)
return;
//var_dump($f);
$keys = array_keys($f);
$f = $f[$keys[0]];
$name = '';
$ref = '';
if(preg_match('/Subtype \/CIDFontType2/', $f))
{
$matches = array();
if(preg_match('/BaseFont \/\S+/', $f, $matches))
$name = substr($matches[0], 10);
if(preg_match('/FontDescriptor \d+ \d+ R+/', $f, $matches))
{
$ref = substr($matches[0], 15);
$ref = $this->GetObjFromRef($ref);
}
$this->embedded_font_addr[$name] = $ref;
}
}while($f !== NULL);
}
private function ParseObj($obj_search_string, $referrer_object_addr=NULL, $referrer_object_name=NULL)
{
$object_data = $this->GetObj($obj_search_string);
if($object_data == NULL)
return false;
$object_addr = array_keys($object_data);
$object_addr = $object_addr[0];
$object_raw_data = $object_data[$object_addr];
$embedded = false;
$subset = false;
if(!array_key_exists($object_addr, $this->objects))
{
$this->objects[$object_addr] = array();
$obj_content_items = preg_split('/\n\r?/', $object_raw_data);
for($i = 0; $i < count($obj_content_items); $i++)
{
$item = explode(' ', $obj_content_items[$i], 2);
if($item[0][0] == '/')
$item[0] = substr($item[0], 1);
if($item[1][0] == '/')
$item[1] = substr($item[1], 1);
if(($i + 1) < count($obj_content_items))
$item[1] = substr($item[1], 0, -1);
if(($item[0] == 'FontFile') || ($item[0] == 'FontFile2') || ($item[0] == 'FontFile3'))
$embedded = true;
else if(($item[0] == 'BaseFont') || ($item[0] == 'FontName'))
{
$item[1] = str_replace('#20', ' ', $item[1]);
if(preg_match('/[A-Z]{5}\+/', $item[1]))
$subset = true;
}
$this->objects[$object_addr][$item[0]] = $item[1]; // item_name => item_value
}
}
if($referrer_object_addr == NULL)
{
if(array_key_exists('Subtype', $this->objects[$object_addr]))
{
if($this->objects[$object_addr]['Subtype'] == 'Type0') //Composite Fonts
{
if(array_key_exists('DescendantFonts', $this->objects[$object_addr]))
{
$descendant_fonts_obj_addr = $this->GetObjFromRef($this->objects[$object_addr]['DescendantFonts']);
array_push($this->font_list, '');
$this->font_list[count($this->font_list)-1] = $this->objects[$object_addr];
$this->ParseObj($descendant_fonts_obj_addr, $object_addr, 'DescendantFonts');
}
}
}
if(array_key_exists('FontDescriptor', $this->objects[$object_addr]))
{
$font_desc_obj_addr = $this->GetObjFromRef($this->objects[$object_addr]['FontDescriptor']);
array_push($this->font_list, '');
$this->font_list[count($this->font_list)-1] = $this->objects[$object_addr];
$this->ParseObj($font_desc_obj_addr, $object_addr, 'FontDescriptor');
}
}
else
{
if(array_key_exists($referrer_object_addr, $this->objects))
{
if($referrer_object_name == 'DescendantFonts')
{
$this->font_list[count($this->font_list)-1]['DescendantFonts'] = array();
array_push($this->font_list[count($this->font_list)-1]['DescendantFonts'], $this->objects[$object_addr]);
$this->is_descendant = true;
if(array_key_exists('FontDescriptor', $this->objects[$object_addr]))
{
$font_desc_obj_addr = $this->GetObjFromRef($this->objects[$object_addr]['FontDescriptor']);
$this->ParseObj($font_desc_obj_addr, $object_addr, 'FontDescriptor');
}
}
else if($referrer_object_name == 'FontDescriptor')
{
if(array_key_exists('Type', $this->objects[$object_addr]))
unset($this->objects[$object_addr]['Type']);
//Embedded or not?
$keys = array_keys($this->objects[$object_addr]);
foreach($keys as $key)
{
if(($key == 'FontFile') || ($key == 'FontFile2') || ($key == 'FontFile3'))
{
$embedded = true;
break;
}
}
if(!$this->is_descendant)
{
$this->font_list[count($this->font_list)-1]['Embedded'] = $embedded;
$this->font_list[count($this->font_list)-1]['Subset'] = $subset;
$this->font_list[count($this->font_list)-1]['FontDescriptor'] = $this->objects[$object_addr];
}
else
{
$this->font_list[count($this->font_list)-1]['DescendantFonts'][0]['Embedded'] = $embedded;
$this->font_list[count($this->font_list)-1]['DescendantFonts'][0]['Subset'] = $subset;
$this->font_list[count($this->font_list)-1]['DescendantFonts'][0]['FontDescriptor'] = $this->objects[$object_addr];
$this->is_descendant = false;
}
}
}
}
return true;
}
private function GetObj($obj_search_string)
{
$obj_offset = strpos($this->raw_data, $obj_search_string, $this->font_read_offset);
if($obj_offset == false)
{
return NULL;
}
$obj_start = 0;
$obj_end = 0;
$object_content_start = 0;
$object_content_end = 0;
$obj_start = strrpos($this->raw_data, 'endobj', $obj_offset - $this->raw_data_len) + 8; // for 'endobj\n\r';
$obj_end = strpos($this->raw_data, 'endobj', $obj_start);
$this->font_read_offset = $obj_end;
$object_content_start = strpos($this->raw_data, '<<', $obj_start) + 2; //for '<<'
$object_content_end = strpos($this->raw_data, '>>', $object_content_start) - 2; //for /n/r;
$object_addr = substr($this->raw_data, $obj_start, $object_content_start - $obj_start - 4); // -2 for /n/r;
$object_raw_data = substr($this->raw_data, $object_content_start, $object_content_end - $object_content_start);
return array($object_addr => $object_raw_data);
}
private function GetXObj($obj_search_string)
{
$obj_offset = strpos($this->raw_data, $obj_search_string, $this->font_read_offset);
if($obj_offset == false)
return NULL;
$obj_start = 0;
$obj_end = 0;
$object_content_start = 0;
$object_content_end = 0;
$obj_start = strrpos($this->raw_data, 'endobj', $obj_offset - $this->raw_data_len) + 8; // for 'endobj\n\r';
$obj_end = strpos($this->raw_data, 'endobj', $obj_start);
$this->font_read_offset = $obj_end;
$object_content_start = strpos($this->raw_data, '<<', $obj_start) + 2; //for '<<'
$object_content_end = $obj_end - 1;
$object_addr = substr($this->raw_data, $obj_start, $object_content_start - $obj_start - 3); // -2 for /n/r;
$object_raw_data = substr($this->raw_data, $object_content_start, $object_content_end - $object_content_start);
return array($object_addr => $object_raw_data);
}
private function GetFontDescriptor($font_object_addr)
{
if(array_key_exists('FontDescriptor', $this->objects[$font_object_addr]))
$font_desc_ref = $this->objects[$font_object_addr]['FontDescriptor'];
$font_desc_obj_addr = $this->GetObjFromRef($font_desc_ref);
$this->ParseObj($font_desc_obj_addr, $font_object_addr);
}
}
private function GetObjFromRef($obj_ref)
{
$obj_ref_num = explode(' ', $obj_ref);
$obj = '';
if(end($obj_ref_num) == 'R')
{
foreach($obj_ref_num as $num)
{
if($num != 'R')
$obj .= (string)$num. ' ';
else
$obj .= 'obj';
}
}
else if(end($obj_ref_num) == 'obj')
$obj = $obj_ref;
return $obj;
}
public function EnsureFonts($filename)
{
$this->raw_data = file_get_contents($filename);
$this->raw_data_len = strlen($this->raw_data);
$this->font_read_offset = 0;
$this->EmbeddedFontRefs();
$this->font_read_offset = 0;
$font_objs = array();
do
{
$xobj = $this->GetXObj('/Type /XObject');
if($xobj !== NULL)
{
$xobj_addr = array_keys($xobj);
$xobj_addr = $xobj_addr[0];
$matches = array();
if(preg_match('/Resources \d+ \d+ R/', $xobj[$xobj_addr], $matches))
{
array_push($font_objs, $this->GetObjFromRef(substr($matches[0], 10))); //Resources+' '
}
}
}while($xobj !== NULL);
$fxobjs = array();
foreach($font_objs as $font_obj)
{
$fobj = $this->GetXObj($font_obj);
if($fobj !== NULL)
{
$xobj_addr = array_keys($fobj);
$xobj_addr = $xobj_addr[0];
$matches = array();
if(preg_match('/Font \d+ \d+ R/', $fobj[$xobj_addr], $matches))
{
array_push($fxobjs, $this->GetObjFromRef(substr($matches[0], 10))); //Resources+' '
}
}
}
$actual_font_objs = array();
foreach($fxobjs as $fxobj)
{
$f_obj = $this->GetXObj($fxobj);
if($f_obj !== NULL)
{
$xobj_addr = array_keys($f_obj);
$xobj_addr = $xobj_addr[0];
$matches = array();
if(preg_match('/F\d+/', $f_obj[$xobj_addr], $matches))
{
array_push($actual_font_objs, $this->GetObjFromRef(substr($matches[0], 10))); //Resources+' '
}
}
}
foreach($actual_font_objs as $fo)
{
$f = $this->GetXObj($fxobj);
if($f !== NULL)
{
$xobj_addr = array_keys($f);
$xobj_addr = $xobj_addr[0];
$matches = array();
$f_name = '';
if(preg_match('/BaseFont \/\S+/', $f[$xobj_addr], $matches))
{
$f_name = substr($matches[0], 10); // BaseFont+' '+'/'
}
if(preg_match('/FontDescriptor \d+ \d+ R/', $f[$xobj_addr], $matches))
{
$fd_replace_start_pos = strpos($this->raw_data, $matches[0]) + 15; //FontDescriptor+' ';
$fd_replace_end_pos = strpos($this->raw_data, 'R', $fd_replace_start_pos) + 1; //For 'R';
$s = substr($this->raw_data, 0, $fd_replace_start_pos);
$r = substr($this->embedded_font_addr[$f_name], 0, -3).'R'; //-4 for obj.
$e = substr($this->raw_data, $fd_replace_end_pos);
$this->raw_data = $s . $r . $e;
}
}
}
file_put_contents($filename, $this->raw_data);
}
public function GetFontInfo()
{
do
{
$this->ParseObj('/Type /Font');
}while($this->ParseObj('/Type /Font') == true);
return $this->GetNotEmbeddedFonts();
}
private function GetNotEmbeddedFonts()
{
$result = array();
foreach($this->font_list as $font)
{
if(array_key_exists('DescendantFonts', $font))
{
foreach($font['DescendantFonts'] as $desc_font)
{
if((array_key_exists('Embedded', $desc_font)) && (array_key_exists('BaseFont', $desc_font)))
if(!$desc_font['Embedded'])
array_push($result, $desc_font['BaseFont']);
}
}
else
{
if((array_key_exists('Embedded', $font)) && (array_key_exists('BaseFont', $font)))
if(!$font['Embedded'])
array_push($result, $font['BaseFont']);
}
}
for($i= 0; $i<count($result); $i++)
{
if(strpos($result[$i], ' ') || strpos($result[$i], ',')) //Adobe Acrobat system font name list. Needs improvment..
{
switch($r)
{
case 'Times New Roman':
$result[$i] = 'TimesNewRomanPSMT';
break;
case 'Times New Roman,Bold':
$result[$i] = 'TimesNewRomanPS-BoldMT';
break;
case 'Times New Roman,Italic':
$result[$i] = 'TimesNewRomanPS-ItalicMT';
break;
case 'Times New Roman,BoldItalic':
$result[$i] = 'TimesNewRomanPS-BoldItalicMT';
break;
case 'Aial':
$result[$i] = 'ArialMT';
break;
}
}
}
return $result;
}
}
$pdf = new PDF($filename);
$pdf->setSourceFile($filename);
$pdf->AddPage();
$tplIdx = $pdf->importPage(1);
$pdf->useTemplate($tplIdx);
$pdf->SetMargins(PDF_MARGIN_LEFT, 40, PDF_MARGIN_RIGHT);
$pdf->SetAutoPageBreak(true, 40);
$pdf->setFontSubsetting(false);
$not_embedded_fonts = $pdf->GetFontInfo();
foreach($not_embedded_fonts as $f)
{
$font_name = strtolower($f);
$font_name = preg_replace('/[^a-z0-9_]/', '', $font_name);
$search = array('bold', 'oblique', 'italic', 'regular');
$replace = array('b', 'i', 'i', '');
$font_name = str_replace($search, $replace, $font_name);
if($pdf->AddFont($font_name) !== false)
$pdf->SetFont($font_name);
else
print('"' .$font_name. '" font not found!');
}
$pdf->Output();
?>

CDbCriteria throwing column name is ambigous

I have the following method in a controller of my Yii app.
public function actionManage($typeid=0, $locationid=0, $page=1, $rows=12, $sidx='date_input', $sord='desc', $kategori='')
{
if (Yii::app()->request->isAjaxRequest) {
// Jika dilakukan operasi 'edit' pada row
if (isset($_REQUEST['oper']))
{
$oper = $_REQUEST['oper'];
$id = $_REQUEST['id'];
if ($oper == 'edit')
{
$value = $_REQUEST['value'];
$record = InputData::model()->findByPk($id);
$record->value = $value;
$record->update();
}
if($oper == 'delete'){
$model = InputData::model()->findByPk($id);
$model->delete();
}
}
// inisialisasi criteria query
$criteria = new CDbCriteria();
$criteria->order = "$sidx $sord";
// filter lokasi
if (is_numeric($locationid) && $locationid !== 0)
{
$criteria->with = array('data'=>array(
'condition'=>'data.locationid=:locationid',
'params'=>array(':locationid'=>$locationid)
));
} else {
if (is_numeric($typeid) && $typeid !== 0)
{
$criteria->with = array('data.location'=>array(
'with'=>array(
'type'=>array(
'condition'=>'type.typeid=:typeid',
'params'=>array(':typeid'=>$typeid)
)
)
));
} else {
$criteria->with = array('data.location'=>array(
'with'=>array(
'type'=>array(
'condition'=>'type.type_desc=:type_desc',
'params'=>array(':type_desc'=>$kategori)
)
)
));
}
}
// filter range tanggal
if (isset($_REQUEST['startdate'], $_REQUEST['enddate']))
{
$startdate = $_REQUEST['startdate'];
$enddate = $_REQUEST['enddate'];
$criteria->condition = 'date_input <= :enddate AND date_input >= :startdate';
$criteria->params = array(':startdate'=>$startdate, ':enddate'=>$enddate);
}
if(isset($_REQUEST['dataid'])){
$dataid = $_REQUEST['dataid'];
$criteria->addCondition("dataid = $dataid");
}
$dataProvider = new CActiveDataProvider('InputData', array(
'criteria'=>$criteria,
'pagination'=>array(
'currentPage'=>$page-1,
'pageSize'=>$rows
)
));
$count = $dataProvider->totalItemCount;
$total_pages = $count > 0 ? ceil($count/$rows) : 0;
if ($page > $total_pages) $page=$total_pages;
// generate response untuk jqgrid
$response = new stdClass();
$response->page = $page;
$response->total = $total_pages;
$response->records = $count;
foreach($dataProvider->getData() as $row)
{
$response->rows[] = array(
'id'=>$row->inputdataid,
'cell'=>array(
$row->inputdataid,
$row->date_input,
$row->time_input,
$row->data->location->location_name,
$row->data->data_name,
$row->data->variable->var_name,
round($row->value, 3),
$row->data->variable->unit->uom_name,
($row->inputOfficer !== NULL ? $row->inputOfficer->officer_name:''),
'<i class="fa fa-pencil-square-o"></i> Edit <i class="fa fa-trash-o"></i> Delete'
)
);
}
echo json_encode($response);
} else {
$url = $this->createUrl("dataAir/manage");
$delurl = $this->createUrl("dataAir/deleteRow");
$startdate = '2013-01-01';
$enddate = date_format(new DateTime(), 'Y-m-d');
$this->render('jqgrid', array(
'kategori'=>$kategori,
'url'=>$url,
'delurl'=>$delurl,
'startdate'=>$startdate,
'enddate'=>$enddate
));
}
}
this line causing the problem
if(isset($_REQUEST['dataid'])){
$dataid = $_REQUEST['dataid'];
$criteria->addCondition("dataid = $dataid");
}
when, I remove those lines the method work just fine. what could be the problem causing ambigous colum name? here is the error log
SELECT COUNT(DISTINCT "t"."inputdataid") FROM "app_inputdata" "t" LEFT OUTER JOIN "app_ref_periodicdata" "data" ON ("t"."dataid"="data"."dataid") WHERE ((date_input <= :enddate AND date_input >= :startdate) AND (dataid = 7)) AND (data.locationid=:locationid). Bound with :startdate='2013-01-01', :enddate='2015-02-02', :locationid='6'
You need to add the table alias t to your condition:
$criteria->addCondition("t.dataid = $dataid");
Also, since $dataid is being obtained from a $_REQUEST it is best to pass it as a parameter. This can be done in two ways:
$criteria->addCondition("t.dataid = :dataid", [":dataid" => $dataid]);
$criteria->compare("t.dataid", $dataid);
you have used table aliases,
'with'=>array(
'type'=>array(
'condition'=>'type.type_desc=:type_desc', <<- I mean here you have used alias : type
'params'=>array(':type_desc'=>$kategori)
)
you just have to remember that the main model that you are working with, will always need alias t to unambigufy! (not sure if that is an actual word :D )

How to save data in db

In user table, the data stored in db as well as resize the images in folders.
But the usergallary table same functionality didn't work only image resized in the folders but not stored in db.
public function actionProfileImage($id) {
$userModel = User::model()->findByAttributes(array('id' => $id));
$userGallary = new UserGallary;
$filePrefix = Yii::app()->params['profile']['prefix'];
$targetImagePath = Yii::app()->params['profile']['image']['path'];
$target150Path = Yii::app()->params['profile']['thumb150']['path'];
$target25Path = Yii::app()->params['profile']['thumb25']['path'];
if (isset($_FILES['uploaded_picture'])) {
$handle = new Upload($_FILES['uploaded_picture']);
if ($handle->uploaded) {
$handle->file_name_body_pre = $filePrefix;
$handle->process($targetImagePath);
if ($handle->processed) {
$handle->file_name_body_pre = $filePrefix;
$handle->image_resize = true;
$handle->image_x = 150;
$handle->image_y = 150;
//$handle->image_ratio_y = true;
$handle->image_ratio = true;
$handle->process($target150Path);
if ($handle->processed) {
$handle->file_name_body_pre = $filePrefix;
$handle->image_resize = true;
$handle->image_x = 25;
$handle->image_y = 25;
// $handle->image_ratio_y = true;
$handle->image_ratio = true;
var_dump($$target150Path);exit;
$handle->process($target25Path);
if ($handle->processed) {
$userGallary->uploaded_picture = "{$filePrefix}.{$handle->file_dst_name_ext}";
$userGallary->save();
}
}
$handle->clean();
}
}
}
echo CJSON::encode( arraY('path150' => $target150Path,
'file' => $userGallary->uploaded_picture,
));
}

SQL showing in place of web page

I've copied a codeigniter website from my old computer to my new one, and copied the database. When I navigate to the site in my localhost however, I just get SQL printed out
db->where($cell_name, $id); $this->db->delete($table_name); if (!is_array($tags)) { $tmp = array(); $tmp[] = $tags; $tags = $tmp; } if (!empty($tags)) { foreach($tags as $tag) { switch(gettype($tag)) { case 'integer': $this->db->set($cell_name, $id); $this->db->set('tag_id', $tag); $this->db->insert($table_name); break; case 'string': $tag = strtolower($tag); $results = $this->db->query('SELECT id FROM tag WHERE tag = \''.$tag.'\' LIMIT 1'); if ($results->num_rows() == 1) { $tag_id = $results->first_row()->id; } else { $this->db->set('tag', $tag); $this->db->set('permalink', url_title($tag, 'dash')); $this->db->insert('tag'); $tag_id = $this->db->insert_id(); } $this->db->set($cell_name, $id); $this->db->set('tag_id', $tag_id); $this->db->insert($table_name); break; } } } } function get_active($type, $id, $return_ids = true) { $ret = array(); switch($type) { case 'media': $table_name = 'media_tag'; $cell_name = 'media_id'; break; case 'post': $table_name = 'post_tag'; $cell_name = 'post_id'; break; } if (!$id) { $results = $this->db->query('SELECT tag.id, tag.tag, tag.permalink FROM '.$table_name.', tag WHERE tag_id = tag.id GROUP BY tag.id ORDER BY tag.tag'); if ($results->num_rows() > 0) { foreach($results->result() as $data) { if ($return_ids) $ret[] = $data->id; else $ret[] = (object) array('id' => $data->id, 'tag' => $data->tag, 'permalink' => $data->permalink); } } } else { $results = $this->db->query('SELECT tag.id, tag.tag, tag.permalink FROM '.$table_name.', tag WHERE '.$cell_name.' = '.$id.' AND tag_id = tag.id ORDER BY tag.tag'); if ($results->num_rows() > 0) { foreach($results->result() as $data) { if ($return_ids) $ret[] = $data->id; else $ret[] = (object) array('id' => $data->id, 'tag' => $data->tag, 'permalink' => $data->permalink); } } } return $ret; } }
Does anyone know if there's a simple reason for this - some setting I need to turn on in wamp for example, I'm completely at a loss, the files and database are identical on my old and new computers
It turns out I had simply not allowed "short open tags" from the WAMP PHP settings