Skip to content

Instantly share code, notes, and snippets.

@sanhuang
Created August 21, 2017 05:31
Show Gist options
  • Save sanhuang/6eff90d8dd94373a41a704ee7414159d to your computer and use it in GitHub Desktop.
Save sanhuang/6eff90d8dd94373a41a704ee7414159d to your computer and use it in GitHub Desktop.
SharesGarevenue.php many to one belongto Shares.php,
<?php
namespace Pubshare\Models;
/**
* Shares
*
* @package Personalwork\Mvc\Model
* @autogenerated by Phalcon Developer Tools
* @date 2017-07-20, 13:32:19
*/
class Shares extends \Personalwork\Mvc\Model
{
/**
*/
public $shareId;
/**
*/
public $PeopleId;
/**
*/
public $tid;
/**
*/
public $title;
/**
*/
public $setTime;
/**
*/
public $setDate;
/**
*/
public $account;
/**
* Initialize method for model.
*/
public function initialize()
{
$this->hasMany('shareId', "\\Pubshare\\Models\\SharesGarevenue", 'ShareId', array('alias' => 'SharesGarevenue'));
$this->belongsTo('PeopleId', "\\Pubshare\\Models\\People", 'peopleId', array('alias' => 'People'));
}
/**
* Returns table name mapped in the model.
*
* @return string
*/
public function getSource()
{
return 'shares';
}
/**
* 先嘗試從shares內抓取即時數據(今日與昨日)
* act1. 若無,則透過GaApi抓全部數據寫入並(寫入cache內設定30分)
* */
public static function findwithFetch( $member )
{
// 處理cache部分
/**
* 處理使用GA API取得該帳號的GA數據!
* 使用Google Console產生的Json檔案
* */
// 數據
$shares = array();
foreach ($res->getRows() as $i => $row) {
if( !preg_match("/from={$member['account']}$/um", $row[0]) ){
continue;
}
$url=explode('-', $row[0]);
if( count($url) < 4){
// var_dump($url);
// var_dump($row);
continue ;
}
$share_record=\Pubshare\Models\Shares::findFirst(["title=:title: AND setDate=:day: AND PeopleId=:pid:",
'bind'=> [
'title' => $row[1],
'day' => date('Y-m-d',strtotime($row[2])) ,
'pid' => $member['peopleId'],
]
]);
if( empty($share_record) ){
$share_record= new Shares();
$share_record->PeopleId = $member['peopleId'];
$share_record->account = $member['account'];
$share_record->tid = $url[1];
$share_record->title = $row[1];
$share_record->setDate = $row[2];
}
$share_record->setTime = time();
$share_record->pageview = $row[3];
if( !$share_record->save() ){
$msg = "寫入即時數據發生錯誤,訊息:".implode(",", $share_record->getMessages());
die( $msg );
return ;
}
}
/**
* $Shares是來自API產生的紀錄,會有多筆對應關係
* 原本期望的做法是在Action內,直接foreach對每一筆$share儲存後在處理
* */
$Shares = new \Pubshare\Models\Shares();
foreach ($Shares as $i => $share) {
$SharesGarevenues[] = new \Pubshare\Models\SharesGarevenue();
// 這裡的邏輯只是示意,不是問題點
$SharesGarevenues->key = 'xxx'.$i;
$SharesGarevenues->value = $i;
$Shares->SharesGarevenue = $SharesGarevenues;
}
if( !$Shares->save() ){
// process error!
}
$rows = self::find(["account='{$member['account']}'", "order"=>"setDate DESC"]);
return $rows;
}
/**
* Allows to query a set of records that match the specified conditions
*
* @param mixed $parameters
* @return Shares[]
*/
public static function find($parameters = null)
{
return parent::find($parameters);
}
/**
* Allows to query the first record that match the specified conditions
*
* @param mixed $parameters
* @return Shares
*/
public static function findFirst($parameters = null)
{
return parent::findFirst($parameters);
}
}
<?php
namespace Pubshare\Models;
/**
* SharesGarevenue
*
* @package Personalwork\Mvc\Model
* @autogenerated by Phalcon Developer Tools
* @date 2017-07-20, 13:32:31
*/
class SharesGarevenue extends \Personalwork\Mvc\Model
{
/**
* @Comment("主鍵")
*
* @var integer
*/
public $pgId;
/**
*/
public $ShareId;
/**
*/
public $pageview;
/**
*/
public $date;
/**
*/
public $ts;
/**
*/
public $revenue;
/**
* Initialize method for model.
*/
public function initialize()
{
$this->belongsTo('ShareId', "\\Pubshare\\Models\\Shares", 'shareId', array('alias' => 'Shares'));
}
/**
* Returns table name mapped in the model.
*
* @return string
*/
public function getSource()
{
return 'shares_garevenue';
}
/**
* Allows to query a set of records that match the specified conditions
*
* @param mixed $parameters
* @return SharesGarevenue[]
*/
public static function find($parameters = null)
{
return parent::find($parameters);
}
/**
* Allows to query the first record that match the specified conditions
*
* @param mixed $parameters
* @return SharesGarevenue
*/
public static function findFirst($parameters = null)
{
return parent::findFirst($parameters);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment