Search code examples
phpmysqlredbean

how to add a child to existing onetomany relationship in redbeanphp?


I am using redbeanphp as my project ORM. I have a table named 'user' witch has one to many relation with 'filter' table. every one user can have many filter. for adding filter to user i use this code :

$user->ownFilterList[] = $filter; 

this code use work fine when 'ownFilterList' has no filter. but when I add a filter to existing list it throw this exception. how can I add a filter to existing list in redbeanphp?


Solution

  • I finally found a solution.

    if we want only add new items to existing ownList and removing or modifying ownList is not important we can use this method:

    $user->ownFilterList = array_merg($user->ownFilterList, $newFilters);
    

    and then item of $newfilters will be added to Filter table with relation to $user;

    we can also use array_push to adding one item to existing list.