douti9253 2014-06-30 15:49
浏览 30
已采纳

Kohana / PHP - 为“收藏夹”表定义ORM关系

I have 3 tables in the following format.

  • users

id

FirstName

LastName

  • userjobs

id

jobinfo

  • starredjobs

id

user_id

userjob_id

comments

enddate

The 'starredjobs' table holds all the jobs which an user starred/added to favorites. I have defined the following relationships in their respective model files.

  • user.php

protected $_has_many = array( 'starredjobs' => array('model' => 'starredjobs' , 'foreign_key'=>'user_id'),

  • starredjob.php

    protected $_belongs_to = array('user' => array('model' => 'user','foreign_key' => 'user_id'));

    protected $_has_many = array('jobs' => array('model'=> 'userjob', 'foreign_key'=> 'job_id'));

  • userjobs.php

    none

The idea is to retrieve all the starred jobs and details regarding jobs from the user object. A user can 'n' number of jobs and A job can be starred by 'n' number of users.

Am i defining relationships correctly?

  • 写回答

1条回答 默认 最新

  • dongshou6041 2014-07-01 02:06
    关注

    Short answer: No. What you have here is a typical n:m relationship which can easily be used in Kohana using has_many "through" (as is used in the default roles users-relationship). But this doesn't allow for extra attributes in the "middle" table, so you need to use 2 has_many with corresponding belongs_to.

    This can be described in plain English like so:

    • One user has many starredjobs.
    • One job has many starredjobs.
    • One starredjob belongs to one user and one job

    Also consider the difference between far_key and foreign_key (official doc sadly doesn't cover it), but one easy rule to remember: The key in the other table is far away -> it is the far_key.

    This would give you the following

    user.php

    $_has_many = array(
        'starredjobs' => array(
            'model' => 'Starredjob',
            'far_key' => 'user_id'
        )
    );
    

    userjob.php

    $_has_many = array(
        'starredbyuser' => array(
            'model' => 'Starredjob',
            'far_key' => 'userjob_id'
        )
    );
    

    starredjob.php

    $_belongs_to = array(
        'user' => array(
            'model' => 'User',
            'foreign_key' => 'user_id'
        ),
        'job' => array(
            'model' => 'Userjob',
            'foreign_key' => 'userjob_id'
        )
    );
    

    Now you can do various things such as:

    //get all jobs starred by given $user
    foreach ($user->starredjobs->find_all() as $starredjob) {
        //info on userjob via $starredjob->job->jobinfo, etc.
        //info from pivot table via $starredjob->comments, etc.
    }
    
    //get all users that starred a given $userjob
    foreach ($userjob->starredbyuser->find_all() as $starredjob) {
        //info on user via $starredjob->user->FirstName, etc.
        //info from pivot table via $starredjob->comments, etc.
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信小程序 用oss下载 aliyun-oss-sdk-6.18.0.min client报错
  • ¥15 ArcGIS批量裁剪
  • ¥15 labview程序设计
  • ¥15 为什么在配置Linux系统的时候执行脚本总是出现E: Failed to fetch http:L/cn.archive.ubuntu.com
  • ¥15 Cloudreve保存用户组存储空间大小时报错
  • ¥15 伪标签为什么不能作为弱监督语义分割的结果?
  • ¥15 编一个判断一个区间范围内的数字的个位数的立方和是否等于其本身的程序在输入第1组数据后卡住了(语言-c语言)
  • ¥15 Mac版Fiddler Everywhere4.0.1提示强制更新
  • ¥15 android 集成sentry上报时报错。
  • ¥15 抖音看过的视频,缓存在哪个文件