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 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀