CakePHP 3.x 数据库映射,BelongsToMany 与 HasMany+HasOne
CakePHP 3.x DB mapping, BelongsToMany vs HasMany+HasOne
我在数据库中有三个表:(1) offers, (2) offer_rows, (3) 产品.
offer_rows 将始终指向 offer,并且可能(但不总是)指向 产品。 offer_rows还有价格等其他字段
与(我的)相同SQL:
create table offers(
id serial not null auto_increment primary key,
...
);
create table offer_rows(
id serial not null auto_increment primary key,
product_id bigint(20) unsigned references products(id),
offer_id bigint(20) unsigned not null references offers(id),
price decimal(15,2),
...
);
create table products(
id serial not null auto_increment primary key,
...
);
就 CakePHP (3.3.16) 而言,对产品的可选引用,正确的映射是什么?
如果 offer_rows 对产品引用有非空限制(目前没有),那么 BelongsToMany 应使用:
(class OffersTable)
@property \Cake\ORM\Association\BelongsToMany $Products
// initialize
$this->belongsToMany('Products', [
'through' => 'OfferRows',
'foreignKey' => 'offer_id',
'joinType' => 'INNER',
'joinTable' => 'offer_rows',
]);
(class OfferRowsTable)
@property \Cake\ORM\Association\BelongsTo $Products
@property \Cake\ORM\Association\BelongsTo $Offers
// initialize
$this->belongsTo('Products', [
'foreignKey' => 'product_id'
]);
$this->belongsTo('Offers', [
'foreignKey' => 'offer_id',
'joinType' => 'INNER'
]);
(class ProductsTable)
@property \Cake\ORM\Association\BelongsToMany $Offers
// initialize
$this->belongsToMany('Offers', [
'through' => 'OfferRows',
'foreignKey' => 'product_id',
'joinType' => 'INNER',
'joinTable' => 'offer_rows',
]);
但是,有可能出现空积,我应该改用 HasMany + HasOne 吗?
(class OffersTable)
@property \Cake\ORM\Association\HasMany $OfferRows
// initialize
$this->hasMany('OfferRows', [
'foreignKey' => 'offer_id'
]);
(class OfferRowsTable)
@property \Cake\ORM\Association\BelongsTo $Offers
@property \Cake\ORM\Association\HasOne $Products
// initialize
$this->belongsTo('Offers', [
'foreignKey' => 'offer_id',
'joinType' => 'INNER'
]);
$this->hasOne('Products', [
'className' => 'Products',
'propertyName' => 'reference_product_obj',
'foreignKey' => 'reference_product'
]);
(class ProductsTable)
@property \Cake\ORM\Association\BelongsToMany $OfferRows
// initialize
$this->belongsToMany('OfferRows', [
'foreignKey' => 'product_id',
'joinType' => 'INNER',
]);
有一个是正确的,还是有第三个选择?
如果您需要检索连接 table 数据,而不管产品是否存在/是否已链接,那么您应该采用类似于后一种解决方案的方法。
但是 OfferRowsTable
class 中的 Products
关联应该是 belongsTo
关联,因为 hasOne
期望目标中存在外键table,即products
table。使用 hasOne
最晚会在保存关联时破坏内容。
此外 ProductsTable
class 中的 belongsToMany
关联应该指向 Offers
,而不是 OfferRows
。
我在数据库中有三个表:(1) offers, (2) offer_rows, (3) 产品.
offer_rows 将始终指向 offer,并且可能(但不总是)指向 产品。 offer_rows还有价格等其他字段
与(我的)相同SQL:
create table offers(
id serial not null auto_increment primary key,
...
);
create table offer_rows(
id serial not null auto_increment primary key,
product_id bigint(20) unsigned references products(id),
offer_id bigint(20) unsigned not null references offers(id),
price decimal(15,2),
...
);
create table products(
id serial not null auto_increment primary key,
...
);
就 CakePHP (3.3.16) 而言,对产品的可选引用,正确的映射是什么?
如果 offer_rows 对产品引用有非空限制(目前没有),那么 BelongsToMany 应使用:
(class OffersTable)
@property \Cake\ORM\Association\BelongsToMany $Products
// initialize
$this->belongsToMany('Products', [
'through' => 'OfferRows',
'foreignKey' => 'offer_id',
'joinType' => 'INNER',
'joinTable' => 'offer_rows',
]);
(class OfferRowsTable)
@property \Cake\ORM\Association\BelongsTo $Products
@property \Cake\ORM\Association\BelongsTo $Offers
// initialize
$this->belongsTo('Products', [
'foreignKey' => 'product_id'
]);
$this->belongsTo('Offers', [
'foreignKey' => 'offer_id',
'joinType' => 'INNER'
]);
(class ProductsTable)
@property \Cake\ORM\Association\BelongsToMany $Offers
// initialize
$this->belongsToMany('Offers', [
'through' => 'OfferRows',
'foreignKey' => 'product_id',
'joinType' => 'INNER',
'joinTable' => 'offer_rows',
]);
但是,有可能出现空积,我应该改用 HasMany + HasOne 吗?
(class OffersTable)
@property \Cake\ORM\Association\HasMany $OfferRows
// initialize
$this->hasMany('OfferRows', [
'foreignKey' => 'offer_id'
]);
(class OfferRowsTable)
@property \Cake\ORM\Association\BelongsTo $Offers
@property \Cake\ORM\Association\HasOne $Products
// initialize
$this->belongsTo('Offers', [
'foreignKey' => 'offer_id',
'joinType' => 'INNER'
]);
$this->hasOne('Products', [
'className' => 'Products',
'propertyName' => 'reference_product_obj',
'foreignKey' => 'reference_product'
]);
(class ProductsTable)
@property \Cake\ORM\Association\BelongsToMany $OfferRows
// initialize
$this->belongsToMany('OfferRows', [
'foreignKey' => 'product_id',
'joinType' => 'INNER',
]);
有一个是正确的,还是有第三个选择?
如果您需要检索连接 table 数据,而不管产品是否存在/是否已链接,那么您应该采用类似于后一种解决方案的方法。
但是 OfferRowsTable
class 中的 Products
关联应该是 belongsTo
关联,因为 hasOne
期望目标中存在外键table,即products
table。使用 hasOne
最晚会在保存关联时破坏内容。
此外 ProductsTable
class 中的 belongsToMany
关联应该指向 Offers
,而不是 OfferRows
。