CakePHP virtualField find all not null

2019-08-24 03:12发布

I have a database table "transactions" which has a field "account". I want to retrieve a subset of all not-null account rows from the current set of data and have it as a virtualField I can access down the line in my view.

class Transaction extends AppModel {
    public $virtualFields = array(
        "Accounts" => $this->Transaction->find("all", array("conditions" => array("not" => array("Transaction.account" => null))))
    );
}

So that I get an array of all transactions with non-null account fields named "Accounts".

This doesn't work - gives me "unexpected T_VARIABLE" error (doesn't like $this). I was trying to follow the guide here. I'm a moderate level PHP developer and this is my first real Cake project, so I may be going about this completely wrong.

1条回答
▲ chillily
2楼-- · 2019-08-24 04:01

When you're inside the model that you're querying, you don't specify the model name, just:

$this->find('all'); // when you're inside transaction model

...so try this:

"Accounts" => $this->find("all", array("conditions" => array("not" => array("Transaction.account" => null))))
查看更多
登录 后发表回答