Search code examples
codeigniterormone-to-manyforeign-key-relationshipredbean

Using simple relation One-To-Many RedBean and CodeIgniter


In my new project, I decided that I will use the RedBean ORM system with CodeIgniter PHP Framework.

How can I get all records from a table with a simple relation? I know that I can use R::exec or R::getAll queries, but I want to make sure if there any other solution(s).

TABLE STRUCTURE:

languages

  • id
  • title

categories

  • id
  • language_id
  • title

The field language_id in the table categories is related with the id field in the table languages.

MY GOAL:

SELECT l.title, c.* 
FROM categories AS c 
LEFT JOIN languages AS l 
    ON (c.language_id = l.id)

Any solutions?


Solution

  • list($language1, $language2, $language3) = R::dispense('language', 3);
    $language1->title = "PHP";
    $language2->title = "Python";
    $language3->title = "Ruby";
    
    list($category1, $category2, $category3) = R::dispense('category', 3);
    $category1->title = "CodeIgniter";
    $category2->title = "Django";
    $category3->title = "Rails";
    
    
    R::store($language1);R::store($language2);R::store($language3);
    R::store($category1);R::store($category2);R::store($category3);
    
    
    $category1->language = $language1;
    $category2->language = $language2;
    $category2->language = $language3;
    
    
    R::store($category1);R::store($category2);R::store($category3);
    
    $categories = $language1->ownCategory;