间接修改重载属性App \ Category :: $ thesizes无效 [英] Indirect modification of overloaded property App\Category::$thesizes has no effect

查看:43
本文介绍了间接修改重载属性App \ Category :: $ thesizes无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于一个网上商店,我试图生成一个看起来像这样的表:

For a webshop im trying to generate a table that looks like:

Tablename: category 1
productname     S   M   L   X  total
name1           1    0  1   3  5
name2           0    1  0   2  3


Tablename: category 2
productname     S   L   X  total
name5           1   1   3  5
name8           0   0   2  2

每个类别都有一个表格,每个类别都有自己的大小(例如表2没有大小M).这些表显示了每个类别中每种产品每种尺寸的已订购产品的数量.

There is a table for each category, each category has his own sizes (table 2 has no size M for example). The tables show the amount of ordered products per size per product in each category.

应用程序中有一个模型 OrderProducts ,该模型是每个 Order 中的已订购产品.

In the application there is a model OrderProducts which are ordered products in each Order.

OrderProduct (订单产品)具有 ProductSize (产品尺寸),该表是产品尺寸的联结表

An OrderProduct has a ProductSize which is a junction table of the product sizes

ProductSize 具有 Size (其中包含尺寸名称)

A ProductSize has a Size (which contains the name of the size)

im tryin要做的第一步是获取每个类别的所有尺寸/产品,例如:

The first step im tryin to do is get all sizes/products for each category like:

    $order = Order::findOrFail($id);
    $products = OrderProduct::where('orders_id',$id)->get();

    $categories = Category::all();
    //get sizes and products per category
    foreach($categories as $cat)
    {
        $cat->thesizes= array();
        $cat->theprodcts= array();
        foreach($products as $product)
        {
            if($product->productSize->product->category_id == $cat->id)
            {
                array_push($cat->thesizes,$product->productSize);
                array_push($cat->theprodcts,$product);
            }

        }
        //make sure all values are unique (no dubbele sizes).
        $cat->theSizes = array_unique($cat->theSizes);
        $cat->theProducts = array_unique($cat->theProducts);
    }

运行代码时,出现以下错误:

When I run my code I get the following error:

对重载属性App \ Category :: $ thesizes的间接修改没有效果

Indirect modification of overloaded property App\Category::$thesizes has no effect

为什么会出现此错误,我该如何解决?

Why do I get this error and how should I solve it?

推荐答案

这是因为您的 Category 类具有 __ get() __ set()已实现魔术方法.

This is because your Category class has the __get() and __set() magic methods implemented.

因此第7行( $ cat-> thesizes = array(); )调用 Category :: __ set()和第12行( array_push($cat-> thesizes,$ product-> productSize); )调用 Category :: __ get() 但不是 Category :: __ set().因此,尽管您打算将值推送到在类别"上设置的数组上来实现此目的,但由于 array_push()正在处理返回值而不是存储在其中的实际数组,因此它不起作用类别.

So line 7 ($cat->thesizes= array();) invokes Category::__set() and line 12 (array_push($cat->thesizes,$product->productSize);) invokes Category::__get() but not Category::__set(). So while you impelemented this with the intention of pushing values onto an array that you set on the Category, it won't work since array_push() is working on a return value and not the actual array stored in the Category.

有几种方法可以解决此问题.最快捷的方法是将 Category :: __ get()更改为通过引用返回值,这是通过在函数的返回声明上使用某种类型的提示来完成的

There are a few ways to fix this. The most shortcut way is to change Category::__get() to return values by reference, which is done by using a sort-of type-hint on the function's return declaration

class Category
{
    public function &__get($key) {
        // body of function
    }
}

但是,出于您好奇的原因,我可能不建议您这样做,因此不建议这样做.

But this is probably not recommended for reasons I can go into if you're curious.

更明智的方法是至少在循环范围内构建数组,然后至少将其修改,然后将它们添加到您的 Category 对象中

The more sensible approach, without significantly modifying your code at least, is to build the arrays within the scope of the loop and then add them to your Category objects

foreach ($categories as $cat) {
    // Scope local arrays here first
    $thesizes = array();
    $theproducts = array();

    foreach ($products as $product) {
        if ($product->productSize->product->category_id == $cat->id) {
            // Push to those local arrays
            array_push($thesizes, $product->productSize);
            array_push($theprodcts, $product);
        }
    }

    // Now assign them to the category object
    $cat->theSizes = array_unique($thesizes);
    $cat->theProducts = array_unique($theproducts);
}

如果您想获得加分,因为这是Laravel,您的返回值为收藏,您可以执行类似的操作来实现更复杂的实现

If you want to go for bonus points, since this is Laravel and your return values are collections, you can do something like this for a more sophisticated implementation

$categories = (Category::all())->map(function(Category $cat) {
    $cat->theProducts = $products
        ->filter(function(Product $product) use ($cat) {
            return $product->productSize->product->category_id == $cat->id;
        })
        ->unique();

    $cat->theSizes = $cat->theProducts
        ->map(function(Product $product) {
            return $product->productSize();
        })->unique();
});

这篇关于间接修改重载属性App \ Category :: $ thesizes无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆