0

我正在使用https://github.com/Astrotomic/laravel-translatable使我的模型可翻译。

虽然我正在尝试使用以下方法更新模型以更新模式及其相关的翻译内容。

$product = $this->repository->update($input, $uuid);

   // output of dd($input);

  "uuid" => "44b26fb0-04b9-11eb-981a-6b01570d58ed"
  "_method" => "PUT"
  "en-us" => array:5 [▶]
  "ar-eg" => array:5 [▶]
  "account_uuid" => "a1c23ce0-04b7-11eb-b060-7faa78f23afd"
  "type" => "tour"
  "iso_4217" => "EGP"
  "status" => "pending"

我收到了这个错误:

Column not found: 1054 Unknown column 'id' in 'where clause'

考虑到模型主键是“uuid”而不是“id”,因此外键是“product_uuid”而不是“product_id”,如下面的模型设置。

class Product extends Model implements Transformable, TranslatableContract
{
    public $incrementing = false;
    public $keyType = 'string';
    public $timestamps = true;
    protected $primaryKey = 'uuid';

    public $translatedAttributes = [
        'name',
        'quantity_label_snigular',
        'quantity_label_plural',
        'brief_description',
        'long_description',
        'terms',
        'meta_keywords',
    ];

    public $translationForeignKey = 'product_uuid';
    public $localeKey = 'uuid';    
    public $useTranslationFallback = true;    
}

我遵循了此链接https://docs.astrotomic.info/laravel-translatable/usage/forms#saving-the-request上的确切文档,尽管我无法追踪该错误。

4

1 回答 1

0

我发现问题出在 productTranslation 模型的设置中。

“product_translations”表主键是“product_uuid”和“locale”的组合。

所以在“ProductTranslation”模型中,我将主键定义为一个数组

protected $primaryKey = ['product_uuid', 'locale'];

并根据此答案扩展这些方法以从数组而不是字符串中获取主键https://stackoverflow.com/a/37076437

    /**
     * Set the keys for a save update query.
     *
     * @param  \Illuminate\Database\Eloquent\Builder  $query
     * @return \Illuminate\Database\Eloquent\Builder
     */
    protected function setKeysForSaveQuery(Builder $query)
    {
        $keys = $this->getKeyName();
        if(!is_array($keys)){
            return parent::setKeysForSaveQuery($query);
        }

        foreach($keys as $keyName){
            $query->where($keyName, '=', $this->getKeyForSaveQuery($keyName));
        }

        return $query;
    }

    /**
     * Get the primary key value for a save query.
     *
     * @param mixed $keyName
     * @return mixed
     */
    protected function getKeyForSaveQuery($keyName = null)
    {
        if(is_null($keyName)){
            $keyName = $this->getKeyName();
        }

        if (isset($this->original[$keyName])) {
            return $this->original[$keyName];
        }

        return $this->getAttribute($keyName);
    }
于 2020-10-04T12:09:01.223 回答