php 间接修改重载属性 Laravel MongoDB
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34467076/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Indirect Modification of Overloaded Property Laravel MongoDB
提问by Raheel
I am using MongoDB with Laravel. I have a collection called categories
which has one document
我在 Laravel 中使用 MongoDB。我有一个名为的集合categories
,其中包含一个文档
[
{
"_id": "567dc4b4279871d0068b4568",
"name": "Fashion",
"images": "http://example.com/1.jpg",
"specifics": [
"made"
],
"brands": [
{
"name": "Giordano",
"logo": "http://example.com/"
},
{
"name": "Armani",
"logo": "http://example.com/"
}
],
"updated_at": "2015-12-25 22:40:44",
"created_at": "2015-12-25 22:35:32"
}
]
I am trying to make a function that add specifics to the specifics array in the above document.
我正在尝试创建一个向上述文档中的 specifics 数组添加细节的函数。
Here is how my request body is
这是我的请求正文的方式
HTTP: POST
{
"specifics": [
"material"
]
}
And i am handling this request with the following function
我正在使用以下功能处理此请求
/**
* Add specs to category
* @param string $category_id
* @return Illuminate\Http\JsonResponse
*/
public function addSpecifics($category_id)
{
$category = $this->category->findOrFail($category_id);
$category->specifics[] = $this->request->get('specifics');
$status_code = config('http.UPDATED');
return response()->json($category->save(), $status_code);
}
But when i hit this call, I get error of
但是当我打这个电话时,我得到了错误
ErrorException in CategoryController.php line 101: Indirect modification of overloaded property App\Category::$specifics has no effect
CategoryController.php 第 101 行中的 ErrorException:对重载属性 App\Category::$specifics 的间接修改无效
Please help me in fixing this.
请帮我解决这个问题。
I am using https://github.com/jenssegers/laravel-mongodbthis package for MongoDB.
我正在将https://github.com/jenssegers/laravel-mongodb这个包用于 MongoDB。
回答by jedrzej.kurylo
Due to how accessing model attributes is implemented in Eloquent, when you access $category->specifics, a magic __get()method is called that returns a copy of that attribute's value. Therefore, when you add an element to that copy, you're just changing the copy, not the original attribute's value. That's why you're getting an error saying that whatever you're doing, it won't have any effect.
由于在Eloquent 中访问模型属性的方式,当您访问$category->specifics 时,会调用一个神奇的__get()方法,该方法返回该属性值的副本。因此,当您向该副本添加元素时,您只是在更改副本,而不是原始属性的值。这就是为什么您会收到错误消息,说无论您在做什么,都不会产生任何影响。
If you want to add a new element to $category->specificsarray, you need to make sure that the magic __set()is used by accessing the attribute in a setter manner, e.g.:
如果要向$category->specifics数组添加新元素,则需要通过以 setter 方式访问属性来确保使用魔术__set(),例如:
$category->specifics = array_merge($category->specifics, $this->request->get('specifics'));
回答by Radames E. Hernandez
You can use this method in case that you want to add a single item to the array:
如果要将单个项目添加到数组,则可以使用此方法:
PHP:
PHP:
$new_id = "1234567";
$object->array_ids = array_merge($object->array_ids, [$new_id]);
This work's for me!
这份工作是给我的!
回答by Lucas Bustamante
Store your variable in a temp variable:
将变量存储在临时变量中:
// Fatal error
$foo = array_shift($this->someProperty);
// Works fine
$tmpArray = $this->someProperty;
$foo = array_shift($tmpArray);
回答by Sapnesh Naik
Don't try to directly modify an Eloquent collection, instead cast the collection to an Array and use that array for your modifications.
不要尝试直接修改 Eloquent 集合,而是将集合转换为 Array 并使用该数组进行修改。
$model = Model::findOrFail($id);
$array = $model->toArray();
$array['key'] = $value;