Pimcore\Model\Property::setDataFromResource PHP Méthode

setDataFromResource() public méthode

Takes data from resource and convert it to internal objects
public setDataFromResource ( mixed $data ) : static
$data mixed
Résultat static
    public function setDataFromResource($data)
    {
        // IMPORTANT: if you use this method be sure that the type of the property is already set
        // do not set data for object, asset and document here, this is loaded dynamically when calling $this->getData();
        if ($this->type == "date") {
            $this->data = \Pimcore\Tool\Serialize::unserialize($data);
        } elseif ($this->type == "bool") {
            $this->data = false;
            if (!empty($data)) {
                $this->data = true;
            }
        } else {
            // plain text
            $this->data = $data;
        }
        return $this;
    }

Usage Example

Exemple #1
0
 /**
  * Get the properties for the object from database and assign it
  *
  * @return void
  */
 public function getProperties($onlyInherited = false)
 {
     $properties = array();
     // collect properties via parent - ids
     $parentIds = $this->getParentIds();
     $propertiesRaw = $this->db->fetchAll("SELECT * FROM properties WHERE ((cid IN (" . implode(",", $parentIds) . ") AND inheritable = 1) OR cid = ? )  AND ctype='object'", $this->model->getId());
     // because this should be faster than mysql
     usort($propertiesRaw, function ($left, $right) {
         return strcmp($left["cpath"], $right["cpath"]);
     });
     foreach ($propertiesRaw as $propertyRaw) {
         try {
             $property = new Model\Property();
             $property->setType($propertyRaw["type"]);
             $property->setCid($this->model->getId());
             $property->setName($propertyRaw["name"]);
             $property->setCtype("object");
             $property->setDataFromResource($propertyRaw["data"]);
             $property->setInherited(true);
             if ($propertyRaw["cid"] == $this->model->getId()) {
                 $property->setInherited(false);
             }
             $property->setInheritable(false);
             if ($propertyRaw["inheritable"]) {
                 $property->setInheritable(true);
             }
             if ($onlyInherited && !$property->getInherited()) {
                 continue;
             }
             $properties[$propertyRaw["name"]] = $property;
         } catch (\Exception $e) {
             \Logger::error("can't add property " . $propertyRaw["name"] . " to object " . $this->model->getFullPath());
         }
     }
     // if only inherited then only return it and dont call the setter in the model
     if ($onlyInherited) {
         return $properties;
     }
     $this->model->setProperties($properties);
     return $properties;
 }