I have been following the book provided by the agile toolkit and am at this portion: http://agiletoolkit.org/learn/app/logic
Unfortunately the dsql api lacks some clarity and the calculated fields example on that page is just not working.
I tried using:
class Model_DVD extends Model_Table {
public $table='dvd';
function init(){
parent::init();
$this->hasOne('Movie');
$this->addField('code');
$this->addfield('is_rented')
->type('boolean')
->calculated(true);
}
function calculate_is_rented(){
return $this->add('Model_Rental')
->dsql()
->field('id')
->where('rental.dvd_id=dvd.id')
->where('is_returned!=','Y')
->select()
;
}
}
This returns 1 every time so I tried to follow: Calculated field always returns 1 - atk 4.2
And now I'm not sure how to create this properly; I know the following works but I kind of made it up as I went and have NO IDEA why it works, and why I need the count() - etc etc.
How do I go about getting the calculated field to work? Can someone please explain it in this example, I learn best by examples so I wanted to follow through with it.
My working code that I don't understand:
class Model_DVD extends Model_Table {
public $table='dvd';
function init(){
parent::init();
$this->hasOne('Movie');
$this->addField('code');
$this->debug();
$this->addExpression('is_rented')->set($this->add('Model_Rental')
->addCondition('dvd_id', $this->_dsql()->getField('id'))
->count()
->where('is_returned!=','Y'))
->datatype('boolean')
->enum(array('Y', 'N'));
}
}
Thanks!