我有一个名字很长的表,例如products_with_a_long_name
。型号名称为ProductsWithALongName
。
现在,我有一个查询,在与另一个表联接时,我需要从该表中选择许多列。例子:
ProductsWithALongName::find()
->select(['products_with_a_long_name.id', 'products_with_a_long_name.selling_price','client.name'])
->leftjoin('all_the_clients as client','products_with_a_long_name.clientId = client.id')
->where(['products_with_a_long_name.id' => $var]);
现在,我如何为第一个表使用别名
products_with_a_long_name
,因为我为第二个表使用别名。我知道我可以使用Query
,但是在这种情况下,我需要结果为ActiveRecord
,因此在这种情况下,这不是一个选择。
最佳答案
您可以使用 alias()
:
ProductsWithALongName::find()
->alias('p')
->select(['p.id', 'p.selling_price','client.name'])
->leftjoin('all_the_clients as client', 'p.clientId = client.id')
->where(['p.id' => $var]);
关于Yii2-具有ActiveRecord的表别名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49474661/