douju3911 2013-06-13 11:26
浏览 40
已采纳

Kohana 3.3表结构

I have a simple crud application in Kohana 3.3, with a few different types of data or models. Let’s say those models are:

  • Users
  • Locations
  • Skills

I have a table for each of those models, but the tables aren’t related to one another in any way. I’ve been trying to define relationships with ORM, but I’m still confused. As an example:

I have a number of locations. Each location has many users.

I know I can define that with:

class Model_Location extends ORM {

    /**
     * A location has many users
     *
     * @var array Relationships
     */
    protected $_has_many = array(
        'users' => array('model' => 'user'),
    );

}

As I understand it, I can connect the two by referencing the ID of the parent location from the row in the user table. However, what if each user can belong to many locations? Am I supposed to store serialised data in a foreign key? Should I create a “look-up table”? If so, how should it look?

How can I for example, query the database for a location and all users attached to it? Is ORM even the right technology to be using for this kind of thing?

  • 写回答

1条回答 默认 最新

  • dqsa17330 2013-06-13 11:41
    关注

    You are looking for the through parameter, have a look at the docs.

    You need an extra table that stores the ids of both models

    table users_locations, fields: user_id and location_id (both indexed, of course)

    And in your model:

    protected $_has_many = array(
        'users' => array(
            'model' => 'user'
            'through' => 'users_locations',
        ),
    );
    

    And vice versa in the user model:

    protected $_has_many = array(
        'locations' => array(
            'model' => 'location'
            'through' => 'users_locations',
        ),
    );
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥15 树莓派5怎么用camera module 3啊
  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事: