kero
- kero 评论了教程 Yii2 Mongodb 生成递增id的方法
$result = \Yii::$app->db->getCollection('ids')->findandmodify(
['name'=>static::collectionName()], ['$inc'=>array('id'=>1)], ['upsert'=>true,'fields' => 'id'] );
return $result['id'];
这样就可以了吧
- kero 2017-12-22 已签到连续签到6天,获得了20个金钱
$this->hasMany(Spu::className(),['sku.sku' => 'spu.sku'])->where(['sku.account' => 'spu.account']);这样试试
或者
你的sku.account和spu.account是一对多的关系
class Sku extends ActiveRecord{public function getSpu(){ return $this->hasMany(Spu::className(),['sku.sku' => 'spu.sku']); } public function getItems() { return $this->hasMany(Item::className(), ['sku.account' => 'spu.account']) ->via('spu'); }
}
$sku = Sku::findOne(100);
$items = $sku->items;你这想的是中间表的概念吧,Item::className() ?多了一个模型 我没怎么理解。后面我想了一下 多字段关联应该不是纯粹上的一对多关系吧(这个还不怎么确定,但是底层代码确实单独写了解析多个字段的方法,后面直接是在产品里多加了个唯一序列去关联的)
- kero 2017-12-21 已签到连续签到5天,获得了20个金钱
- kero 2017-12-20 已签到连续签到4天,获得了20个金钱
假设:
$customers = Customer::find()->with([ 'orders' => function ($query) { /**@var yii\db\ActiveQuery $query */ $query->andWhere(['status' => Order::STATUS_ACTIVE]); }, ])->all();
建议看:http://www.yiichina.com/doc/guide/2.0/db-active-record
再建议:多看文档,比在这里问人好多了感觉答非所问! $this->hasMany(Spu::className(),['sku.sku' => 'spu.sku','sku.account' => 'spu.account']); 我要在模型里配置类似这样的关联 关联条件不是单个字段,是多个字段的
- kero 回复了 dingjj2010 的回答
首先,框架这么解析是没有错的.
其次,你要的是什么? 什么逻辑, 或者有什么错误?
问题中只看到 "加单个条件是可以的,多个条件不行", 然后??通过两个字段 sku,account 关联的产品表和子产品表 怎样通过with查询来实现查询产品列表?
我在产品模型中配置获取子产品的 hasMany时
public function getSpu(){ return $this->hasMany(Spu::className(),['sku.sku' => 'spu.sku','sku.account' => 'spu.account']); }
我的产品和子产品是 sku,account 同时关联的 怎样去配置with查询 产品列表?
- kero 2017-12-19 已签到连续签到3天,获得了15个金钱