The first code block is a bit of my account model.
I'm not using query builder because model relationships are easier to define, debug, and build.
Account hasOne Region. That part is correct.
The serialization is happening when I call
$response->setJsonContent($account); //serializes the Account object model into JSON
In the 2.0 version, I didn't use the Phalcon Response object. I simply did this:
echo json_encode($account);
die();
This approach doesn't work the same in 3.0 either.
What I'm really after is an easy way to return attributes that are not necessarily part of the model along with the json data to the UI. Right now, this data is related models and alternate date formats. It will eventually include calculated values and other things. Here is my entire account model, if that helps:
<?php
namespace Zipline\Models;
use Phalcon\Mvc\Model;
use Phalcon\DI;
use Phalcon\Mvc\Model\Behavior\SoftDelete;
class Account extends Model {
protected $api_key;
protected $private_key;
protected $rpp;
public $sdk_account;
public $region;
public function initialize() {
$this->setSource("accounts");
$this->addBehavior(new SoftDelete([
'field' => 'deleted',
'value' => gmdate('Y-m-d H:i:s')
]));
$this->hasOne('region_id','Zipline\\Models\\Region','code',['alias'=>'region']);
}
public function afterFetch() {
$this->region = $this->region;
$this->sdk_account = $this->getSDKAccount();
$this->created = $this->getCreated();
$this->updated = $this->getUpdated();
}
public function beforeValidationOnCreate() {
$this->created = gmdate('Y-m-d H:i:s');
$this->updated = gmdate('Y-m-d H:i:s');
}
public function beforeValidationOnUpdate() {
$this->updated = gmdate('Y-m-d H:i:s');
}
public function getCreated() {
return gmdate('D M d Y H:i:s O',strtotime($this->created));
}
public function getUpdated() {
return gmdate('D M d Y H:i:s O',strtotime($this->updated));
}
public function createAccount($request) {
if ($sdk_account = $this->createSDKAccount($request)) {
$this->region_id = $request->region;
$this->sdk_id = $sdk_account->id;
$this->company_name = $request->company_name;
$this->api_key = $sdk_account->apiKey;
$this->private_key = $sdk_account->privateKey;
if ($this->save()) {
$account = Account::findFirst([
"conditions" => "id = :id:",
"bind" => [
"id" => $this->id
]
]);
$session = $this->getDi()->getShared('session');
//save the account in session so we can capture billing information, if needed, without logging the user in
//used by the billing models and PayPal library.
$session->set('signup_account',$account->toArray());
return $account;
}
}
return false;
}
private function createSDKAccount($request) {
$sdk = $this->getDi()->getShared("sdk");
$params = [
"name" => $request->company_name,
"company" => $request->company_name,
"contact" => $request->first_name." ".$request->last_name,
"email" => $request->username,
"url" => "https://",
"region" => $request->region,
"type" => "full",
"level" => $request->subscription
];
$sdk_response = $sdk->get("Create/Account",$params);
if ($sdk_response->result != "success") {
$this->appendMessage(new Message($sdk_response->message));
return false;
}
return $sdk_response->account;
}
protected function getSDKAccount() {
$session = $this->getDi()->getShared('session');
$account = $session->get('sdk_account'.$this->id);
if (!empty($account)) {
return $account;
}
$sdk = $this->getDi()->getShared('sdk');
$params = [
"id" => $this->sdk_id
];
$sdk_response = $sdk->get("View/Account",$params);
if ($sdk_response->result != "success") {
$this->appendMessage(new Message($sdk_response->message));
return false;
}
unset($sdk_response->account->apiKey);
unset($sdk_response->account->privateKey);
$session->set('sdk_account'.$this->id,$sdk_response->account);
return $sdk_response->account;
}
}