Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Again about relations
  • Hi, I followed the tutorial on net tuts, and following but modifying for my needs I created two models: Projects and Donations I like to think that one project has many donation and 1 donation belongs to one project. So I have my Models like that: Donation:
    class Model_Donation extends \Orm\Model
    {
     protected static $_belongs_to = array('project');
    

    Project:
    class Model_Project extends \Orm\Model
    {
     protected static $_has_many = array('donations') ;
    

    Now I have entered manually 1 project and 1 donation
    tables as such: Donations:
    id
    amount
    project_id Projects:
    id
    name
    slug
    target
    description I've tried the console to see the relationship but it goes:
     $d = Model_Donation::find(1);
    >>> $d
    Model_Donation::__set_state(array(
       '_is_new' => false,
       '_frozen' => false,
       '_data' => 
      array (
        'id' => '1',
        'amount' => '500',
        'project_id' => '1',
        'created_at' => '1328175979',
        'updated_at' => '1328175979',
      ),
       '_original' => 
      array (
        'id' => '1',
        'amount' => '500',
        'project_id' => '1',
        'created_at' => '1328175979',
        'updated_at' => '1328175979',
      ),
       '_data_relations' => 
      array (
      ),
       '_original_relations' => 
      array (
      ),
       '_view' => NULL,
       '_iterable' => 
      array (
      ),
    ))
    

    Any help would be great. Thanks
  • You haven't requested the relation in any way, the ORM won't fetch relations automaticly - that would be massively stupid for requireing processing/memory without it being necessary. You have to either:
    - eager load, thus put it in the find and have it fetched directly: Model_Donation::find(1, array('related' => array('project')));
    - lazy load, thus request the property later on the $donation object which will trigger a second query for the project: $donation->project All of this is documented by the way, which leads me to a RTFM recommendation before asking a question.

Howdy, Stranger!

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

In this Discussion