drl9940 2015-09-18 20:59
浏览 54

Grocery CRUD中的一对多关系

There are examples on the Grocery CRUD website of a one-to-one relationship and a many-to-many relationship, but no example of a one-to-many relationship.

Here is an example: I have car_makes and car_models tables. Each model_id may only belong to one make_id. Each make_id can be associated with many model_ids.

SQFiddle: http://sqlfiddle.com/#!9/c357c

Now, I try to set up Grocery CRUD:

public function cars(){
    $crud = new grocery_CRUD();
    $crud->set_table("car_makes");
    $crud->set_relation("make_id","car_models","model_name");
    $output = $crud->render();
    $this->_example_output($output);    
    }

This doesn't work -- in the grid view, the make_id is suddenly replaced with a model_name, and it's totally wrong. For example, I'm seeing the "Ford Volt" when I clearly specified the model_name Volt is associated with make_id 2, and make_id 2 has a make_name of Chevrolet. When viewing a single record, only one make is visible, when there should be a list.

What I really want to do is make it easy to add additional models to car makes, and see what models are already associated with a make.

How should I configure Grocery CRUD to handle this 1-n relationship?

I need to avoid a many-to-many relationship, because a model may not belong to more than one make. Thanks.

EDIT: A colleague suggests handling models through a model list instead of part of the makes list would be the way to go. This seems reasonable, but less useful.

EDIT 2: Here's the code that allows you to look up a model and assign a make to it:

public function car_models(){
    $crud = new grocery_CRUD();
    $crud->set_table("car_models");
    $crud->set_relation("make_id","car_makes","make_name");
    $output = $crud->render();
    $this->_example_output($output);
    }

Now, I really think it should be the other way around -- you look up a make and assign models to it -- but I don't think Grocery CRUD supports that for 1-n relationships when using set_relation(). That is supported for many-to-many relationships, where you can look up 1 movie and assign many actors to it. Perhaps by using set_relation_n_n() this can be accomplished?

  • 写回答

1条回答 默认 最新

  • dream3323 2015-09-19 06:43
    关注

    Please check this link Grocery CRUD do provide one-to-many relationship and it is also explain everywell using example. set_relation function is for 1-n i.e. one-to-many relationship only, see in documentation it is written in it.

    set_relation

    This is the function and parameter you need to follow

    set_relation( string $field_name , string  $related_table, string  $related_title_field  [, mixed $where [, string $order_by ] ] )
    

    1st parameter should be field name, 2nd is relation table, and 3rd is relation table field name that need to be displayed. Other parameters are optional and you can also use that.

    Go through the documentation properly you will surely get it.

    评论

报告相同问题?

悬赏问题

  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥20 Python安装cvxpy库出问题
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥15 python天天向上类似问题,但没有清零
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 C#调用python代码(python带有库)
  • ¥15 活动选择题。最多可以参加几个项目?
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题