How to get the value of a explicitly selected column of a CDbCriteria

Hi, I want to select additional columns in a CDbCriteria, like this:


$events = new CActiveDataProvider('events', array(

  'criteria' => array(

    'select' => array( '*', 'UNIX_TIMESTAMP( t.when ) AS when_ts' ),

    'condition'=> Blah, etc... ) );

How can I read when_ts?

$events[0]->when_ts doesn’t work

Have you added a holder for when_ts to your Events model? i.e.,




class Events extends CActiveRecord{

   public $when_ts;

}



When the query pulls the data, it should assign it to the property you created and allow you to access it.

Thanks dana!

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 *.

There is an example in this section of the guide

http://www.yiiframework.com/doc/guide/1.1/en/database.ar#reading-record

You can use the implicit attribute names in select unless you need to select DB-specific functions.

/Tommy

Sorry Tommy, I can’t find anything related to my question in there, can you be more specific?

What are implicit attribute names?

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.

(not tested)

/Tommy

I’ve used this before to good effect, looking for an example that I can share now, will post it as soon as I locate it ;)

If you don’t wish to add more variables to your models, why don’t you work with CDbCommands?

ie.

$data=Yii::app()->db->createCommand(‘SELECT * FROM blahblah’)->queryAll();

After that you can access the $data and even use CArrayDataProvider if you wish to use it with CGridView or CListView…

Just a suggestion.

Best

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



Thanks Dana! it worked.

I was using an array for the select property, instead of a comma separated string.

And also wasn’t referencing t.* but just *

Thanks!

hi Dana,

Your reply on bellow post was very helpful for me. Thank you :)

instead of defining fields in models for every find request. I will prefer @Antonio Ramirez suggession.

Thanks antonio!