Jarves\Storage\Propel::populateRow PHP Method

populateRow() public method

Generates a row from the propel object using the get*() methods. Resolves *-to-many relations.
public populateRow ( $clazz, $row, $selects, $relations, $relationFields, boolean $permissionCheck = false ) : array
$clazz
$row
$selects
$relations
$relationFields
$permissionCheck boolean
return array
    public function populateRow($clazz, $row, $selects, $relations, $relationFields, $permissionCheck = false)
    {
        $item = new $clazz();
        $item->fromArray($row);
        $newRow = [];
        foreach ($selects as $select) {
            if (strpos($select, '.') === false) {
                $newRow[lcfirst($select)] = $item->{'get' . $select}();
            }
        }
        if (!$relations) {
            return $newRow;
        }
        foreach ($relations as $name => $relation) {
            /** @var $relation \Propel\Runtime\Map\RelationMap */
            if ($relation->getType() != RelationMap::MANY_TO_MANY && $relation->getType() != RelationMap::ONE_TO_MANY) {
                if (isset($relationFields[$name]) && is_array($relationFields[$name])) {
                    $foreignClazz = $relation->getForeignTable()->getClassName();
                    $foreignObj = new $foreignClazz();
                    $foreignRow = array();
                    $allNull = true;
                    foreach ($relationFields[$name] as $col) {
                        if ($row[$name . "." . $col] !== null) {
                            $foreignRow[$col] = $row[$name . "." . $col];
                            $allNull = false;
                        }
                    }
                    if ($allNull) {
                        $newRow[lcfirst($name)] = null;
                    } else {
                        $foreignObj->fromArray($foreignRow);
                        $foreignRow = array();
                        foreach ($relationFields[$name] as $col) {
                            $foreignRow[lcfirst($col)] = $foreignObj->{'get' . $col}();
                        }
                        $newRow[lcfirst($name)] = $foreignRow;
                    }
                }
            } else {
                //many-to-one and many-to-many, we need a extra query
                if (is_array($relationFields[$name]) && ($relationField = $this->getDefinition()->getField($name))) {
                    if (!($relationObjectName = $relationField->getObject())) {
                        $relationObjectName = $this->getDefinition()->getKey();
                        //                            if (!$relationField->getObjectDefinition() || !$relationObjectName = $relationField->getObjectDefinition()->getKey()) {
                        //                                throw new ObjectNotFoundException(sprintf('No object defined for relation `%s`.', $relationField->getId()));
                        //                            }
                    }
                    $sClazz = $relation->getRightTable()->getClassname();
                    $queryName = $sClazz . 'Query';
                    if ($relation->getType() === RelationMap::MANY_TO_MANY) {
                        $filterBy = 'filterBy' . $this->getDefinition()->getId();
                    } else {
                        $filterBy = 'filterBy' . $relation->getSymmetricalRelation()->getName();
                    }
                    $sQuery = $queryName::create()->select($relationFields[$name])->{$filterBy}($item);
                    $condition = null;
                    if ($permissionCheck) {
                        $condition = $this->acl->getListingCondition($relationObjectName);
                    }
                    $sStmt = $this->getStm($sQuery, $condition);
                    $sItems = array();
                    while ($subRow = $sStmt->fetch(\PDO::FETCH_ASSOC)) {
                        $sItem = new $sClazz();
                        $sItem->fromArray($subRow);
                        $temp = array();
                        foreach ($relationFields[$name] as $select) {
                            $temp[lcfirst($select)] = $sItem->{'get' . $select}();
                        }
                        $sItems[] = $temp;
                    }
                } else {
                    $get = 'get' . $relation->getPluralName();
                    $sItems = $item->{$get}();
                }
                if ($sItems instanceof ObjectCollection) {
                    $newRow[lcfirst($name)] = $sItems->toArray(null, null, TableMap::TYPE_CAMELNAME) ?: null;
                } else {
                    if (is_array($sItems) && $sItems) {
                        $newRow[lcfirst($name)] = $sItems;
                    } else {
                        $newRow[lcfirst($name)] = null;
                    }
                }
            }
        }
        return $newRow;
    }

Usage Example

Ejemplo n.º 1
0
 public function populateRow($clazz, $row, $selects, $relations, $relationFields, $permissionCheck = false)
 {
     $row = parent::populateRow($clazz, $row, $selects, $relations, $relationFields, $permissionCheck);
     if ($row) {
         $row['url'] = $this->pageStack->getNodeUrl($row['id']);
     }
     return $row;
 }