0

I am trying to run a query through the query builder . So the query is running but I get incorrect result.

The main problem is when I use the min() function I don't get correct result and even I tried to run the firstDue query alone but still there is no result. How ever when I remove the min() function I get all payment instalments.

(int) 2 => object(App\Model\Entity\Order) {

    'order_id' => '10000',
    'final_price' => (int) 775,
    'payment_status_id' => (int) 2,
    'status2' => (int) 1,
    'client_deadline_orig' => object(Cake\I18n\FrozenTime) {

        'time' => '2016-09-05T14:08:40+01:00',
        'timezone' => 'Europe/London',
        'fixedNowTime' => false

    },
    'payment_instalments' => [], // The payment instalments is empty 
    'customer_files' => [],
    'order_master' => object(Cake\ORM\Entity) {

        'order_id' => '10000',
        'subj1' => 'Mathematical & Theoretical Physics',
        '[new]' => false,
        '[accessible]' => [
            '*' => true
        ],
        '[dirty]' => [],
        '[original]' => [],
        '[virtual]' => [],
        '[errors]' => [],
        '[invalid]' => [],
        '[repository]' => 'OrderMasters'

    },
    'subj1' => 'Mathematical & Theoretical Physics',
    '[new]' => false,
    '[accessible]' => [
        '*' => true
    ],
    '[dirty]' => [
        'subj1' => true
    ],
    '[original]' => [],
    '[virtual]' => [],
    '[errors]' => [],
    '[invalid]' => [],
    '[repository]' => 'Orders'

},

My Tables Associations

Orders Table

class OrdersTable extends Table
{
public function initialize(array $config)
{
    $this->table('orders');
    $this->primaryKey('order_id');

    // Relationships
    $this->addAssociations([
        'belongsTo' => [
            'Users' => [
                'foreignKey' => 'customer_id'
            ]
        ],
        'hasMany' => [
            'PaymentInstalments' => [
                'foreignKey' => 'order_id'
            ]
        ]
    ]);
}

Transactions Table

class TransactionsTable extends Table
{

public function initialize(array $config)
{
    // Relationships
    $this->addAssociations([
        'belongsTo' => [
            'Orders'
        ],
        'hasOne' => [
            'PaymentInstalments'
        ]
    ]);
}

PaymentInstalments Table

class PaymentInstalments extends Table
{
public function initialize(array $config)
{
    // Relationships
    $this->belongsTo('Orders');
    $this->belongsTo('Transactions');
}

public function findFirstDue(Query $query, array $options)
    {
        $alias = $this->alias();

        $query
            ->select([
                'id' => "MIN($alias.id)",
                'order_id', 'amount', 'date_due', 'transaction_id'
            ])
//          ->where(function ($exp, $q) use ($alias) {
//              return $exp->isNull('transaction_id');
//          });
            ->where(["$alias.transaction_id IS NULL"]);

        return $query;
    }

===============================================================

I want to get all the instalments that their transaction_id IS NULL. Here is my main query in my controller:

    $this->loadModel('Orders');
    $orders = $this->Orders
        ->select([
            'order_id', 'final_price', 'payment_status_id', 'status2',
            'client_deadline_orig'
        ])
        ->contain([
            'PaymentInstalments' => function($q) {
                return $q->find('firstDue');
            }
        ])
        ->where(['Orders.status2 !=' => 7])
        ->all();

I thought the problem might be because of isNull() function but it is not that's why I try to write the condition manually.

Note: This query was working on cakephp-3.1 and since we have updated our application to the latest version of cakephp-3.2 this problem happened.

Any help please.

Fury
  • 4,643
  • 5
  • 50
  • 80
  • 1
    Please don't repost questions to make up for downvotes, instead try to make the question better. "_Doesn't work_" usually isn't a helpful problem description. There are many unknown here... like, what type of is the association? Are you aware of how an aggregate function like `MIN()` affects an SQL query? Have you debugged the generated query? Does it look like what you'd expect? What _do_ you expect the query to look like? And what exactly is it that you expect to retrieve for what existing data? – ndm Aug 26 '16 at 19:39
  • Thank you @ndm. I had to do it because I haven't seen any reason why it's been downvoted. I believe this problem happened since we have upgraded our cakephp(3.1) to 3.2 – Fury Aug 26 '16 at 21:50

0 Answers0