Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用pomm项目自动选择特定的hstore_Php_Postgresql_Pomm - Fatal编程技术网

Php 使用pomm项目自动选择特定的hstore

Php 使用pomm项目自动选择特定的hstore,php,postgresql,pomm,Php,Postgresql,Pomm,我希望使用特定的hstore键轻松选择多行。这里是“fr”键。 您可以看到以下结构: +----+----------------+-------------------------+ | id | name_i18n | description_i18n | +----+----------------+-------------------------+ | 1 | "fr"=> "nom 1" | "fr"=> "Description 1" | +

我希望使用特定的hstore键轻松选择多行。这里是“fr”键。 您可以看到以下结构:

+----+----------------+-------------------------+
| id | name_i18n      | description_i18n        |
+----+----------------+-------------------------+
|  1 | "fr"=> "nom 1" | "fr"=> "Description 1"  |
+----+----------------+-------------------------+
|  2 | "fr"=> "nom 2" | "fr"=> "Description 2"  |
+----+----------------+-------------------------+
|  3 | "fr"=> "nom 3" | "fr"=> "Description 3"  |
+----+----------------+-------------------------+
我想用计算机获得这个结果。为此,我为此创建了一个可扩展的ModelI18n。你可以看到


覆盖默认投影是一种很好的做法?您还有其他想法吗?

据我所知,您希望使用HStore字段保存翻译

模型类通过投影将数据库关系链接到PHP实体,因此投影意味着重载

<?php

class MyEntityModel extends Model
{
    protected $culture = 'en';

    public function setCulture($culture)
    {
        $this->culture = $culture;

        return $this;
    }

    public function createProjection()
    {
        return parent::createProjection()
            ->unsetField('name_i18n')
            ->setField(
                'name',
                sprintf("%%:name_i18n:%%->'%s'", $this->culture),
                'varchar'
            )
            ->unsetField('description_i18n')
            ->setField(
                'description',
                sprintf("%%:description_i18n:%%->'%s'", $this->culture),
                'text'
            )
        ;
    }
}
您可以用完全相同的方式使用JSONB字段

您提供的
createProjection
方法更通用,它可以位于由其他模型扩展的
GenericModel
类中

$entity = $pomm
    ->getDefaultSession()
    ->getModel(MyEntityModel::class)
    ->setCulture('fr')
    ->findByPk(['id' => $id])
    ;
/*
    SELECT
      id as id,
      name_i18n->'fr' as name,
      description_i18n->'fr' as description
    FROM
      a_schema.a_table
    WHERE
      id = $*
*/

echo $entity['name']; // nom 1
echo $entity['description']; // Description 1