Many to many with extra fields

73 views
Skip to first unread message

Ariel Souza

unread,
Aug 9, 2016, 6:35:40 PM8/9/16
to redbeanphp

I'm developing a system where i need to keep track of animal diseases and when they were cured.
I need to create a many to many between the animal and his diseases and keep when it was added (when he got infected), cured and when it was cured.

I'm was wondering if i should go with the many-to-many relation or i should create an extra model for this case.

gabor

unread,
Aug 11, 2016, 6:03:48 PM8/11/16
to redbeanphp

Hi,

When you say 'many-to-many' with 'extra fields' you almost always have your answer: it's not really many-to-many, its just another concept.
Most many-to-many relationships are hidden concepts, the emerge as soon as the extra (read intrinsic) properties are added.

Of course I am no expert in your domain but I would say it consists of the following entities:

disease
cure
treatment - has a disease_id and a cure_id

Note that even if you use an entity instead of an N-M, you can still use that entity as a N-M to quickly gain access to a related bean, use the via()
function to do this.

cheers
Gabor

Ariel Souza

unread,
Aug 21, 2016, 5:19:44 PM8/21/16
to redbeanphp
Hy Gabor, thanks for the help. I will try to implement your idea. It sounds way better than what i already did.

I managed to get it working byt later editing the table and adding the extra field i needed.

Another thing, i'm having some trouble with a relation where it shows me a undefined index _type. What could cause this problem ? I tried to find it on the source code but i couldn't find it

<br />
<b>Notice</b>:  Undefined index: _type in <b>Lib\RedBean\OODBBean.php</b> on line <b>487</b><br />
<br />
<b>Fatal error</b>:  Uncaught exception 'Lib\RedBean\RedException' with message 'Bean has incomplete Meta Information II' in Lib\RedBean\Repository.php:304
Stack trace:
#0 Lib\RedBean\Repository\Fluid.php(262): Lib\RedBean\Repository-&gt;check(Object(Lib\RedBean\OODBBean))
#1 Lib\RedBean\OODB.php(235): Lib\RedBean\Repository\Fluid-&gt;dispense(NULL, 1, false)
#2 Lib\RedBean\OODBBean.php(487): Lib\RedBean\OODB-&gt;dispense(NULL)
#3 Lib\RedBean\Plugin\ModelValidation.php(79): Lib\RedBean\OODBBean-&gt;import(Array)
#4 index.php(15): Lib\RedBean\Plugin\ModelValidation::validate(Object(Lib\RedBean\OODBBean))
#5 [internal function]: {closure}(Object(Lib\RedBean\OODBBean))
#6 Lib\RedBean\Facade.php(1912): call_user_func_array(Object(Closure), Array)
#7 Models\DAL.php(80): Lib\RedBean\Facad in <b>Lib\RedBean\Repository.php</b> on line <b>304</b><br />

gabor

unread,
Aug 23, 2016, 5:52:25 PM8/23/16
to redbeanphp

You're trying to dispense a bean without a type (type seems to be NULL here).
Maybe indirectly ?
Reply all
Reply to author
Forward
0 new messages