• Yii2 关联查询结果AR对象 如何取到表以外的字段


    一、

    model模型中重写 fields 方法

    public function fields()
    {
        $fields = parent::fields();
        $fields['store_id']  = 'store_id';
        $fields['name']  = 'name';
        $fields['username']  = 'username';
        $fields['password']  = 'password';
        $fields['integral']  = 'integral';
        $fields['email']  = 'email';
        return $fields;
    }

    二、

    给model模型增加相对应的成员属性,然后重写 attributes 方法

    class user extends \yii\db\ActiveRecord
    

    {

    public $store_id;
    public $name;
    public $username;
    public $password;
    public $integral;
    public $email;
    public function rules(){}
    

    ...

    ...

    public function attributes()
    {
        $keys = parent::attributes();
        $keys[] = 'store_id';
        $keys[] = 'name';
        $keys[] = 'username';
        $keys[] = 'password';
        $keys[] = 'integral';
        $keys[] = 'email';
        return $keys;
    }

    }

    后续再整理一下,可能有点混乱

    例子

    1. namespace app\models;
    2. use Yii;
    3. /**
    4. * This is the model class for table "{{%cat}}".
    5. *
    6. * @property int $id
    7. * @property int $parent_id 上级分类id
    8. * @property string $name 分类名称
    9. * @property integer $sort
    10. * @property int $addtime
    11. * @property int $is_delete
    12. * @property string $child
    13. */
    14. class Cat extends \yii\db\ActiveRecord
    15. {
    16. public $child;
    17. /**
    18. * {@inheritdoc}
    19. */
    20. public static function tableName()
    21. {
    22. return '{{%cat}}';
    23. }
    24. /**
    25. * {@inheritdoc}
    26. */
    27. public function rules()
    28. {
    29. return [
    30. [['parent_id', 'addtime', 'is_delete', 'sort'], 'integer'],
    31. [['name'], 'required'],
    32. [['name'], 'string', 'max' => 255],
    33. ];
    34. }
    35. /**
    36. * {@inheritdoc}
    37. */
    38. public function attributeLabels()
    39. {
    40. return [
    41. 'id' => 'ID',
    42. 'parent_id' => '上级分类id',
    43. 'name' => '分类名称',
    44. 'sort' => '排序,升序',
    45. 'addtime' => 'Addtime',
    46. 'is_delete' => 'Is Delete',
    47. ];
    48. }
    49. public function fields()
    50. {
    51. $fields = parent::fields();
    52. $fields['child'] = 'child';
    53. return $fields;
    54. }
    55. public function attributes()
    56. {
    57. $keys = parent::attributes();
    58. $keys[] = 'child';
    59. return $keys;
    60. }
    61. /**
    62. * [getChildrenList 获取和父级select一致的子级列表{还有完善空间}]
    63. */
    64. public function getChildrenList()
    65. {
    66. $keys = array_keys(array_filter($this->attributes)); // 使用$this->attributes 并不靠谱, 这里需要获取父级的select 都设置了哪些字段
    67. return $this->hasMany(Cat::className(), ['parent_id' => 'id'])->select($keys)->where(['is_delete' => 0])->orderBy('sort,addtime DESC');
    68. }
    69. }
    
                    
  • 相关阅读:
    sql server判断两个集合字符串是否存在交集
    [1795]. 每个产品在不同商店的价格
    人工智能AI风口已开:如何赋予UI设计与视频剪辑新生命
    CSS高级技巧
    简述 HTML 的语义化标签是什么?
    【算法集训 | 暑期刷题营】7.20题---贪心
    HTML语义化标签(一)
    SecureCRT SSH与FTP连接中文乱码
    华为设备Smart Link和Monitor Link配置命令
    读书笔记系列——读《深入理解JVM第3版》笔记:Java内存区域
  • 原文地址:https://blog.csdn.net/qq_38032300/article/details/124687031