在cakephp3.2中使用SQL函数ORM查询构建器不起作用

时间:2016-08-26 19:31:04

标签: cakephp cakephp-3.2

我正在尝试通过查询构建器运行查询。所以查询正在运行,但结果不正确。

主要问题是当我使用min()函数时,我得不到正确的结果,甚至我试图单独运行firstDue查询,但仍然没有结果。 当我删除min()功能时,我会收到所有付款分期付款。

(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'

},

我的桌子关联

订单表

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'
            ]
        ]
    ]);
}

交易表

class TransactionsTable extends Table
{

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

PaymentInstalments表

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;
    }

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

我想获得他们transaction_id IS NULL的所有分期付款。 这是我在控制器中的主要查询:

    $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();

我认为问题可能是因为isNull()函数,但这并不是我尝试手动编写条件的原因。

注意:此查询正在使用cakephp-3.1,因为我们已将应用程序更新到最新版本的cakephp-3.2,所以发生了这个问题。

请帮助。

0 个答案:

没有答案