【问题标题】:Redbean MySql insert errorRedbean MySql 插入错误
【发布时间】:2016-03-22 17:18:50
【问题描述】:

我有以下代码:

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 个多小时试图解决这个问题,但我仍然无法解决它。

【问题讨论】:

  • 假设你的代码是 PHP,你需要用括号括起来字符串,不是吗?还向我们展示2203rb.php 行吗?
  • 这是我的参数数组 ( [title] => asdas [question] => dasdsa [gender] => [age] => [description] => [imageUrl] => [plan_id] = > 2 [plan_type] => special [plan_price] => 199 [specialty_id] => 7 [specialty] => 心脏病专家)
  • 如果您删除了专业参数,它可以正常工作。
  • 你试过用括号括起来你的字符串并添加逗号吗?你得到的错误我不清楚。 Array ( [title] => "asdas", [question] => "dasdsa", [gender] => "", [age] => "", [description] => "", [imageUrl] => "", [plan_id] => 2, [plan_type] => "special", [plan_price] => 199, [specialty_id] => 7, [specialty] => "Cardiologist" );
  • 我试过了。专业问题。

标签: php redbean


【解决方案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 工具,但您必须深入了解它的网站才能使用它;

【讨论】:

  • 我想使用specialty_text?
  • 同样的概念正在使用其他表格,它在专业领域正确插入而没有错误。
  • 你不能在表名中使用_,但在列名上使用它没问题,为了帮助你,在rb.php中搜索方法runQuery并记录$sql和@ 987654331@ 变量,比你可以检查什么 redbean 在数据库上执行
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-01
  • 2011-08-12
  • 2013-06-17
  • 2016-07-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多