使用RedBeanPHP在MySQL中插入对象(类引用)

使用RedBeanPHP在MySQL中插入对象(类引用),php,mysql,orm,redbean,Php,Mysql,Orm,Redbean,上下文 我想在MySQL数据库中插入一些对象,因为我不想使用PDO的纯代码进行所有的数据库管理,所以我决定尝试使用RedBean PHP ORM 下面是我的两个对象: class Profile { public $Name; public $Description; public $Disabled; public $ListOfProfileTranslation; } class ProfileTranslation { public $LanguageCode;

上下文

我想在MySQL数据库中插入一些对象,因为我不想使用PDO的纯代码进行所有的数据库管理,所以我决定尝试使用RedBean PHP ORM

下面是我的两个对象:

class Profile {
  public $Name;
  public $Description;
  public $Disabled;
  public $ListOfProfileTranslation;
}

class ProfileTranslation {
  public $LanguageCode;
  public $Title;
  public $Description;
}
上面的两个对象在某种程度上是“链接”的,因为Profile的ListOfProfileTranslation包含一个“ProfileTranslation”数组

执行和红豆PHP

我知道RedBean PHP可以帮助简化DB上的CRUD操作;我也看到过类似于上的例子,它们独立地声明表和每一列,但我认为如果我向RedBean PHP传递一个对象(因为表名、列名和值,所以我猜测RedBean PHP可以以某种方式自行处理,但我可能错了)

这样我就可以写这样的东西:

    $Profile = R::dispense('Profile');
    $Profile = $itemObject; // where $itemObject is a "Profile" object 
//which already exists in memory
    R::store($Profile);
$Profile = R::dispense('Profile');

$Profile->Name = $itemObject->Name;
$Profile->Description = $itemObject->Description;
$Profile->Disabled = $itemObject->Disabled;

R::store($Profile);
我知道上述操作会引发异常,不会执行,但在数据库管理简化方面有什么方法可以做到这一点吗

我是否必须完成所有步骤,如:

    $Profile = R::dispense('Profile');
    $Profile = $itemObject; // where $itemObject is a "Profile" object 
//which already exists in memory
    R::store($Profile);
$Profile = R::dispense('Profile');

$Profile->Name = $itemObject->Name;
$Profile->Description = $itemObject->Description;
$Profile->Disabled = $itemObject->Disabled;

R::store($Profile);

根据您的需要,实现这两个对象并在DB中使用RedBean PHP链接它们的最佳解决方案是什么?

如果有$profile和$translation,您可以这样链接它们:

$profile->ownTranslation[] = $translation;
R::store($profile);
现在,RedBeanPHP将把翻译连接到配置文件。 至于课程,你不需要这些。假设您有这个ProfileTranslation类,您将如何设置属性?使用setter

$profTrans->setLanguageCode($lang);
那么,为什么不直接设置它们呢?我们都知道,设置者不会做很多有用的事情

$profTrans->language = $lang;
如果需要某种验证,您可以将其添加到类中,但无需重新声明类中的属性、写入访问器等。这两者由RedBeanPHP自动“融合”:

 class Model_Translation extends RedBean_SimpleModel {

     public function update() {
        ...validate here, just throw exception if anything is wrong...
     }

 }
而且。。。你完了。不需要属性声明,不需要访问器、获取器、设置器。。。刚刚做完

这就是RedbeanHP的威力

干杯,
Gabor

给定$profile和$translation,您可以这样链接它们:

$profile->ownTranslation[] = $translation;
R::store($profile);
现在,RedBeanPHP将把翻译连接到配置文件。 至于课程,你不需要这些。假设您有这个ProfileTranslation类,您将如何设置属性?使用setter

$profTrans->setLanguageCode($lang);
那么,为什么不直接设置它们呢?我们都知道,设置者不会做很多有用的事情

$profTrans->language = $lang;
如果需要某种验证,您可以将其添加到类中,但无需重新声明类中的属性、写入访问器等。这两者由RedBeanPHP自动“融合”:

 class Model_Translation extends RedBean_SimpleModel {

     public function update() {
        ...validate here, just throw exception if anything is wrong...
     }

 }
而且。。。你完了。不需要属性声明,不需要访问器、获取器、设置器。。。刚刚做完

这就是RedbeanHP的威力

干杯, 加博