ProductAttributeType::find_or_make PHP Méthode

find_or_make() public static méthode

public static find_or_make ( $name )
    public static function find_or_make($name)
    {
        if ($type = ProductAttributeType::get()->filter("Name:nocase", $name)->first()) {
            return $type;
        }
        $type = ProductAttributeType::create();
        $type->Name = $name;
        $type->Label = $name;
        $type->write();
        return $type;
    }

Usage Example

 function variationRow(&$obj, $val, $record)
 {
     $obj->write();
     //make sure product is in DB
     //TODO: or find existing variation
     $variation = DataObject::get_one('ProductVariation', "InternalItemID = '{$val}'");
     if (!$variation) {
         $variation = new ProductVariation();
         $variation->InternalItemID = $val;
         $variation->ProductID = $obj->ID;
         //link to product
         $variation->write();
     }
     $varcols = array('->processVariation', '->processVariation1', '->processVariation2', '->processVariation3', '->processVariation4', '->processVariation5', '->processVariation6');
     foreach ($varcols as $col) {
         if (isset($record[$col])) {
             $parts = explode(":", $record[$col]);
             if (count($parts) == 2) {
                 $attributetype = trim($parts[0]);
                 $attributevalues = explode(",", $parts[1]);
                 //get rid of empty values
                 foreach ($attributevalues as $key => $value) {
                     if (!$value || trim($value) == "") {
                         unset($attributevalues[$key]);
                     }
                 }
                 if (count($attributevalues) == 1) {
                     $attributetype = ProductAttributeType::find_or_make($attributetype);
                     foreach ($attributevalues as $key => $value) {
                         $val = trim($value);
                         if ($val != "" && $val != null) {
                             $attributevalues[$key] = $val;
                         }
                         //remove outside spaces from values
                     }
                     $attributetype->addValues($attributevalues);
                     //create and add values to attribute type
                     $obj->VariationAttributes()->add($attributetype);
                     //add variation attribute type to product
                     //TODO: if existing variation, then remove current values
                     //record vairation attribute values (variation1, 2 etc)
                     foreach ($attributetype->convertArrayToValues($attributevalues) as $value) {
                         $variation->AttributeValues()->add($value);
                         break;
                     }
                 }
             }
         }
     }
     //copy db values into variation (InternalItemID, Price, Stock, etc) ...there will be unknowns from extensions.
     $dbfields = $variation->db();
     foreach ($record as $field => $value) {
         if (isset($dbfields[$field])) {
             $variation->{$field} = $value;
         }
     }
     $variation->write();
 }
All Usage Examples Of ProductAttributeType::find_or_make