0

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.

Randy Hall
  • 7,716
  • 16
  • 73
  • 151

1 Answers1

1

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))))
scrowler
  • 24,273
  • 9
  • 60
  • 92
  • You're probably correct, however, I'm still getting "Parse error: syntax error, unexpected T_VARIABLE ... on line 3". It's exactly what I show up top, the entire file (tried without extra "Transaction->") – Randy Hall Dec 08 '13 at 20:10
  • 1
    I see - chances are those properties are being defined too early to be able to do any calls. You could try what this part of the manual suggests and define these in your constructor: http://book.cakephp.org/2.0/en/models/virtual-fields.html#virtual-fields-and-model-aliases – scrowler Dec 08 '13 at 20:15
  • New issue if you want to take a stab at it: http://stackoverflow.com/questions/20459606/virtualfield-find-places-array-in-mysql-statement – Randy Hall Dec 08 '13 at 21:53