PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, January 16, 2022

[FIXED] CakePHP 3: add extra field to access from template

 January 16, 2022     cakephp, cakephp-3.x     No comments   

Issue

I'm using CakePHP 3.6

I have two models Users and WalletTransactions.

WalletTransactions is associated with Users with user_id and have a column price.

I am using following code to show the list of users

UsersController.php

public function index()
{
    $users = $this->Users->find();

    $this->set(compact('users'));
}

users/index.ctp

<?php foreach($users as $user): ?>

    <?= $user->name ?>

<?php endforeach; ?>

Now, I want to show the sum of price from WalletTransactions inside the list of users in the template.

For that, I have written a function to get the sum of price in WalletTransactionsTable.php

public function walletBalance($user_id)
{
    $total_balance = 0;

    $walletTransactions = $this->find()
        ->where([
            'user_id' => $user_id
        ]);

    foreach ($walletTransactions as $transaction) {
        $total_balance += $transaction->price;
    }

    return $total_balance;
}

and set a property function in User.php entity class

protected function _balance()
{
    $user_id = $this->_properties['id'];

    $WalletTransactions = TableRegistry::get('WalletTransactions');

    $transaction = $WalletTransactions->walletBalance($user_id);

    return $transaction;
}

in the template in the users loop when using

<?= $user->balance ?>

it prints nothing.

How can I show values from other models inside a template?


Solution

Accessors must start with _get, ie _getBalance. However, ideally entities should be stupid data containers, making them issue queries isn't the best idea, not to mention that it's rather inefficient to issue a query everytime a property is being accessed.

I'd suggest to use for example a custom finder. Also there's no need to retrieve all transactions and calculate on PHP level, this can be easily done on SQL level instead:

// UsersTable

public function findWithWalletBalance(\Cake\ORM\Query $query, array $options)
{
    return $query
        ->leftJoinWith($this->WalletTransactions->getName())
        ->select([
            'balance' => $query->func()->sum($this->WalletTransactions->aliasField('price'))
        ])
        ->group(array_map([$this, 'aliasField'], (array)$this->getPrimaryKey()))
        ->enableAutoFields(true);
}
// UsersController

public function index()
{
    $users = $this->Users->find('withWalletBalance');

    $this->set(compact('users'));
}

See also

  • Cookbook > Database Access & ORM > Entities > Accessors & Mutators
  • Cookbook > Database Access & ORM > Retrieving Data & Results Sets > Custom Finder Methods
  • Cookbook > Database Access & ORM > Retrieving Data & Results Sets > Using leftJoinWith


Answered By - ndm
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing