繁体   English   中英

用于唯一键的RedBean PHP setMeta(…)不起作用

[英]RedBean PHP setMeta(…) for UNIQUE keys not working

我正在将 5.1.0与

我试图仅使用RedBeanPHP将一个bean属性(或数据库中的列)设置为UNIQUE。

根据RedBeanPHP官方文档以及许多Google和StackOverflow文章所述,解决方案是使用功能setMeta(...)但不幸的是它无法正常工作。

这是我为测试问题而编写的代码。

<?php

include 'rb.php';
R::setup('mysql:host=localhost;dbname=test', 'root', '');

R::nuke();

// =====================================

$bean = R::dispense('bean');
$bean->name = 'any';

$bean -> setMeta('buildcommand.unique', array(array('name')) );
R::store($bean);  // No problem here. As expected :-)

// =====================================

$another_bean = R::dispense('bean');
$another_bean->name = 'any';

R::store($another_bean); // it works !!, but it shouldn't. Exception expected :-(

?>

根据另一个谷歌搜索结果,我也尝试过setMeta(...)的其他选项,尽管我认为它们已被弃用。 无论如何,它们也不起作用。

$bean -> setMeta('buildcommand.unique.0', array('name') );

...也...

$bean -> setMeta('sys.uniques', array('name') );

...还有许多类似的东西。 还尝试使用其他架构(不是“测试”)。 我认为这可能与缺乏执行“ ALTER TABLE”的特权有关,但并没有

还激活了...

R::debug(true)

...但是我完全看不到RedBeanPHP试图做一个“ ALTER TABLE”。

谢谢你的帮助

抱歉,但是自版本4.2.0起,不再支持构建命令,如此处所述https://redbeanphp.com/index.php?p=/changelog

如果您的要求不允许在数据库中将属性设置为唯一,则可以引入模型并自己检查属性的唯一性。

class Model_Book extends RedBean_SimpleModel
{
    public function update()
    {
        if (!$this->bean->getId()) {
            if (R::findOne("book", "name = ?", array($this->bean->name))) {
                throw new Exception('Book with name ' . $this->bean->name . ' already exists');
            }
        }
    }
 }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM