But instead of using holders for each query, isn’t there a simpler solution?
Edit: anyway, tried the holder, didn’t work
I wonder why the Yii documentation doesn’t have any code samples, it would be so much easier seeing how they manage to get a value from the select clause of a CDbCriteria, other than *.
Normally the columns names from DB will become attributes (properties of an AR class object), thus my use of the word implicit attribute names, correction identifiers. I guess your question was about a specific case and I believe Dana’s suggestion should work.
Just to be clear, this isn’t to store the query, but to store the additional value that’s been selected (AS when_ts).
If you dig into the ActiveRecord code, you can see how it decides where to populate the results of the query:
/**
* Returns the named attribute value.
* If this is a new record and the attribute is not set before,
* the default column value will be returned.
* If this record is the result of a query and the attribute is not loaded,
* null will be returned.
* You may also use $this->AttributeName to obtain the attribute value.
* @param string the attribute name
* @return mixed the attribute value. Null if the attribute is not set or does not exist.
* @see hasAttribute
*/
public function getAttribute($name)
{
if(property_exists($this,$name))
return $this->$name;
else if(isset($this->_attributes[$name]))
return $this->_attributes[$name];
}
/**
* Sets the named attribute value.
* You may also use $this->AttributeName to set the attribute value.
* @param string the attribute name
* @param mixed the attribute value.
* @return boolean whether the attribute exists and the assignment is conducted successfully
* @see hasAttribute
*/
public function setAttribute($name,$value)
{
if(property_exists($this,$name))
$this->$name=$value;
else if(isset($this->getMetaData()->columns[$name]))
$this->_attributes[$name]=$value;
else
return false;
return true;
}
So, as long as you’ve set the property on the object, it will populate that before looking at the generated array of attributes.
I think the problem you’re running into is with your selection criteria.
Just realized your CDbCriteria is a little off of what you want it to be –
Have you tried structuring it this way?
In the events model:
class Events extends CActiveRecord{
public $when_ts;
...
}
In the controller:
$criteria = new CDbCriteria;
$criteria->select = "t.*, UNIX_TIMESTAMP(t.when) as when_ts";
$criteria->condition = "your condition here";
$events = new CActiveDataProvider('events', array( 'criteria' => $criteria ) );