简体   繁体   中英

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.

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;

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM