Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
ORM Relations with Package ORM
  • Hello. At first, sorry for my bad englisch. Can someone explain me how i create the relations for this. Tables:
    books
    id    integer
    title varchar(50)
    
    authors
    id    integer
    name  varchar(50)
    

    Classes:
    class Model_Book extends Orm\Model {
       protected static $_properties = array('id', 'title' );
       protected static $_has_one = array('author' );
    }
    
    class Model_Author extends Orm\Model {
       protected static $_properties = array('id', 'name' ) ;
       protected static $_has_many = array('books' ) ;
    }
    

    Test:
    // Read book with id=1
    $book = Model_Book::find(1);
    
    // Dump Book
    Debug::dump($book);
    

    Looks good:
    Variable #1:
    object(Model_Book)#14 (6) {
      ["_is_new":"Orm\Model":private]=>
      bool(false)
      ["_frozen":"Orm\Model":private]=>
      bool(false)
      ["_data":"Orm\Model":private]=>
      array(2) {
        ["id"]=>
        string(1) "1"
        ["title"]=>
        string(6) "Book 1"
      }
      ["_original":"Orm\Model":private]=>
      array(2) {
        ["id"]=>
        string(1) "1"
        ["title"]=>
        string(6) "Book 1"
      }
      ["_data_relations":"Orm\Model":private]=>
      array(0) {
      }
      ["_original_relations":"Orm\Model":private]=>
      array(0) {
      }
    }
    
    // Now dump author
    Debug::dump($book->author);
    

    Looks not good:
    Fuel\Core\Database_Exception [ 1054 ]: Unknown column 't0.book_id' in 'where clause' [ 
    SELECT `t0`.`id` AS `t0_c0`, `t0`.`name` AS `t0_c1` FROM `authors` AS `t0` WHERE `t0`.`book_id` = '1' LIMIT 1 ]
    
    COREPATH/classes/database/mysqli/connection.php @ line 193
    
    188                
    189                $this->trans_errors[] = $this->_connection->errno.': '.$this->_connection->error.' [ '.$sql.' ]';
    190            }
    191            else
    192            {
    193                throw new \Database_Exception($this->_connection->error.' [ '.$sql.' ]', $this->_connection->errno);
    194            }
    195        }
    196
    197        if (isset($benchmark))
    198        {
    

    I hope some one can help me. Thanks.
  • Next time put your code between [ code] and [/ code] tags (without the spaces), I did it for you this time. The problem is that there's no way in your table setup for it to know which books belong to which authors. You need to change the relationship for books to author from has_one to belongs_to and you need to add a int field author_id to your books table. After that the author's ID can be saved with the book and the system knows which author has written the book and which books belong to an author.
  • Hello Jelmer. For the missed tags, sorry. I try this and give you a feedback tomorrow. Greets
    Peter
  • Hello Jelmer. Now it works. Thanks for your help. Peter
  • When trying to delete a record I get this error: Orm\FrozenObject [ Error ]: No changes allowed. What does this error mean?
  • @David
    That should get its own topic, post it there with the code that precedes it and I'll try to help you out.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion