gpt4 book ai didi

php - Yii2 elasticsearch setAttributes()不起作用

转载 作者:行者123 更新时间:2023-12-03 00:36:15 26 4
gpt4 key购买 nike

我正在将Yii 2.1和Yii2-elasticsearch 2.0.3与elasticsearch 1.5.0服务器一起使用,以尝试为成员模型建立索引以进行更强大的搜索。我有一个common\indexes\Member模型,该模型扩展了yii\elasticsearch\ActiveRecord并设置了要索引的属性。

namespace common\indexes;

use yii\elasticsearch\ActiveRecord;

class Member extends ActiveRecord {

/**
* @return array the list of attributes for this record
*/
public function attributes() {
// path mapping for '_id' is setup to field 'id'
return ['id', 'given_name', 'family_name', 'email'];
}
}

我在设置 common\indexes\Member模型中需要的属性时遇到麻烦。

我正在创建对象的新实例,并尝试通过ActiveRecord setAttributes()方法设置属性值,但似乎没有设置任何值。
$index = new common\indexes\Member();
$index->setAttributes(['given_name' => 'Test', 'family_name' => 'User', 'email' => 'test.member@test.com']);
$index->save();

这似乎创建一个空记录。如果我一一手动设置属性,那么似乎一切正常,并且在Elasticsearch数据库中创建了具有正确属性的记录。
$index = new common\indexes\Member();
$index->given_name = 'Test';
$index->family_name = 'User';
$index->email = 'test.member@test.com';
$index->save();

我是否对Elasticsearch ActiveRecord使用了 setAttributes()方法错误?我是否需要以其他方式设置我的Elasticsearch模型?

最佳答案

默认情况下,setAttributes仅设置至少定义一个验证规则或至少定义为“安全”的属性(通过 safeAttributes() 或通过safe-validator)。

您可以通过将 call 更改为来强制其分配所有内容

$index->setAttributes([
'given_name' => 'Test',
'family_name' => 'User',
'email' => 'test.member@test.com'
], false);

这表明还可以分配非安全属性。
但是我通常更喜欢确保 the validation配置正确

关于php - Yii2 elasticsearch setAttributes()不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30635360/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com