首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Yii2中将数据更新到表中时遇到问题

在Yii2中将数据更新到表中时遇到问题
EN

Stack Overflow用户
提问于 2020-07-16 19:51:53
回答 1查看 64关注 0票数 0

我的一个同事在yii2关系中使用了下面的包。现在,在更新数据的时候,我有时会遇到下面的错误。

Package link

错误:

代码语言:javascript
复制
 "name": "Exception",
    "message": "Unable to link models: the link defining the relation does not involve any primary key.",
    "code": 0,
    "type": "yii\\base\\InvalidCallException",
    "file": "/app/vendor/yiisoft/yii2/db/BaseActiveRecord.php",
    "line": 1352,
    "stack-trace": [
        "#0 /app/vendor/la-haute-societe/yii2-save-relations-behavior/src/SaveRelationsBehavior.php(558): yii\\db\\BaseActiveRecord->link('metafields', Object(shopify\\models\\VariantMetafield), Array)",
        "#1 /app/vendor/la-haute-societe/yii2-save-relations-behavior/src/SaveRelationsBehavior.php(512): lhs\\Yii2SaveRelationsBehavior\\SaveRelationsBehavior->_afterSaveHasManyRelation('metafields')",

我的模型代码:

代码语言:javascript
复制
    public function behaviors(){
        
        $behaviors = parent::behaviors();
        
        $behaviors[] = [
            'class' => SaveRelationsBehavior::className(),
            'relationKeyName' => SaveRelationsBehavior::RELATION_KEY_RELATION_NAME,
            'relations' => [
                'images',
                'options',
                'variants', 
                'metafields',  
                'tags',
                'vendors',
                'productTypes' 
            ]
        ]; 
        return $behaviors;
    }

public function getMetafields(){
            
            $productMetafieldClass  = Yii::$app->factory->getClassByResource(Factory::PRODUCT_METAFIELD);
            
            return $this->hasMany($productMetafieldClass, [
                'owner_id'          => 'id',
                'consumer_id'       => 'consumer_id'
            ]);
        }

谁能帮我解决上面的问题。谢谢。

表结构:

变异表

代码语言:javascript
复制
CREATE TABLE `variants` (
  `id` bigint(20) NOT NULL COMMENT 'unique identifier of Shopify Shop',
  `product_id` bigint(20) DEFAULT NULL COMMENT 'unique identifier of shop',
  `consumer_id` int(30) NOT NULL COMMENT 'unique identifier of shop',
  `option1` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `option2` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `option3` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `barcode` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `compare_at_price` float DEFAULT NULL,
  `fulfillment_service` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `grams` float DEFAULT NULL,
  `image_id` bigint(20) DEFAULT NULL COMMENT 'unique identifier of shop',
  `inventory_item_id` bigint(20) DEFAULT NULL COMMENT 'unique identifier of shop',
  `inventory_management` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `inventory_policy` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `inventory_quantity` int(11) DEFAULT NULL,
  `position` int(11) DEFAULT NULL,
  `price` float DEFAULT NULL,
  `sku` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `taxable` tinyint(1) DEFAULT NULL,
  `tax_code` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `title` varchar(500) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `weight` float DEFAULT NULL,
  `weight_unit` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `created_at` varchar(25) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT 'The date and time (ISO 8601) when the customer was updated.',
  `updated_at` varchar(25) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT 'The date and time (ISO 8601) when the customer was updated.',
  `update_hash` varchar(64) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `condition` int(11) DEFAULT 0,
  `feed_image_id` bigint(20) DEFAULT NULL,
  UNIQUE KEY `index__variants-consumer_id` (`id`,`consumer_id`),
  UNIQUE KEY `index__variants-product_id` (`id`,`product_id`,`consumer_id`),
  KEY `fk__variants__consumers-id` (`consumer_id`),
  KEY `fk__variants-product_id__product-id` (`product_id`,`consumer_id`),
  CONSTRAINT `fk__variants-product_id__product-id` FOREIGN KEY (`product_id`, `consumer_id`) REFERENCES `products` (`id`, `consumer_id`) ON DELETE CASCADE,
  CONSTRAINT `fk__variants__consumers-id` FOREIGN KEY (`consumer_id`) REFERENCES `consumers` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci

Metafield表

代码语言:javascript
复制
CREATE TABLE `metafields` (
  `id` bigint(20) NOT NULL COMMENT 'unique identifier of Shopify Shop',
  `owner_id` bigint(20) DEFAULT NULL COMMENT 'unique identifier of shop',
  `owner_resource` varchar(300) COLLATE utf8mb4_unicode_ci NOT NULL,
  `description` text COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `namespace` varchar(20) COLLATE utf8mb4_unicode_ci NOT NULL,
  `key` varchar(30) COLLATE utf8mb4_unicode_ci NOT NULL,
  `value` text COLLATE utf8mb4_unicode_ci NOT NULL,
  `value_type` varchar(30) COLLATE utf8mb4_unicode_ci NOT NULL,
  `created_at` varchar(25) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT 'The date and time (ISO 8601) when the customer was updated.',
  `updated_at` varchar(25) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT 'The date and time (ISO 8601) when the customer was updated.',
  `consumer_id` int(30) DEFAULT NULL COMMENT 'unique identifier of shop',
  `update_hash` varchar(64) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  UNIQUE KEY `index__product_metafields-consumer_id` (`id`,`consumer_id`),
  KEY `fk__product_metafields__consumers-id` (`consumer_id`),
  CONSTRAINT `fk__product_metafields__consumers-id` FOREIGN KEY (`consumer_id`) REFERENCES `consumers` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
EN

回答 1

Stack Overflow用户

发布于 2020-07-17 18:25:29

yii\db\ActiveRecordlink()方法要求关系使用主键。默认情况下,框架从db模式中查找主键,但是您的表没有任何主键。

您可以尝试覆盖模型中primaryKey()方法,使框架使用具有唯一索引的列作为主键,而不是在数据库架构中查找它们。

在你的模型类中

代码语言:javascript
复制
public static function primaryKey()
{
    return ['id', 'consumer_id'];
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62934087

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档