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 Macbookpro 连接热点正常上网,连接不了Wi-Fi。
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析