laravel 4 output json order desc - json

I use laravel 4.2
How can I output json in desc order? Thanks.
Below is my code :
public function show($id) {
$member = $this->getMember ();
$transition = $member->getTransition ( $id )->first ();
$transition_info = $transition-> transitionInfo;
return ResponseWrapper::toJson ( $transition_info );
}
...
public function index() {
$member = $this->getMember ();
$transitions = $member->getTransitions ();
return ResponseWrapper::toJson ( $transitions );
}
/* 12/28 update */
Maybe I should change in below model?
(project/app/models/Member.php)
public function getTransitions()
{
$array = $this->hasMany('Transition', 'payeer_id', 'id')->select($this->transition_index_payeer_column)->get()->all();
$arrayb = $this->hasMany('Transition', 'remitter_id', 'id')->select($this->transition_index_remitter_column)->whereRaw('NOT (card_type_remitter = "focas" and focas_status = "")')->get()->all();
$reuslt = array_merge ( $array, $arrayb );
return $reuslt;
}

You should try this:
public function index() {
$member = $this->getMember ();
$transitions = $member->orderBy('id','desc')->getTransitions ();
return ResponseWrapper::toJson ( $transitions );
}

Try this one,
public function index() {
$member = $this->getMember ();
$transitions = $member->getTransitions()->orderBy('id','desc')->get();
return ResponseWrapper::toJson ( $transitions );
}

For Laravel V4 :
$transitions = $member::orderBy('id', 'DESC')->getTransitions();

Related

User registration in yii2?

I am getting the following exception
PHP Fatal Error – yii\base\ErrorException
Maximum execution time of 60 seconds exceeded !!!!!!!
public $password;
public function generateAuthKey()
{
return $this->auth_key = Yii::$app->security->generateRandomString();
}
public function setPassword($password)
{
return $this->password_hash = Yii::$app->security->generatePasswordHash($password);
}
public function save($runValidation = true, $attributeNames = null)
{
$user = new Users();
$user->username = $this->username;
$user->email = $this->email;
$user->setPassword($this->password);
$user->generateAuthKey();
if ($user->save()) {
$this->id = $user->id;
return true;
}
}
in your User model you should canghe the function name save() with another name eg saveUser so this is not recalled by the inner $user->save() that is a normal ActiveRecord function for save.
Calling User->save() as you are doing you produce an infinite nested loop ..
.....
public function saveUser($runValidation = true, $attributeNames = null)
{
$user = new Users();
$user->username = $this->username;
$user->email = $this->email;
$user->setPassword($this->password);
$user->generateAuthKey();
if ($user->save()) {
$this->id = $user->id;
return true;
}
}
...
In your controller Action eg:action Create you should call your $user->saveUser(...)
*/
public function actionCreateForCatone2()
{
....
$user->saveUser(...);
...
}

How to add a fuction in a model yii2

I have modified my signup form, I add a field that called billCode, so I want add a function to my signForm model to random integer number, I have a function to random integr and I put that into signup form model like this
class SignupForm extends Model {
// ...
// ...
public static function randomNumber()
{
$randBill = '';
for ($i=0; $i < 8; $i++){
$randBill .= mt_rand(0, 9);
}
return $randBill;
}
public function signup()
{
if (!$this->validate()) {
return null;
}
$user = new User();
$user->username = $this->username;
$user->email = $this->email;
$user->setPassword($this->password);
$user->generateAuthKey();
$user->billCode = randomNumber();
return $user->save() ? $user : null;
}
}
when I press the submit button in signUp Form, yii2 keep give me this error
Call to undefined function frontend\models\randomNumber()
Any help?
Copy from comment
randomNumber is a static function, you can't call it just by randomNumber(). Using self::randomNumber() could be work.
You can call it by self keyword
class SignupForm extends Model {
// ...
// ...
public static function randomNumber()
{
$randBill = '';
for ($i=0; $i < 8; $i++){
$randBill .= mt_rand(0, 9);
}
return $randBill;
}
public function signup()
{
if (!$this->validate()) {
return null;
}
$user = new User();
$user->username = $this->username;
$user->email = $this->email;
$user->setPassword($this->password);
$user->generateAuthKey();
//$user->billCode = $this->randomNumber();
$user->billCode = self::randomNumber();
return $user->save() ? $user : null;
}
}

Laravel saves child record, but sets foreign key to null

This has got to be a simple fix, as I have done this many times before. But as it stands I am completely stumped. I use the following code to save a parent object Unknown_Tag and its many children.
Method:
public function saveUnknown(Request $request)
{
$url = $request->url;
$tag = new Unknown_Tag();
$tag->url = $url;
$protocol =
substr($url, 0, strpos($url, ':'));
$tag->protocol = $protocol;
$domain =
parse_url($url, PHP_URL_HOST);
$tag->domain = $domain;
$tag->save();
//get the path
$Path = parse_url($url, PHP_URL_PATH);
if ($Path) {
$splitPath = substr($Path, 1);
$paths = explode('/', $splitPath);
foreach ($paths as $p) {
$path = new Path();
$path->path = $p;
$tag->Paths()->save($path);
}
}
//get Queries
$splitQuery = parse_url($url, PHP_URL_QUERY);
if ($splitQuery) {
$queries = explode('&', $splitQuery);
foreach ($queries as $q) {
$query = new Query();
$q = substr($q, 0, strpos($q, '='));
IF (SUBSTR($q, -1) != ' ') {
$q .= ' ';
}
$query->var = $q;
$value = $q = preg_replace('/^[^=]*:/', '', $q);
$query->value = $value;
$tag->Queries()->save($query);
}
}
}
The Parent Object
class Unknown_Tag extends Model
{
protected $table = 'unknown_tags';
public $timestamps = false;
public function Paths()
{
return $this->hasMany('App\Path', 'tag_id', 'ID');
}
public function Queries()
{
return $this->hasMany('App\Query', 'tag_id', 'ID');
}
}
The Child objects
class Query extends Model
{
protected $table = 'queries';
public $timestamps = false;
public function Tag()
{
return $this->belongsTo('App\Unknown_Tag', 'tag_id', 'ID');
}
}
class Path extends Model
{
protected $table = 'paths';
public $timestamps = false;
public function Tag()
{
return $this->belongsTo('App\Unknown_Tag', 'tag_id', 'ID');
}
}
When I run all this via a post request, The Parent and all the children are saved properly, but all the child objects have a foreign key that is set to null. If I manually change the foreign key to what it should be, everything works just fine, so I am fairly sure this is not a problem with my database. Can anyone see the obvious that I am missing here?
EDIT:Just to be clear, this returns no errors
If anyone ever sees this, laravel assumes the default primary key is 'id'. I had set mine to 'ID', so I had to let laravel know by using
protected $primaryKey = 'ID';
in my Unknown_tag definition

Dispaly records from database in magento

I am using this code in my Grid.php to display records from a single tabel 'paypal_payment_transaction':
protected function _prepareCollection()
{
$db = Mage::getSingleton('core/resource')->getConnection('core_write');
$query = "SELECT transaction_id,txn_id,additional_information,created_at,user_id,reference_txn
FROM `paypal_payment_transaction`
LIMIT 0 , 30";
$result = $db->query($query);
// Get count of affected rows
$affected_rows = $result->rowCount();
$orders = $result->fetchAll($sql);
foreach($orders as $order)
{
echo "<pre>"; print_r($order);
}
}
I need this query in magento way:
may be like this
protected function _prepareCollection()
{
$collection = Mage::getResourceModel('sales/order_collection')
SOME QUERY TO SELECT RECORD FROM TABEL 'paypal_payment_transaction'
;
$this->setCollection($collection);
parent::_prepareCollection();
return $this;
}
So that I can display it in grid accordingly i.e. in :
protected function _prepareColumns()
{
-------------------
------------------
}
Try this -
protected function _prepareCollection()
{
$collection = Mage::getModel('paypal/payment_transaction')->getCollection();
$this->setCollection($collection);
parent::_prepareCollection();
return $this;
}
you can try the following code
protected function _prepareCollection()
{
$collection = Mage::getResourceModel('payment/transaction')
$Collection->addFieldToSelect('transaction_id');
$this->setCollection($collection);
parent::_prepareCollection();
return $this;
}
Thanks

Codeigniter Select JSON, Insert JSON

I have very simple users database: user_id, user_name, user_email
My model this:
class Users extends CI_Model {
private $table;
private $table_fields;
private $table_fields_join;
function __construct() {
parent::__construct();
$this->table = 'users';
$this->table_fields = array(
$this->table.'.user_id',
$this->table.'.user_name',
$this->table.'.user_email'
);
$this->table_fields_join = array();
}
function select(){
$this->db->select(implode(', ', array_merge($this->table_fields, $this->table_fields_join)));
$this->db->from($this->table);
$query = $this->db->get();
if($query->num_rows() > 0){
return $query->result();
} else {
return false;
}
}
function insert($data) {
$data = array(
'user_name' => $data['user_name'],
'user_email' => $data['user_email']
);
$this->db->insert($this->table, $data);
}
My controller this:
class Users extends CI_Controller {
function __construct(){
parent::__construct();
$this->load->model('users');
}
public function select(){
$data['query'] = $this->users->select();
$data = json_encode($data['query']);
echo $data;
}
public function insert($json){
$data = json_decode($json);
$this->users->insert($data);
}
}
And this is my routing.php:
$route['default_controller'] = 'Welcome';
$route['users'] = 'users/select';
$route['users/insert/:(any)'] = 'users/insert';
I would like that 127.0.0.1/users/select give json.
Example: [{"user_name":"user1","user_email":"user#user.de"}]
This JSON insert my table: 127.0.0.1/users/insert/[{"user_name":"user1","user_email":"user#user.de"}]
But my code is not working. :-(
You want to return json object in response, so it's required to set json type in response header. As given here
public function select(){
$data['query'] = $this->users->select();
$this->output
->set_content_type('application/json')
->set_output(json_encode($data['query']));
}
It is required to encode part as below for insert part. so you can use this generated url to call your insert.
site_url('usres/insert/'.urlencode('[{"user_name":"user1","user_email":"user#user.de"}]'));
your insert route should be as
$route['users/insert/:(any)'] = 'users/insert/$1';
your insert method should be updated as
public function insert($json){
$json = urldecode($json);
$data = json_decode($json);
$this->users->insert($data);
}
}