选择后 Silverstripe 不保存到对象

Silverstripe not saving to object after selection

我想在使用过滤器选择对象后插入数据库。

这是我的代码:

    $dayMealType = $newCustomer->Diets()->first()->DietMealPlan()->first()->Days()->filter('Order',$request->postVar('updateData')['dayNr'])->first()->DayMealType()->filter("MealTypeID",$request->postVar('updateData')['MealTypeId'])->first()->Foods();
$food_tmp = new FoodMealPlan();
$food_tmp->Carbohydrates = $request->postVar('updateData')['carbohydrates'];
$dayMealType->add($food_tmp);
$dayMealType->write();

选择有效,它 return 一个如下所示的对象:

object(ManyManyList)#102 (16) {
  ["joinTable":protected]=>
  string(17) "DayMealType_Foods"
  ["localKey":protected]=>
  string(14) "FoodMealPlanID"
  ["foreignKey":protected]=>
  string(13) "DayMealTypeID"
  ["extraFields":protected]=>
  array(0) {
  }
  ["_compositeExtraFields":protected]=>
  array(0) {
  }
  ["dataClass":protected]=>
  string(12) "FoodMealPlan"
  ["dataQuery":protected]=>
  object(DataQuery)#98 (8) {
    ["dataClass":protected]=>
    string(12) "FoodMealPlan"
    ["query":protected]=>
    object(SQLQuery)#97 (12) {
      ["isDelete":protected]=>
      bool(false)
      ["select":protected]=>
      array(0) {
      }
      ["groupby":protected]=>
      array(0) {
      }
      ["having":protected]=>
      array(0) {
      }
      ["distinct":protected]=>
      bool(true)
      ["orderby":protected]=>
      array(0) {
      }
      ["limit":protected]=>
      NULL
      ["where":protected]=>
      array(1) {
        [0]=>
        array(1) {
          [""DayMealType_Foods"."DayMealTypeID" = ?"]=>
          array(1) {
            [0]=>
            int(26810)
          }
        }
      }
      ["connective":protected]=>
      string(3) "AND"
      ["from":protected]=>
      array(2) {
        ["FoodMealPlan"]=>
        string(14) ""FoodMealPlan""
        ["DayMealType_Foods"]=>
        array(5) {
          ["type"]=>
          string(5) "INNER"
          ["table"]=>
          string(17) "DayMealType_Foods"
          ["filter"]=>
          array(1) {
            [0]=>
            string(58) ""DayMealType_Foods"."FoodMealPlanID" = "FoodMealPlan"."ID""
          }
          ["order"]=>
          int(20)
          ["parameters"]=>
          array(0) {
          }
        }
      }
      ["replacementsOld":protected]=>
      array(0) {
      }
      ["replacementsNew":protected]=>
      array(0) {
      }
    }
    ["collidingFields":protected]=>
    array(0) {
    }
    ["queriedColumns":"DataQuery":private]=>
    NULL
    ["queryFinalised":"DataQuery":private]=>
    bool(false)
    ["querySubclasses":protected]=>
    bool(true)
    ["filterByClassName":protected]=>
    bool(true)
    ["queryParams":"DataQuery":private]=>
    array(3) {
      ["Component.ExtraFields"]=>
      array(0) {
      }
      ["Foreign.ID"]=>
      int(26810)
      ["Foreign.Filter"]=>
      array(1) {
        [""DayMealType_Foods"."DayMealTypeID""]=>
        int(26810)
      }
    }
  }
  ["model":protected]=>
  object(DataModel)#3 (1) {
    ["customDataLists":protected]=>
    array(0) {
    }
  }
  ["inAlterDataQueryCall":protected]=>
  bool(false)
  ["failover":protected]=>
  NULL
  ["customisedObject":protected]=>
  NULL
  ["objCache":"ViewableData":private]=>
  array(0) {
  }
  ["class"]=>
  string(12) "ManyManyList"
  ["extension_instances":protected]=>
  array(0) {
  }
  ["beforeExtendCallbacks":protected]=>
  array(0) {
  }
  ["afterExtendCallbacks":protected]=>
  array(0) {
  }
}

食物的class是这样的:

class DayMealType extends DataObject
{
     private static $db = array(
        'Calories'  => 'Int',
        'Carbohydrates'  => 'Int',
        'Proteins'  => 'Int',
        'Lipids'  => 'Int',
        'Price' => 'Decimal'
     );

      private static $has_one = array(
        "MealType" => "MealTypeDetails",
        "DayDietMealPlan" => "DayDietMealPlan"
      );
      private static $many_many = array(
        "Foods" => "FoodMealPlan"
      );

      private static $casting = array(
        'FoodsList' => 'HTMLText'
      );

它 return 函数 write() 后出错;

不写入数据库

您需要先将 FoodMealPlan 保存到数据库,然后再添加关系。

$food_tmp = new FoodMealPlan();
$food_tmp->Carbohydrates = $request->postVar('updateData')['carbohydrates'];
$food_tmp->write(); // Saves to the database
$dayMealType->add($food_tmp);
$dayMealType->write();

参考:https://github.com/silverstripe/silverstripe-framework/blob/3.5/model/ManyManyList.php#L198