RedBean ORM能够创建唯一密钥吗? [英] Is RedBean ORM able to create unique keys?

查看:87
本文介绍了RedBean ORM能够创建唯一密钥吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望RedBean在生成模式时创建唯一的键/索引.以下代码确实做到了–与我对文档的理解相反–不能做到这一点:

I'd like RedBean to create unique keys/indexes when generating the schema. The following code does- opposed to how I understand the documentation- not do this:

R :: setup('sqlite:rss_loader.db3');

R::setup('sqlite:rss_loader.db3');

$bean = R::findOne(IMG);
if (!$bean->id) {
    $bean = R::dispense(IMG);
    $bean->setMeta("buildcommand.unique.0", array('url'));
    $bean->url      = 'text';
    R::store($bean);
    $bean->wipe();

    R::freeze(); //no more schema changes!
}

sqlite中正在发生什么:

What is happening in sqlite ist this:

create table img (id integer primary key autoincrement, url) 

我期望的是:

create table img (id integer primary key autoincrement, url text unique) 

无需针对RedBean编写SQL就能实现吗?

Can this be achieved without write SQL against RedBean?

推荐答案

您正在使用哪个版本的Redbean?看来他们已将buildcommand更新为最新版本.这就是手册说的:

What version of Redbean are you using? It looks like they updated the buildcommand in the latest version. This is what the manual says:

$bean->setMeta("buildcommand.unique" , array(array($property1, $property2)));

使用您拥有的物品

$bean->setMeta("buildcommand.unique" , array(array('url')));

如果这不起作用,则可能必须阅读setMeta函数下的实际代码,并查看实际发生的情况.

If that doesn't work, you may have to read the actual code under the setMeta function and see what is actually going on.

要在现有表上执行此操作,只需像这样存储"一个空bean就足够了-无需将任何数据添加到DB:

To do this on an existing table it is sufficient to "store" an empty bean like this- no data needs to be added to the DB:

$bean = R::dispense(IMG);
$bean->setMeta("buildcommand.unique", array(array(...)));
R::store($bean);

(警告词,如果这样做后冻结了,就不能保证拥有所有的列)

(Word of warning, if you freeze after doing this, you're not guaranteed to have all your columns)

这篇关于RedBean ORM能够创建唯一密钥吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆