コード例 #1
0
ファイル: SQL.php プロジェクト: nOoiser/SimplOnPHP
    public function readElements(\SimplOn\Elements\Element &$element, $returnAs = 'array')
    {
        /*
         * Reads the storage "table" for each class, usually it's the same 
         * as the class name but could be otherwise (any class may use 
         * any storage if wanted)
         */
        $storages = is_array($element->storage()) ? $element->storage() : array($element->getClass() => $element->storage());
        /*
         * Select only the readable elements
         */
        foreach ($element->processData('doRead') as $dataInfo) {
            foreach ($dataInfo as $fieldInfo) {
                $fields[] = $fieldInfo[0];
            }
        }
        /*
         * 
         */
        foreach ($storages as $class => $storage) {
            $storage_fields = $fields;
            // id as id, 'id' as field_id, 'fe' as storage
            $addFields = '"' . strtr($class, array('\\' => '\\\\')) . '" as `SimplOn_class`, ' . '"' . $element->field_id() . '" as `SimplOn_field_id`, ' . '"' . $element->field_id() . '" as `SimplOn_id`, ' . '"' . $element->field_id() . '", `';
            $where = $this->filterCriteria($element);
            $selects[] = '(SELECT ' . $addFields . implode('`, `', $storage_fields) . '` FROM ' . $storage . ' ' . ($where ? 'WHERE ' . $where : '') . ')';
        }
        // @todo: where and order by (and limit)
        $query_string = implode("\n" . ' UNION ', $selects) . '
			ORDER BY SimplOn_id desc
		';
        $query = $this->db->prepare($query_string);
        // Obtains values
        $values = $this->obtainValues($element, $query_string);
        // Executes the query and returns the results
        $query->execute($values);
        /**
         * Example:
         * $array_of_datas = array(
         *      array(
         *          'SimplOn_class' => 'Home',
         *          'SimplOn_field_id' => 'id',
         *          'SimplOn_id' => 1,
         *          'id' => 1,
         *          ...
         *      ),
         *      ...
         * );
         */
        $array_of_datas = $query->fetchAll();
        switch ($returnAs) {
            case 'array':
                $return = $array_of_datas;
                break;
            case 'Elements':
                $return = array();
                foreach ($array_of_datas as $datas) {
                    $return[] = new $datas['SimplOn_class']($datas);
                }
                break;
            default:
                //trigger error
        }
        return $return;
    }
コード例 #2
0
ファイル: SQL.php プロジェクト: simplonphp/simplonphp
 function countElements(\SimplOn\Elements\Element &$element, $group = null)
 {
     if (isset($group)) {
         $group = implode(',', $group);
     }
     $storages = is_array($element->storage()) ? $element->storage() : array($element->getClass() => $element->storage());
     foreach ($element->processData('doRead') as $dataInfo) {
         foreach ($dataInfo as $fieldInfo) {
             $fields[] = $fieldInfo[0];
         }
     }
     foreach ($storages as $class => $storage) {
         $storage_fields = $fields;
         $addFields = '"' . strtr($class, array('\\' => '\\\\')) . '" as `SimplOn_class`, ' . '"' . $element->field_id() . '" as `SimplOn_field_id`, ' . '"' . $element->field_id() . '" as `SimplOn_id`, ' . '"' . $element->field_id() . '", ';
         $where = $this->filterCriteria($element);
         $selects[] = '(SELECT ' . $addFields . implode(', ', $storage_fields) . ' FROM ' . $storage . ' ' . ($where ? 'WHERE ' . $where : '') . ' ' . ($group ? 'GROUP BY ' . $group : '') . ')';
     }
     $query_string = implode("\n" . ' UNION ', $selects) . 'ORDER BY SimplOn_id desc';
     $query = $this->db->prepare($query_string);
     $values = $this->obtainValues($element, $query_string);
     $query->execute($values);
     $results = $query->fetchAll();
     $totalRecords = sizeof($results);
     return $totalRecords;
 }