Yii Framework Forum: [Solved] Active records Custom culumn - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • This topic is locked

[Solved] Active records Custom culumn it is possible? Rate Topic: -----

#1 User is offline   R.K. 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 117
  • Joined: 20-September 10

Posted 22 October 2010 - 08:01 AM

It is possible get custom database column from active record. ex:.

sql:

SELECT CONCAT(Name, LastName) as FullName FROM myTable



ActiveRecord;

$result = $myTable::model()->findAll(array('select'=>'CONCAT(Name, " ", LastName) as FullName'));

foreach ($result as $result) {
    	echo $result->FullName;
}


0

#2 User is offline   zaccaria 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 2,232
  • Joined: 04-October 09
  • Location:Moscow

Posted 22 October 2010 - 08:12 AM

Is possible, you have just to add a property to the model class.

class myTable extends CActiveRecord
{
   public $FullName;

   [...]



This will work fine. Anyway you can always write a getter method instead, in order to avoid to use the concat (it has not the same syintax on each DBMS):

In the class mytable you can do:

public function getFullName()
{
    return $this->Name.' '.$this->LastName;
}


I prefer the second method, in order to leave all business rule in the model (is not required to set the select statment).
0

#3 User is offline   R.K. 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 117
  • Joined: 20-September 10

Posted 22 October 2010 - 08:19 AM

the think is that i need get binary set of column.

this was simplified example of it.

http://dev.mysql.com...t-datatype.html

i want get bin set of my column

mysql> SELECT rowid, myset, LPAD(BIN(myset+0),4,'0') AS binset FROM set_test;

the result is .

+-------+-----------------------------------+--------------+
| rowid | myset | binset |
+-------+-----------------------------------+--------------+
| 1 | Travel,Sports,Dancing | 0111 |
| 2 | Travel,Dancing | 0101 |
| 3 | Travel | 0001 |
| 4 | Dancing | 0100 |
| 5 | Dancing | 0100 |
| 6 | Sports,Dancing | 0110 |
| 7 | Travel,Sports,Dancing,Fine Dining | 1111 |
| 8 | Travel,Fine Dining | 1001 |
| 9 | Sports,Fine Dining | 1010 |
| 10 | Travel,Dancing,Fine Dining | 1101 |
+-------+-----------------------------------+--------------+


with my active record class.
0

#4 User is offline   zaccaria 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 2,232
  • Joined: 04-October 09
  • Location:Moscow

Posted 22 October 2010 - 08:37 AM

You have to add the property, then you can select with:

$criteria=new CDbCriteria;
$criteria->select='rowid, myset, LPAD(BIN(myset+0),4,'0') AS binse';
set_test::model()->findAll();

0

#5 User is offline   R.K. 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 117
  • Joined: 20-September 10

Posted 22 October 2010 - 08:53 AM

View Postzaccaria, on 22 October 2010 - 08:37 AM, said:

You have to add the property, then you can select with:

$criteria=new CDbCriteria;
$criteria->select='rowid, myset, LPAD(BIN(myset+0),4,'0') AS binse';
set_test::model()->findAll();





juppi :) it worked thank you ;)
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • This topic is locked

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users