【问题标题】:Is RedBean ORM able to create unique keys?RedBean ORM 是否能够创建唯一键?
【发布时间】:2012-05-28 02:02:56
【问题描述】:

我希望 RedBean 在生成模式时创建唯一键/索引。以下代码确实-与我对文档的理解相反-不要这样做:

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 中发生了什么是这样的:

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

我所期待的是:

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

不针对 RedBean 编写 SQL 是否可以实现?

【问题讨论】:

标签: php sqlite redbean


【解决方案1】:

您使用的是哪个版本的 Redbean?看起来他们在最新版本中更新了buildcommand。手册是这样说的:

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

插入你所拥有的:

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

如果这不起作用,您可能需要阅读setMeta 函数下的实际代码,看看实际发生了什么。

要在现有表上执行此操作,只需像这样“存储”一个空 bean - 无需将数据添加到 DB:

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

(警告,如果您在执行此操作后冻结,则不能保证拥有所有列)

【讨论】:

  • 优秀。诀窍是将 buildcommand.unique 与双精度数组一起使用。
  • 附录:要在现有表上执行此操作,只需像这样“存储”一个空 bean - 无需将数据添加到 DB:$bean = R::dispense(IMG) ; $bean->setMeta("buildcommand.unique", array(array(...))); R::store($bean);
猜你喜欢
  • 2023-03-06
  • 2011-12-07
  • 1970-01-01
  • 1970-01-01
  • 2012-03-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多