metaforce
metaforce

Reputation: 1377

String concatenation in ActiveRecord in Yii Framework?

Is it possible to CONCAT values of two table columns using the Yii's ActiveRecord methods and patterns?

If is, how?

I'm currently using this:

return $this->findByAttributes(array('alias' => $alias));

But in this I need to return the concated string/values.

Please help.

Upvotes: 2

Views: 4155

Answers (3)

Mamba
Mamba

Reputation: 61

yes you can do it by using the sql concat function in your activerecord definition
...find()
->select(['alias'=>'CONCAT(....)'])
->all();

that would produce a column called alias with the values from the columns used in the concat
hope that still helps

Upvotes: 1

Neil McGuigan
Neil McGuigan

Reputation: 48246

add a method to your model, like this:

public function getEmail()
{
  return $this->username.'@'.$this->domain;
}

then in views, you can just use the "email" attribute (not getEmail) and you're good to go.

Upvotes: 2

The Bndr
The Bndr

Reputation: 13394

If you are looking for something like that:

select username||'@'||domain from user_mail_table;

to receive concatenated values - so i don't think, that it's possible. That's not the idea of ActiveRecord.

If you like to concat values like the example above, you should take DAO http://www.yiiframework.com/doc/guide/1.1/en/database.dao ...or if you need ActiveRecord, you should concat the values inside your application

$mailaddress=$model->username.'@'.$model->domain;

Upvotes: 4

Related Questions