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

one to many

posted by mungiu 8 years ago

how i can get related record, now i receive Message: The table proprietati is not in a *-to-one relationship with the table costuri

fORM::defineActiveRecordClass("Proprietati");fORM::defineActiveRecordClass("Costuri");
        $proprietati = fRecordSet::build("Proprietati");
        //$costuri = fRecordSet::build("Costuri");
        //foreach ($proprietati as $value) {
                $costuri = $proprietati->buildCosturi();

        //}

Do you have the relations in the schema?

Also, you can try with the method createTableName('relation_field');

posted by jmtucu 8 years ago

i have one to many proprietati have more costuri

posted by mungiu 8 years ago

Try with this, I have Services and Categories, also in a relation one to many

$services = fRecordSet::build('Service');
foreach ($services as $service):
  $cs = $service->createCategoryService('category_id');
  echo $service->prepareName().' - '.$cs->prepareCategoryName();
endforeach;
posted by jmtucu 8 years ago

i dont want to find what category have a service i want all services for all categories.

posted by mungiu 8 years ago

So the result of fRecordSet::build() is an fRecordSet object. The only way you can call build{RealtedClass}() on an fRecordSet is if each record in the record set is in a *-to-one relationship with the related class.

In you case you have multiple costuri per proprietati. Are you looking for a list of all costuri that are related to a proprietati? If so, the simplest and fastest way is:

// This assumes the foreign key to proprietati is proprietati_id
$costuri = fRecordSet::build("Costuri", array('proprietati_id!=' => NULL));

If you are looking for something else, let me know and I can help you get it.

posted by wbond 8 years ago

i don't understand if i want to receive particular costuri for every proprietati how that can help me how does flourish know what costuri have proprietati, maybe like this foreach ($proprietati as $value) { // $costuri = fRecordSet::build("Costuri",array("id="=>$value->getId())); // // } but a question is if i print objesc have sometning like this related_records:protected => Array ( ) where is all the time empty, what was that useful. how in this case schema many to one is useful. pls explain me.

posted by mungiu 8 years ago

rectify foreach ($proprietati as $value) { // $costuri = fRecordSetbuild("Costuri",array("contract="$valuegetId())); whete costuri is foreign key from proprietati

posted by anonymous 8 years ago

I think I will be better able to help you if I know what your database schema looks like.

If you want the costuri for each propretati, and there are multiple costuri that can reference a single proprietati, you would use the following code:

foreach ($proprietatis as $proprietati) {
    $costuris = $proprietati->buildCosturis();
}

The build action for related records will create an fRecordSet.

If there is only one costuri per proprietati, use the following code:

foreach ($proprietatis as $proprietati) {
    $costuri = $proprietati->createCosturi();
}

The create action will construct a single fActiveRecord object of the appropriate class.

The related_records attribute of an fActiveRecord object is an internal cache that Flourish uses to reduce database calls.

posted by wbond 8 years ago

tanks

posted by anonymous 8 years ago