duanquannan0593 2016-02-04 22:40
浏览 113
已采纳

外键一对多(postgresql,php,yii2)

Heloo,

I just beginner with yii2 and postgresql. I have problem like this:

First table:

tbl_printer (PK dept_id and id)

| dept_id | id | name |

Second table

tbl_printer_group (PK dept_id and id)

| dept_id | id | printer1 | printer2 | printer3 |

printer1, printer2, printer3 data/value from tbl_printer.id

I want to 'DISALLOWED to DELETE' tbl_printer IF printer (id) STILL IN USE at tbl_printer_group (printer1,printer2,printer3). If not wrong foreign key, one to one from parent to child, but this one to many.

How to solve this problem, I'm using php, yii2 and postgresql.

Thanks and sorry for my english

  • 写回答

1条回答 默认 最新

  • douchao1957 2016-02-05 00:30
    关注

    The foreign key is added through migration with help of addForeignKey() method:

    $this->addForeignKey(
        'name_of_foreign_key_constraint',
        'products',
        'category_id',
        'products_categories',
        'id',
        'CASCADE',
        'CASCADE'
    );
    

    The last two options is for ON UPDATE and ON DELETE accordingly.

    Most DBMS support these options: RESTRICT, CASCADE, NO ACTION, SET DEFAULT, SET NULL.

    Note that you can specify arrays instead of category_id and id columns:

    If there are multiple columns, separate them with commas or use an array.

    Framework provides most used and needed methods for that, but remember that you can always execute custom SQL inside a migration with just writing:

    $this->execute('YOUR CUSTOM SQL QUERY');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部