I've got a problem with insert and update queries in Zend (Select is ok).
Definition of table:
class Application_Model_DbTable_Kpr1Data extends Zend_Db_Table_Abstract
{
protected $_name = 'kpr_kpr1_data';
}
Here is my data mapper (model)
class Application_Model_Kpr1DataMapper
{
protected $_dbTable;
public function setdbTable($dbTable) {
if(is_string($dbTable)){
$dbTable = new $dbTable();
}
if(!$dbTable instanceof Zend_Db_Table_Abstract ){
throw new Exception ('Invalid table data gateway provided.');
}
$this->_dbTable = $dbTable;
return $this;
}
public function getdbTable() {
if (null === $this->_dbTable){
$this->setdbTable('Application_Model_DbTable_Kpr1Data');
}
return $this->_dbTable;
}
public function save(Application_Model_Kpr1Data $kpr1data){
$data = array('id' => (int) $kpr1data->getId(),
'kpr1_plaza_id' => (int) $kpr1data->getPlaza(),
'kpr1_data' => new Zend_db_Expr("STR_TO_DATE('".$kpr1data->getDate()."', '%Y-%m-%d')"),
'kpr1_money_delivered' => (float) $kpr1data->getDelivered(),
'kpr1_money_transactions' => (float) $kpr1data->getTransactions(),
'kpr1_created' => new Zend_Db_Expr('CURDATE()')
);
$id = (int) $kpr1data->getId();
$table = $this->getdbTable();
if (is_null($id) && $id != 0) {
unset($data['id']);
$table->insert($data);
} else {
$table->update($data, array('id => ?', $id));
}
}
The last one is save function that should insert and update the data!
And this save is called from save action:
public function saveAction()
{
$plazaid = (int) $this->getRequest()->getParam('plaza');
$date = (string) $this->getRequest()->getParam('date');
$delivered = (string) $this->getRequest()->getParam('delivered');
$transactions = (string) $this->getRequest()->getParam('transactions');
$kpr1data = new Application_Model_Kpr1Data();
if ($plazaid && $date) {
$kpr1datamapper = new Application_Model_Kpr1DataMapper();
if($kpr1datamapper->findDatePlaza($date, $plazaid, $kpr1data)){
$kpr1data->setDelivered($delivered)
->setTransactions($transactions);
$kpr1datamapper->save($kpr1data);
$this->_helper->layout->disableLayout();
$this->view->result = json_encode(array("success"=>"true"));
} else {
$kpr1data->setDate($date);
$kpr1data->setDelivered($delivered);
$kpr1data->setTransactions($transactions);
$kpr1data->setPlaza($plazaid);
$kpr1datamapper->save($kpr1data);
$this->_helper->layout->disableLayout();
$this->view->result = json_encode(array("success"=>"true"));
}
$this->_helper->layout->disableLayout();
$this->view->result = json_encode(array(
//"success"=>"false",
"errorMsg"=>"Saving error"
));
} else {
$this->_helper->layout->disableLayout();
$this->view->result = json_encode(array(
//"success"=>"false",
"errorMsg"=>"Saving error"
));
}
return true;
}
Save action is called via JS, but even called directly through webbrowser it fails.
Behaviour: Application is running, and when debugger runs into update/insert line:
if (is_null($id) && $id != 0) {
unset($data['id']);
$table->insert($data);
} else {
$table->update($data, array('id => ?', $id));
}
it's redirecting to ErrorController.
I've check that:
1. firePHP is not showing this statements
2. MySQL database doesn't log this statement (I've checked via general_log feature).
I'm stucked. Help me please.
edit
$data=
array(6) (
[id] => (int) 0
[kpr1_plaza_id] => (int) 116
[kpr1_data] => Zend_Db_Expr object {
_expression => (string) STR_TO_DATE('2013-03-01', '%Y-%m-%d')
}
[kpr1_money_delivered] => (float) 120
[kpr1_money_transactions] => (float) 122
[kpr1_created] => Zend_Db_Expr object...
$kpr1data=
Application_Model_Kpr1Data object {
_plaza => (string) 116
_date => (string) 2013-03-01
_delivered => (string) 120.00
_transactions => (string) 122.00
_created => null
_id => null
_plazaname => null
}
This one should do insert.
And next one update:
Application_Model_Kpr1Data object {
_plaza => (string) 117
_date => (string) 2013-03-01
_delivered => (string) 120.00
_transactions => (string) 122.00
_created => (string) 2013-03-06 12:42:13
_id => (string) 79
_plazaname => (string) SPO Kraj...
in your saveAction() $this->view->result gets overwritten after if/else statement since your function does not return anything after (initially) setting $this->view->result.
Furthermore setting the first Saving error seems to be needless.
Try this:
public function saveAction()
{
$plazaid = (int) $this->getRequest()->getParam('plaza');
$date = (string) $this->getRequest()->getParam('date');
$delivered = (string) $this->getRequest()->getParam('delivered');
$transactions = (string) $this->getRequest()->getParam('transactions');
$kpr1data = new Application_Model_Kpr1Data();
if ($plazaid && $date) {
$kpr1datamapper = new Application_Model_Kpr1DataMapper();
if($kpr1datamapper->findDatePlaza($date, $plazaid, $kpr1data)){
$kpr1data->setDelivered($delivered)
->setTransactions($transactions);
$kpr1datamapper->save($kpr1data);
$this->_helper->layout->disableLayout();
$this->view->result = json_encode(array("success"=>"true"));
} else {
$kpr1data->setDate($date);
$kpr1data->setDelivered($delivered);
$kpr1data->setTransactions($transactions);
$kpr1data->setPlaza($plazaid);
$kpr1datamapper->save($kpr1data);
$this->_helper->layout->disableLayout();
$this->view->result = json_encode(array("success"=>"true"));
}
} else {
$this->_helper->layout->disableLayout();
$this->view->result = json_encode(array(
//"success"=>"false",
"errorMsg"=>"Saving error"
));
}
return true;
}
EDIT:
Try this as your save action:
public function save(Application_Model_Kpr1Data $kpr1data){
$table = $this->getdbTable();
if ($id == $kpr1data->getId()) {
$data = array('id' => (int) $id,
'kpr1_plaza_id' => (int) $kpr1data->getPlaza(),
'kpr1_data' => new Zend_Db_Expr("STR_TO_DATE('".$kpr1data->getDate()."', '%Y-%m-%d')"),
'kpr1_money_delivered' => (float) $kpr1data->getDelivered(),
'kpr1_money_transactions' => (float) $kpr1data->getTransactions(),
'kpr1_created' => new Zend_Db_Expr('CURDATE()')
);
$table->update($data, array('id => ?', $id));
} else {
[...]
$table->insert($data);
}
}
Related
I have a SQL query that return the result to List
public class WebTrafficStat
{
public string Group { get; set; }
public string Stat { get; set; }
public string Total { get; set; }
public bool? IsSubTotal { get; set; }
}
The result must be displayed as HTML Pivot table with either results grouped by "Stat" or "Group" field.
SQL result:
Desired result as HTML
I did the functions that does this PHP but now the project must be written in MVC Core
This is my PHP functions that I used currently
I grouped the array from SQL call results here:
function array_group_by(array $arr, $gElement, $gkey) {
$refined = $arr;
$result = array();
foreach ($arr[$gElement] as $data) {
$id = $data[$gkey];
if ( isset($data[$gkey]) && !empty($data[$gkey]) ) {
if (isset($result[$id])) {
$result[$id][] = $data;
} else {
$result[$id] = array($data);
}
}
}
if ( !empty($result) ) {
$refined[$gElement] = array();
foreach($result as $key=>$value) {
$refined[$gElement][] = array('name' => $key, 'childs' => $value);
}
$refined['grouped'] = 1;
}
return $refined;
}
And second function is
function grouped_array_to_html ($grouped, $groupField ){
$tableRows = array();
$columns = array();
$Row_Data = array();
if ( $grouped['grouped'] == 1 ){ // grouping found
$columns[] = 'Group';
// table columns/headers
foreach ($grouped['rows'] as $row) {
foreach ($row['childs'] as $child) {
if ( !in_array($child[$groupField], $columns ) ){
$columns[] = $child[$groupField];
}
}
}
//table rows
foreach ($grouped['rows'] as $a => $row) {
$tableRows[$a][] = $row['name'];
foreach ($row['childs'] as $c => $child) {
foreach ($columns as $x => $col){
if ( $col == $child[$groupField] )
{
$tableRows[$a][$x] = $child['total'];
break;
}
}
}
}
//Output Finale
foreach ( $tableRows as $b => $tr )
{
foreach ($columns as $c => $col) {
if ( !isset($tableRows[$b][$c]) )
$Row_Data[$b][$c] = '-';
else
$Row_Data[$b][$c] = $tableRows[$b][$c];
}
}
} else { // no grouping
foreach ($grouped['rows'] as $row) {
$Row_Data[] = $row;
}
}
$htmlOut = array(
'theaders' => $columns,
'trows' => $Row_Data
);
$grouped['rows']['html'] = $htmlOut;
return $grouped;
}
And I call PHP like this ... this gives me HTML table header rows and Body rows
In this case, I tell it to group by "Stat" column
$result = grouped_array_to_html( array_group_by($response, 'rows', 'group'), 'stat' );
So please how can I achieve same result using EF Core or Linq
I ended up approaching it like this:
if ((Model.WebReport != null) && Model.WebReport.Rows.Any())
{
var columns = Model.WebReport.Rows
.Select(c => (Model.GroupBy.Equals("Group") ? c.Stat : c.Group))
.Distinct()
.ToList();
columns.Insert(0, "Group");
var reportStat = Model.WebReport.Rows
.GroupBy(g => (Model.GroupBy.Equals("Group") ? g.Group : g.Stat))
.Select(x => new
{
Group = x.Key,
Stats = x.ToDictionary(y => (Model.GroupBy.Equals("Group") ? y.Stat : y.Group), y => y.Total)
});
Model.Columns = columns;
Model.ReportStats = reportStat.ToList();
}
How to make loading multiple images and making many-to-many relationships on a table of apartments?
I have a model Apartment:
public function getApartmentImages()
{
return $this->hasMany(ApartmentImages::className(), ['apartment_id' => 'id']);
}
public function getImages()
{
return $this->hasMany(Images::className(), ['id' => 'image_id'])
->via('apartmentImages');
}
Model Images:
public function getApartmentImages()
{
return $this->hasMany(ApartmentImages::className(), ['image_id' => 'id']);
}
/**
* #return \yii\db\ActiveQuery
*/
public function getApartments()
{
return $this->hasMany(Apartment::className(), ['id' => 'apartment_id'])
->via('apartmentImages');
}
Model ApartmentImages
public function getImage()
{
return $this->hasOne(Images::className(), ['id' => 'image_id']);
}
public function getApartment()
{
return $this->hasOne(Apartment::className(), ['id' => 'apartment_id']);
}
The apartment has a main image (layout) and many others.
I managed to make the layout load:
public function actionCreate()
{
$model = new Apartment();
$model->load(Yii::$app->getRequest()->getBodyParams(), '');
$layout = new Images();
$layout->imageFile = UploadedFile::getInstanceByName('layout');
if ($layout->upload()) {
$model->layout_id = $layout->id;
}
if ($model->validate()) {
if ($model->save()) {
$response = Yii::$app->getResponse();
$response->setStatusCode(201);
$id = implode(',', array_values($model->getPrimaryKey(true)));
$response->getHeaders()->set('Location', Url::toRoute(['view', 'id' => $id], true));
} elseif (!$model->hasErrors()) {
throw new ServerErrorHttpException('Failed to create the object for unknown reason.');
}
}
return $model;
}
I filled the array with all the sent pictures, and in the loop I upload to the site, and using
//INSERT INTO `apartment_images` (`apartment_id`, `image_id`) VALUES (...)
$model->link('images', $images[0]);
I create an entry in the link table.
public function actionCreate()
{
$model = new Apartment();
$model->load(Yii::$app->getRequest()->getBodyParams(), '');
$layout = new Images();
$layout->imageFile = UploadedFile::getInstanceByName('layout');
$layout->validate();
if ($layout->upload()) {
$model->layout_id = $layout->id;
}
$count = count(UploadedFile::getInstancesByName('images'));//Get the number of images
$images = [new Images()];//First image required
$images[0]->imageFile = UploadedFile::getInstanceByName('images[0]');
if(!$images[0]->validate()) return $images[0]->errors;//Check errors
if ($model->validate()){
if ($model->save()) {
if ($images[0]->upload()) {
$model->link('images',$images[0]);//Binding many to many
}
for($i = 1; $i < $count; $i++) {//Check the rest and repeat again
$images[$i] = new Images();
$images[$i]->imageFile = UploadedFile::getInstanceByName('images['.$i.']');
if ($images[$i]->upload()) {
$model->link('images',$images[$i]);
}
}
$response = Yii::$app->getResponse();
$response->setStatusCode(201);
$id = implode(',', array_values($model->getPrimaryKey(true)));
$response->getHeaders()->set('Location', Url::toRoute(['view', 'id' => $id], true));
} elseif (!$model->hasErrors()) {
throw new ServerErrorHttpException('Failed to create the object for unknown reason.');
}
}
else return $model->errors;
return $model;
}
I use jenssegers/laravel-mongodb. I make scope
public function scopeWhereFullText($query, $search)
{
return $query->whereRaw(['$text' => ['$search' => $search]],['score'=>['$meta'=>'textScore']]);
}
How I can order By score field like in MongoDB js example:
db.products.find({$text:{$search:"SomeText"}},{score:{$meta:'textScore'}}).sort({score:{$meta:'textScore'}})
What is the solution without crutch:
public function scopeWhereFullText($query, $search)
{
$query->getQuery()->projections = ['score'=>['$meta'=>'textScore']];
return $query->whereRaw(['$text' => ['$search' => $search]]);
}
and in result
$products = Product::whereFullText($request->get('q',''))
->orderBy('score',['$meta'=>'textScore'])->get();
$max = $products->max('score');
$min = $products->min('score');
$products = $products->filter(function($item) use($max,$min){
return $item->score > ($max+$min)/2;
});
Use
$results = DB::connection()->collection('_User')->whereRaw(['$text' => ['$search' => 'SEARCH QUERY']])->project(['score'=>['$meta'=>'textScore']])->orderBy('score', ['$meta' => "textScore"])->limit(10)->get();
By using webmaster tool API to parse data from google. The speed of downloading is very slow, Is there any way to optimize, Or use another method. It can take more than hour depending on the data.
Regards.
const HOST = "https://www.google.com";
const SERVICEURI = "/webmasters/tools/";
public function __construct()
{
$this->_auth = $this->_loggedIn = $this->_domain = false;
$this->_data = array();
}
public function getArray($domain)
{
if ($this->_validateDomain($domain)) {
if ($this->_prepareData()) {
return $this->_data;
} else {
throw new Exception('Error receiving crawl issues for ' . $domain);
}
} else {
throw new Exception('The given domain is not connected to your Webmastertools account!');
exit;
}
}
public function getCsv($domain, $localPath = false)
{
if ($this->_validateDomain($domain)) {
if ($this->_prepareData()) {
if (!$localPath) {
$this->_HttpHeaderCSV();
$this->_outputCSV();
} else {
$this->_outputCSV($localPath);
}
} else {
throw new Exception('Error receiving crawl issues for ' . $domain);
}
} else {
throw new Exception('The given domain is not connected to your Webmastertools account!');
exit;
}
}
public function getSites()
{
if ($this->_loggedIn) {
$feed = $this->_getData('feeds/sites/');
if ($feed) {
$doc = new DOMDocument();
$doc->loadXML($feed);
$sites = array();
foreach ($doc->getElementsByTagName('entry') as $node) {
array_push($sites, $node->getElementsByTagName('title')->item(0)->nodeValue);
}
return (0 < sizeof($sites)) ? $sites : false;
} else {
return false;
}
} else {
return false;
}
}
public function login($mail, $pass)
{
$postRequest = array(
'accountType' => 'HOSTED_OR_GOOGLE',
'Email' => $mail,
'Passwd' => $pass,
'service' => "sitemaps",
'source' => "Google-WMTdownloadscript-0.11-php"
);
// Before PHP version 5.2.0 and when the first char of $pass is an # symbol,
// send data in CURLOPT_POSTFIELDS as urlencoded string.
if ('#' === (string) $pass[0] || version_compare(PHP_VERSION, '5.2.0') < 0) {
$postRequest = http_build_query($postRequest);
}
$ch = curl_init(self::HOST . '/accounts/ClientLogin');
curl_setopt_array($ch, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_CONNECTTIMEOUT => 30,
CURLOPT_SSL_VERIFYPEER => 0,
CURLOPT_FOLLOWLOCATION => 1,
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => $postRequest,
));
$output = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);
if (200 != $info['http_code']) {
throw new Exception('Login failed!');
exit;
} else {
#preg_match('/Auth=(.*)/', $output, $match);
if (isset($match[1])) {
$this->_auth = $match[1];
$this->_loggedIn = true;
return true;
} else {
throw new Exception('Login failed!');
exit;
}
}
}
private function _prepareData()
{
if ($this->_loggedIn) {
$currentIndex = 1;
$maxResults = 100;
$encUri = urlencode($this->_domain);
/*
* Get the total result count / result page count
*/
$feed = $this->_getData("feeds/{$encUri}/crawlissues?start-index=1&max-results=1");
if (!$feed) {
return false;
}
$doc = new DOMDocument();
$doc->loadXML($feed);
$totalResults = (int) $doc->getElementsByTagNameNS('http://a9.com/-/spec/opensearch/1.1/', 'totalResults')->item(0)->nodeValue;
$resultPages = (0 != $totalResults) ? ceil($totalResults / $maxResults) : false;
unset($feed, $doc);
if (!$resultPages) {
return false;
}
/*
* Paginate over issue feeds
*/
else {
// Csv data headline
$this->_data = Array(
Array(
'Issue Id',
'Crawl type',
'Issue type',
'Detail',
'URL',
'Date detected',
'Last detected'
)
);
while ($currentIndex <= $resultPages) {
$startIndex = ($maxResults * ($currentIndex - 1)) + 1;
$feed = $this->_getData("feeds/{$encUri}/crawlissues?start-index={$startIndex}&max-results={$maxResults}");
$doc = new DOMDocument();
$doc->loadXML($feed);
foreach ($doc->getElementsByTagName('entry') as $node) {
$issueId = str_replace(self::HOST . self::SERVICEURI . "feeds/{$encUri}/crawlissues/", '', $node->getElementsByTagName('id')->item(0)->nodeValue);
$crawlType = $node->getElementsByTagNameNS('http://schemas.google.com/webmasters/tools/2007', 'crawl-type')->item(0)->nodeValue;
$issueType = $node->getElementsByTagNameNS('http://schemas.google.com/webmasters/tools/2007', 'issue-type')->item(0)->nodeValue;
$detail = $node->getElementsByTagNameNS('http://schemas.google.com/webmasters/tools/2007', 'detail')->item(0)->nodeValue;
$url = $node->getElementsByTagNameNS('http://schemas.google.com/webmasters/tools/2007', 'url')->item(0)->nodeValue;
$dateDetected = date('d/m/Y', strtotime($node->getElementsByTagNameNS('http://schemas.google.com/webmasters/tools/2007', 'date-detected')->item(0)->nodeValue));
$updated = date('d/m/Y', strtotime($node->getElementsByTagName('updated')->item(0)->nodeValue));
// add issue data to results array
array_push($this->_data, Array(
$issueId,
$crawlType,
$issueType,
$detail,
$url,
$dateDetected,
$updated
));
}
unset($feed, $doc);
$currentIndex++;
}
return true;
}
} else {
return false;
}
}
private function _getData($url)
{
if ($this->_loggedIn) {
$header = array(
'Authorization: GoogleLogin auth=' . $this->_auth,
'GData-Version: 2'
);
$ch = curl_init(self::HOST . self::SERVICEURI . $url);
curl_setopt_array($ch, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_CONNECTTIMEOUT => 30,
CURLOPT_SSL_VERIFYPEER => 0,
CURLOPT_FOLLOWLOCATION => 1,
CURLOPT_ENCODING => 1,
CURLOPT_HTTPHEADER => $header
));
$result = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);
return (200 != $info['http_code']) ? false : $result;
} else {
return false;
}
}
private function _HttpHeaderCSV()
{
header('Content-type: text/csv; charset=utf-8');
header('Content-disposition: attachment; filename=gwt-crawlerrors-' . $this->_getFilename());
header('Pragma: no-cache');
header('Expires: 0');
}
private function _outputCSV($localPath = false)
{
$outstream = !$localPath ? 'php://output' : $localPath . DIRECTORY_SEPARATOR . $this->_getFilename();
$outstream = fopen($outstream, "w");
if (!function_exists('__outputCSV')) {
function __outputCSV(&$vals, $key, $filehandler)
{
fputcsv($filehandler, $vals); // add parameters if you want
}
}
array_walk($this->_data, "__outputCSV", $outstream);
fclose($outstream);
}
private function _getFilename()
{
return 'gwt-crawlerrors-' . parse_url($this->_domain, PHP_URL_HOST) . '-' . date('Ymd-His') . '.csv';
}
private function _validateDomain($domain)
{
if (!filter_var($domain, FILTER_VALIDATE_URL)) {
return false;
}
$sites = $this->getSites();
if (!$sites) {
return false;
}
foreach ($sites as $url) {
if (parse_url($domain, PHP_URL_HOST) == parse_url($url, PHP_URL_HOST)) {
$this->_domain = $domain;
return true;
}
}
return false;
}
The url from Google is in error Google has added a false extension look carefully and you will see it.
Google Crap!
Your site is probably fine.
I have here many-to-many table, i need to get just inserted id - BlogId. lastInsertId() is NULL result. Any ideas?
$table = new Blog_Model_Blog_Table();
$relTable = new Blog_Model_Relation_Table();
$Object = $table->createRow();
$form = new Blog_Form_Blog_Add($Object);
if ($this->getRequest()->isPost() and $form->isValid($_POST)) {
Blog_Model_Blog_Manager::add($Object);
$blogId = $table->getAdapter()->lastInsertId();
foreach ($_POST['category_id'] as $value) {
$relTable->insert(array('id' => $blogId, 'category_id' => $value));
}
Blog_Model_Blog_Manager:
class Blog_Model_Blog_Manager
{
static function add(Blog_Model_Blog_Item &$Object)
{
$data = array(
'time_add' => time(),
'time_edit' => time(),
'url_keyword' => Ap_Filter_Translit::asURLSegment($Object->name)
);
$Object->setFromArray($data)
->save();
}
I make it with mySql function LAST_INSERT_ID():
$blogId = $table->fetchRow($table->select()->from($table)->columns('LAST_INSERT_ID() as idi'));