Trees

56 views
Skip to first unread message

Mike Whiting

unread,
Aug 31, 2022, 6:22:15 PM8/31/22
to redbeanphp
Hi,

I'm not sure I'm understanding trees correctly.

I have ready the docs and I concluded that it might be possible to add
children to a bean by using the regular one-to-many syntax.

e.g.

$newComp = \R::dispense('component');
$newComp->name = "Foo";
$component = \R::find('component', 1);
$component->ownComponentList[] = $newComp;
\R::store($component);

However this only creates a new column on the component table
"compnent_id" with the new component id added. However, I want to
have many children!

The docs describe the canes example which uses a "ownCane" syntax but
this only has the same effect ownComponentList. i.e. a single child
created in the component table as a foreign key.

If I attempt to add multiple children like this...

$component->ownComponentList[] = $newComp;
$component->ownComponentList[] = \R::find('component', 12);

...I get an array to string conversion error.

What am I missing here?

Cheers!

Benoit Pruneau

unread,
Sep 1, 2022, 11:30:33 AM9/1/22
to redbeanphp
The method to retrieve a bean with an ID is R::load(), not R::find().

R::load('component', 1) is equivalent to R::findOneOrDispense('component','id=1').

Mike Whiting

unread,
Sep 1, 2022, 11:48:07 AM9/1/22
to redbeanphp
Sorry my bad. Please ignore that mistake. The rest still applies.

$component->ownComponent[] = \R::load(12);

or $component->ownComponentList[] = \R::load(12);

Both create a foreign key in the component table to a single component
rather than a join table.

Thanks
> --
> You received this message because you are subscribed to the Google Groups "redbeanphp" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to redbeanorm+...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/redbeanorm/20db691c-6a4c-4784-9be0-0d2022354f74n%40googlegroups.com.

Mike Whiting

unread,
Sep 1, 2022, 12:28:00 PM9/1/22
to Mike Whiting, redbeanphp

I can do $component->sharedComponentList[] = \R::load(‘component’, 12); and this creates a join table but I’m not sure if this technically still a tree.


Sent from my iPhone

> On 1 Sep 2022, at 16:47, Mike Whiting <mi...@ai-em.net> wrote:
>
> Sorry my bad. Please ignore that mistake. The rest still applies.

Benoit Pruneau

unread,
Sep 1, 2022, 2:11:18 PM9/1/22
to redbeanphp
Yes, it does create a foreign key in the component table.

In the field name 'component_id', you must understand 'Parent Id'.

Yes, a component can have many children components, but a child component must have only one parent component. The link is not set in the parent component, but in all children components.

$parentComponent = R::dispense('component');
$childComponent1 = R::dispense('component');
$childComponent2 = R::dispense('component');

$parentComponent->name='I am the parent!';
$childComponent1->name='I am the first child!';
$childComponent2->name='I am the second child!';

$parentComponent->ownComponentList[] = $childComponent1;
$parentComponent->ownComponentList[] = $childComponent2;
R::store($parentComponent);

If the table does not exist, this will create it with 3 records:

+----+--------------------------+--------------+
| id | name                     | component_id |
+----+--------------------------+--------------+
|  1 | I am the parent!         |       (null) |
|  2 | I am the first child!    |            1 |
|  3 | I am the second child!   |            1 |
+----+--------------------------+--------------+

If you now load the parent component, you can retrieve its children :

$p = R::load('component', 1);
echo $p->name;
foreach($p->ownComponentList as $c) {
    echo $c->name;

Mike Whiting

unread,
Sep 2, 2022, 5:32:12 AM9/2/22
to redbeanphp
Thanks, but that's not really answering my question.

I think I've come to the conclusion that the Trees api is not
available when dealing with many-many relationships.
> To view this discussion on the web visit https://groups.google.com/d/msgid/redbeanorm/1a0a8188-f3bc-40f8-b1fc-d4fb4b265053n%40googlegroups.com.

Mike Whiting

unread,
Sep 2, 2022, 6:10:48 AM9/2/22
to redbeanphp
I'm now stuck on Link Beans when dealing with many-many relationships
on the same table.

I am able to create the relationship by using:

$newComp = \R::load('component', 12);
$bean->link('component_component', [
'quantity' => 10,
])->component2 = $newComp;

This means that I can access the child beans in the conventional way using:

foreach($component->sharedComponentList as $c) {
echo $c->name; // prints child names
}

I can also access the links beans using:

$component = \R::load('component', 28);
foreach ($component->ownComponentComponentList as $link) {
echo $link->quantity; // prints 10
}

However when looping through the sharedComponentsList I can't
access the link bean from the other direction. i.e.

foreach($component->sharedComponentList as $c) {
echo c->ownComponentComponentList->quantity // null
}

Perhaps this is a limitation due to the table name being the same but
just wondering if there's a workaround.

Perhaps the best thing is to create a new Entity called "Dependency"
with "parent", and "child" fields where
I can also store the quantities.

Thanks

Benoit Pruneau

unread,
Sep 2, 2022, 11:45:42 AM9/2/22
to redbeanphp
If a child component can have many parents, this is not a tree.

Benoit Pruneau

unread,
Sep 2, 2022, 11:53:54 AM9/2/22
to redbeanphp
Ok your concern is about storing a quantity...

Effectively, the best way is to create a new Entity "Dependency". It will be clearer than "component_component"...
Reply all
Reply to author
Forward
0 new messages