Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Model Belongs to and Has One - Relationships Issue
  • I'm having relationship issues (lol)...

    I'm fairly new to FuelPHP so apologies if this is a bit of a novice question. It's about the ORM (which is amazing by the way).

    I have two models 'Item' and 'Offer'. An Item can have many Offers - so Offer belongs to Item. But, an Offer also has one different item.

    Here's my (simplified) Item Model:

    class Model_Item extends \Orm\Model
    {

    protected static $_belongs_to = array('user', 'offer');
    protected static $_has_many = array('offers');

    protected static $_properties = array(
    'id',
    'user_id',
    );

    protected static $_observers = array(
    'Orm\Observer_CreatedAt' => array(
    'events' => array('before_insert'),
    'mysql_timestamp' => false,
    ),
    'Orm\Observer_UpdatedAt' => array(
    'events' => array('before_save'),
    'mysql_timestamp' => false,
    ),
    );
    }

    Here's my (simplified) Offer model:

    class Model_Offer extends \Orm\Model
    {
    protected static $_belongs_to = array('item');
    protected static $_has_one = array('item');
     
    protected static $_properties = array(
    'id',
    'item_id',
    'owneditem_id', // <- THIS IS THE ITEM IT OWNS
    );

    protected static $_observers = array(
    'Orm\Observer_CreatedAt' => array(
    'events' => array('before_insert'),
    'mysql_timestamp' => false,
    ),
    'Orm\Observer_UpdatedAt' => array(
    'events' => array('before_save'),
    'mysql_timestamp' => false,
    ),
    );
    }

    As you can see I need to be able to save the 'owning' item in the offer model, as well as an item that it 'owns', but I cannot redeclare item_id because it is already taken by the owner of the offer. How can I tell Fuel and the ORM that owneditem_id is an item object and not just an integer in a way that I can access that item using $myOffer->owneditem?

    Sorry it's hard to explain...
  • Write out the relation definition as described in the docs, and use the correct key_from and key_to fields.

    If you use a definition like array('item'); the ORM will try to expand that to a proper definition using set rules. For keys, that means assuming that key_from is 'id', and key_to is 'item_id'. In this case, you have a non-standard FK name, so you have to write the relationship definition yourself.
  • vesselinvvesselinv
    Accepted Answer
    protected static $_has_many       = array(
    'offers' => array(
    'key_from' => 'id',
    'model_to' => 'Model_Offer',
    'key_to' => 'item_id',
    'cascade_save' => false,
    'cascade_delete' => false,
    ));

    This is how you'd define a HasMany relationship in your Item model.
  • Cool thanks.

    I got it in the end!

    Offer relationships:

    protected static $_has_one = array(
    //(offered item)
    'offereditem' => array(
    'key_from' => 'offereditem_id',
    'model_to' => 'Model_Item',
          'key_to' => 'id',
          'cascade_save' => true,
          'cascade_delete' => false,
    )
    );

    and item model relationships:

    protected static $_belongs_to = array('user', 'item');
    protected static $_has_many = array('offers');

    with migrations:

    //Add OfferedItem to Offers Migration
    oil generate migration add_offereditem_to_offers offereditem:int

    //Rename OfferedItem to OfferedItem_ID in Offers Migration
    oil generate migration rename_field_offereditem_to_offereditem_id_in_offers

Howdy, Stranger!

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

In this Discussion