Thursday, January 22, 2015

Re: Cake 3.0 - get source table and schema of each field in a recordset

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+unsubscribe@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.

No comments: