Here's what i ended up with

class TableHelper extends Helper {

public function build(ResultSet $resultset)
{
 foreach ($resultset as $result) {
 $schema = $this->getSchema($result->source(), $result->toArray());
 break;
 }
 debug($schema);
 //do stuff
}

public function getSchema($table, $data)
{
 $schema = [];
 foreach ($data as $field => $value) {
 if (is_array($value)) {
 $schema[$field] = $this->getSchema($field, $value);
 } else {
 $schema[$field] = TableRegistry::get($table)->schema()->columnType($field);
 }
 }
 return $schema; 
}

}

here's the result

[
        'id' => 'integer',
        'name' => 'string',
        'group_id' => 'integer',
        'value_1' => 'decimal',
        'value_2' => 'decimal',
        'groups' => [

                'id' => 'integer'

'name' => 'string' ] ]


i'm looking to load the whole schema for each field, how can i do? i still 
haven't found a way.

Looking forward for your advices/enhanchments :)

PS: how can i access the query of a recordset? i'm not good at oop :(


Il giorno giovedì 22 gennaio 2015 16:40:54 UTC+1, Ernesto ha scritto:
>
> Ok i'll try.
>
> i'll let you know
>
> Thank you very much
>
> Il giorno giovedì 22 gennaio 2015 15:30:51 UTC+1, José Lorenzo ha scritto:
>>
>> Associations are already loaded, as you define them in the initialize() 
>> method of the table.
>>
>> You can inspect the associations tree in a query by calling 
>> $query->eagerLoader()->normalized($query->repository());
>>
>> On Thursday, January 22, 2015 at 2:47:35 PM UTC+1, Ernesto wrote:
>>>
>>> The "inspect the contain tree" way sounds better.
>>>
>>> i'll try to explain:
>>>
>>> With your method you will end up loading every belongsTo and hasOne 
>>> association, even if not used.
>>> The table i'm using right now has 3 belongsTo relationships but i need 
>>> to show only one
>>>
>>> On the second hand: first level association are not always enough.
>>> This worsen the problem quite a bit.
>>> if i use your method i must recursively loop throug tables and theyr 
>>> relationships, loading a ton of useless classes.
>>>
>>> What do you think?
>>>
>>> Il giorno giovedì 22 gennaio 2015 12:56:19 UTC+1, José Lorenzo ha 
>>> scritto:
>>>>
>>>> The associations used in a find are represented in a Tree-like 
>>>> structure. I guess in your case it will be more than enough getting the 
>>>> first-level associations
>>>> of type belongsTo and hasOne, as those are the only types that can be 
>>>> represented in a html table (they are in the same level of nesting)
>>>>
>>>> So to get associations of a table of type belongsTo and hasOne:
>>>>
>>>> $firstLevelAssociations = 
>>>> collection($mainTable->associations()->type('HasOne'))
>>>>     ->append($mainTable->associations()->type('BelongsTo'));
>>>>
>>>> $schemas = [];
>>>> foreach ($firstLevelAssociations as $association) {
>>>>    $schemas[$association->property()] = $association->schema();
>>>> }
>>>>
>>>> At the end of the loop you will have an array of property names 
>>>> pointing to the table schema. So, finally:
>>>>
>>>> foreach ($schemas as $property => $schema) {
>>>>    if ($singleResultFromQuery->has($property)) {
>>>>         // Do stuff with the schema
>>>>    }
>>>> }
>>>>
>>>>
>>>> This is one way to do it. The other way is inspecting the query object 
>>>> to get the 'contain' tree. Let me know if you need to know more about it :)
>>>> On Thursday, January 22, 2015 at 11:01:35 AM UTC+1, Ernesto wrote:
>>>>>
>>>>> what about getting all the associations used in a record set?
>>>>>
>>>>> Il giorno giovedì 22 gennaio 2015 10:02:02 UTC+1, José Lorenzo ha 
>>>>> scritto:
>>>>>
>>>>>> foreach ($results as $result) {
>>>>>>     $source = $result->source();
>>>>>>     $sourceSchema = TableRegistry::get($source)->schema();
>>>>>> }
>>>>>>
>>>>>> Usually all records in a result set are of the same table, so you 
>>>>>> on;y need to do that for the first result.
>>>>>>
>>>>>> If you have associations, then you can get the property in the result:
>>>>>>
>>>>>> $associationSource = $result->association_property->source();
>>>>>>
>>>>>>
>>>>>> On Thursday, January 22, 2015 at 9:07:31 AM UTC+1, Ernesto wrote:
>>>>>>>
>>>>>>> Hi all
>>>>>>>
>>>>>>> i'm migrating an HtmlTableHelper from a Cake 2.x project to  new 3.0 
>>>>>>> one
>>>>>>>
>>>>>>> The main function of this helper scans the array of data passed as 
>>>>>>> argument and uses ClassRegistry to obtain schemas for each Model.field.
>>>>>>> Later on these informations are used to format the TDs (text fields 
>>>>>>> justified to the left, number fields justified to the right and so on).
>>>>>>>
>>>>>>> in 3.0 data arrays are gone, replaced by resultsets.
>>>>>>>
>>>>>>> I tried to rewrite a similar approach but i'm struggling with Cake's 
>>>>>>> new ORM
>>>>>>>
>>>>>>> what's the best way to get source table and schema of each field in 
>>>>>>> a recordset?
>>>>>>>
>>>>>>> Thank you very much
>>>>>>>
>>>>>>>
>>>>>>>

-- 
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP

--- 
You received this message because you are subscribed to the Google Groups 
"CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to cake-php+unsubscr...@googlegroups.com.
To post to this group, send email to cake-php@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/d/optout.

Reply via email to