Flourish PHP Unframework
This is an archived copy of the forum for reference purposes

Creating one-to-many related records

posted by aris 8 years ago

Hello.

As in previous projects, I'm using Flourish in my latest one. This time I have to link two tables, 'companies' and 'users', through a one-to-many relation.

According to fCore::expose($schema->getRelationships()), the relations for 'users' are:

Array
(
    [many-to-one] => Array
        (
            [0] => Array
                (
                    [table] => users
                    [column] => id_company
                    [related_table] => companies
                    [related_column] => id
                )
        
        )
    
)

And for 'companies' (yes, there is a self-reference in this table, as well):


Array
(
    [many-to-one] => Array
        (
            [0] => Array
                (
                    [table] => companies
                    [column] => id_agency
                    [related_table] => companies
                    [related_column] => id
                )
        
        )
  
    [one-to-many] => Array
        (
            [0] => Array
                (
                    [table] => companies
                    [column] => id
                    [related_table] => companies
                    [related_column] => id_agency
                    [on_delete] => no_action
                    [on_update] => no_action
                )
        
            [1] => Array
                (
                    [table] => companies
                    [column] => id
                    [related_table] => users
                    [related_column] => id_company
                    [on_delete] => cascade
                    [on_update] => no_action
                )
        
        )
)

I have a unique form to collect both the user and company data simultaneously, although from there it should result in a 'user' record and a 'company' record, each stored in its own table and keeping the relation. Of course, I could use the company data to build a record, storing it, getting its id and using to prepare and store the related user record, but is there some more direct way to get both records stored in a same operation and with minimal code?

Regards, A.