[英]RedBean PHP setMeta(…) for UNIQUE keys not working
我正在將redbean 5.1.0與mariadb和php一起使用 。
我試圖僅使用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.