【问题标题】:I am facing issue while updating the data into table in Yii2我在将数据更新到 Yii2 中的表时遇到问题
【发布时间】:2020-07-17 18:25:29
【问题描述】:

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

Package link.

错误:

 "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')",

我的型号代码:

    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'
            ]);
        }

谁能帮我解决上述问题。 谢谢。

表结构:

变体表

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

元字段表

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

【问题讨论】:

    标签: php yii2 package relationship yii2-advanced-app


    【解决方案1】:

    yii\db\ActiveRecordlink() 方法期望关系使用主键。 默认情况下,框架会从 db 架构中找到主键,但您的表没有任何主键。

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

    在你的模型类中

    public static function primaryKey()
    {
        return ['id', 'consumer_id'];
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-10-31
      • 1970-01-01
      • 2015-05-31
      • 1970-01-01
      • 1970-01-01
      • 2019-07-22
      • 2021-09-22
      相关资源
      最近更新 更多