💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
控制器方法里的代码 ``` //根据顾客查询订单信息 //$info=Customer::find()->where(['name'=>'zhangsan'])->all(); //$customer=Customer::find()->where(['name'=>'zhangsan'])->one(); //$orders=$customer->hasMany('app\models\Order',['customer_id'=>'id'])->asArray()->all(); //$orders=$customer->hasMany(Order::class,['customer_id'=>'id'])->asArray()->all(); //把上面的放到Customer模型里getOrders()方法里 //$orders=$customer->getOrders(); //调用不存在的属性,触发php的魔术方法 __get,继而调用getOrders()方法 //$orders=$customer->orders; //print_r($orders); //根据订单查询客户的信息 $order=Order::find()->where(['id'=>1])->one(); $customer=$order->getCustomer(); //$customer=$order->customer; print_r($customer); ``` ***** 模型Customer.php里的代码 ``` <?php namespace app\models; use yii\db\ActiveRecord; class Customer extends ActiveRecord{ public static function tableName(){ return '{{%customer}}'; } //获取定单信息 public function getOrders(){ return $this->hasMany(Order::class,['customer_id'=>'id'])->asArray()->all(); } } ``` ***** 模型Order.php里的代码 ``` <?php namespace app\models; use yii\db\ActiveRecord; class Order extends ActiveRecord{ public static function tableName(){ return '{{%order}}'; } public function getCustomer(){ return $this->hasOne(Customer::class,['id'=>'customer_id'])->asArray()->one(); } } ```