RedbeanMySql插入错误


Redbean MySql insert error

我有以下代码:

Array
(
    [title] => asdas
    [question] => dasdsa
    [gender] => 
    [age] => 
    [description] => 
    [imageUrl] => 
    [plan_id] => 2
    [plan_type] => special
    [plan_price] => 199
    [specialty_id] => 7
    [specialty] => Cardiologist
)

我收到专业错误:

Error

RedBeanPHP'RedException Object
(
    [message:protected] => Cannot cast to bean.
    [string:Exception:private] => 
    [code:protected] => 0
    [file:protected] => /Applications/XAMPP/xamppfiles/htdocs/site/rb/rb.php
    [line:protected] => 2203
    [trace:Exception:private] => Array
        (
            [0] => Array
                (
                    [file] => /Applications/XAMPP/xamppfiles/htdocs/site/rb/rb.php
                    [line] => 2414
                    [function] => __set
                    [class] => RedBeanPHP'OODBBean
                    [type] => ->
                    [args] => Array
                        (
                            [0] => specialty
                            [1] => asdsad
                        )
                )
            [1] => Array
                (
                    [file] => /Applications/XAMPP/xamppfiles/htdocs/site/class/crud.php
                    [line] => 52
                    [function] => offsetSet
                    [class] => RedBeanPHP'OODBBean
                    [type] => ->
                    [args] => Array
                        (
                            [0] => specialty
                            [1] => asdsad
                        )
                )
            [2] => Array
                (
                    [file] => /Applications/XAMPP/xamppfiles/htdocs/site/validate/question/step2.php
                    [line] => 37
                    [function] => insert
                    [class] => crud
                    [type] => ->
                    [args] => Array
                        (
                            [0] => question
                            [1] => Array
                                (
                                    [title] => asdas
                                    [question] => dasdsa
                                    [gender] => 
                                    [age] => 
                                    [description] => 
                                    [imageUrl] => 
                                    [plan_id] => 2
                                    [plan_type] => special
                                    [plan_price] => 199
                                    [specialty_id] => 7
                                    [specialty] => asdsad
                                    [contributor] => dinesh
                                    [created_by] => 1
                                    [ip_address] => ::1
                                    [user_agent] => Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10; rv:33.0) Gecko/20100101 Firefox/33.0
                                )
                        )
                )
        )
    [previous:Exception:private] => 
)

我花了1个多小时试图解决这个问题,但仍然无法解决。

您似乎正在尝试保存一个数组而不是一个bean。每次调用R::store()都必须传递一个bean。

Redbean为您创建数据库结构,您只需要切换到流体模式R::freeze(false);

例如:

R::freeze(false);
$plan = R::dispense('plan');
$plan->description = 'Plan 1';
$bean->planprice = 199.99;
$plantype = R::dispense('plantype');//without underline, ok?!!!
$plantype->description = 'Standard';
$specialty = R::dispense('specialty');
$specialty->description = 'Cardiologist';
$bean = R::dispense('tablename');
$bean->title = 'asdas';
$bean->question = 'dasdsa';
$bean->gender = 1;//1 - Male; 2 - Female;
$bean->age = 39; 
$bean->description = 'bla bla bla';
$bean->imageurl = 'http://urltotheimage/img.jpg';
$bean->plan = $plan;
$bean->plantype = $plantype;
$bean->specialty = $specialty;
R::store($bean);

当您存储$bean时,redbean存储您创建的每一个其他bean($plan, $plantype, $specialty),attr名称和列名称末尾的_id

如果您有一个给定的id,您不需要将$plan保存为bean(如果您已经在数据库中存储了注册表),您只需要通过

$bean->plan_id = $givenid;

Redbean是一个了不起的orm工具,但你必须深入它的网站才能使用它;