Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Understanding ORM Relationships
  • Hello all, First of all, I just want to say great work on this framwork... Loving every line of it. I have two tables users and persons. Each has a primary key of id. After creating the crud models, I can access the tables and their values just fine. However I would like to define a one-to-one relationship between the two in order to access the value of one table through the field of another. (e.i. $user->person->full_name). However when I try to define these relationships (one-to-one in User Table, belongs to in Persons Table) with the following code, I get a 'Trying to get property of non-object' error. Any help would be very appreciated. As I have spent quite a bit of time trying to find an answer.
    <pre>
    class Model_User extends \Model_Crud
    {
    // Set the table to use
    protected static $_table_name = 'users'; protected static $_has_one = array(
    'profile' => array(
    'key_from' => 'id',
    'model_to' => 'Model_Person',
    'key_to' => 'person',
    'cascade_save' => true,
    'cascade_delete' => false,
    )
    );
    } class Model_Person extends \Model_Crud
    {
    // Set the table to use
    protected static $_table_name = 'persons'; protected static $_belongs_to = array(
    'post' => array(
    'key_from' => 'user',
    'model_to' => 'Model_User',
    'key_to' => 'person',
    'cascade_save' => true,
    'cascade_delete' => false,
    )
    ); }
    </pre>
    Tables:
    Users
    -id
    -username
    -password
    -person (id of person in persons table) Persons
    -id
    -full_name
    -age
  • Thanks,
    I appreciate the response.
    Boy now I feel like the biggest dummy on the block!
  • You're not using the ORM, you're using Model_Crud. Which doesn't support relations.

Howdy, Stranger!

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

In this Discussion