dongxieyou3314 2013-12-23 12:58
浏览 45
已采纳

FuelPHP ORM无法插入相关表

My database:

CREATE TABLE IF NOT EXISTS `ws_accounts` (
  `account_id` int(11) NOT NULL AUTO_INCREMENT,
  `account_username` varchar(255) DEFAULT NULL COMMENT 'username',
  `account_email` varchar(255) DEFAULT NULL COMMENT 'email',
  `account_salt` varchar(255) DEFAULT NULL COMMENT 'store salt for use when hashing password',
  `account_password` tinytext COMMENT 'password',
  `account_display_name` varchar(255) DEFAULT NULL COMMENT 'name for display on web to prevent show username.',
  `account_firstname` varchar(255) DEFAULT NULL COMMENT 'first name',
  `account_middlename` varchar(255) DEFAULT NULL COMMENT 'middle name',
  `account_lastname` varchar(255) DEFAULT NULL COMMENT 'last name',
  `account_birthdate` date DEFAULT NULL COMMENT 'birthdate store in date format (YYYY-mm-dd)',
  `account_avatar` varchar(255) DEFAULT NULL COMMENT 'avatar file. refer from root web without http or domain',
  `account_signature` text COMMENT 'signature. very useful in forum',
  `account_timezone` varchar(30) NOT NULL DEFAULT 'Asia/Bangkok' COMMENT 'see timezone list here http://www.php.net/manual/en/timezones.php',
  `account_language` varchar(10) DEFAULT NULL COMMENT 'framework language shortcode eg: en, th',
  `account_create` bigint(20) DEFAULT NULL COMMENT 'timestamp of account create date',
  `account_create_gmt` bigint(20) DEFAULT NULL COMMENT 'timestamp of account create date in gmt0',
  `account_last_login` bigint(20) DEFAULT NULL COMMENT 'timestamp of last login date',
  `account_last_login_gmt` bigint(20) DEFAULT NULL COMMENT 'timestamp of last login date in gmt0',
  `account_status` int(1) NOT NULL DEFAULT '0' COMMENT '0=disable, 1=enable',
  `account_status_text` varchar(255) DEFAULT NULL COMMENT 'status text for describe why disable.',
  `account_new_email` varchar(255) DEFAULT NULL COMMENT 'store new email waiting for confirmation',
  `account_new_password` varchar(255) DEFAULT NULL COMMENT 'store new password in reset password progress',
  `account_confirm_code` varchar(255) DEFAULT NULL COMMENT 'confirmation code. use for confirm register, change email, reset password',
  `account_confirm_code_since` bigint(20) DEFAULT NULL COMMENT 'confirm code generated since',
  PRIMARY KEY (`account_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 COMMENT='contain user account' AUTO_INCREMENT=2 ;

--
-- Dumping data for table `ws_accounts`
--

INSERT INTO `ws_accounts` (`account_id`, `account_username`, `account_email`, `account_salt`, `account_password`, `account_display_name`, `account_firstname`, `account_middlename`, `account_lastname`, `account_birthdate`, `account_avatar`, `account_signature`, `account_timezone`, `account_language`, `account_create`, `account_create_gmt`, `account_last_login`, `account_last_login_gmt`, `account_status`, `account_status_text`, `account_new_email`, `account_new_password`, `account_confirm_code`, `account_confirm_code_since`) VALUES
(0, 'Guest', 'none@localhost', NULL, NULL, 'Guest', NULL, NULL, NULL, NULL, NULL, NULL, 'Asia/Bangkok', NULL, 1387121127, 1387095927, NULL, NULL, 0, 'This account is for guest actions.', NULL, NULL, NULL, NULL),
(1, 'admin', 'admin@localhost.com', NULL, '$P$FPnwJAQzX498tYCbbIfYTbdYiOCShE0', NULL, NULL, NULL, NULL, NULL, NULL, NULL, 'Asia/Bangkok', NULL, 1387121127, 1387095927, NULL, NULL, 1, NULL, NULL, NULL, NULL, NULL);

CREATE TABLE IF NOT EXISTS `ws_account_level` (
  `level_id` int(11) NOT NULL AUTO_INCREMENT,
  `level_group_id` int(11) NOT NULL,
  `account_id` int(11) NOT NULL,
  PRIMARY KEY (`level_id`),
  KEY `level_group_id` (`level_group_id`),
  KEY `account_id` (`account_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;

CREATE TABLE IF NOT EXISTS `ws_account_level_group` (
  `level_group_id` int(11) NOT NULL AUTO_INCREMENT,
  `level_name` varchar(255) DEFAULT NULL,
  `level_description` text,
  `level_priority` int(5) NOT NULL DEFAULT '1' COMMENT 'lower is more higher priority',
  PRIMARY KEY (`level_group_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 COMMENT='contain user role or level' AUTO_INCREMENT=5 ;

--
-- Dumping data for table `ws_account_level_group`
--

INSERT INTO `ws_account_level_group` (`level_group_id`, `level_name`, `level_description`, `level_priority`) VALUES
(1, 'Super administrator', 'For site owner or super administrator.', 1),
(2, 'Administrator', NULL, 2),
(3, 'Member', 'For registered user.', 999),
(4, 'Guest', 'For non register user.', 1000);

Models

class Model_Accounts extends \Orm\Model 
{


    protected static $_table_name = 'accounts';
    protected static $_primary_key = array('account_id');

    // relations
    protected static $_has_many = array(
        'account_level' => array(
            'model_to' => 'Model_AccountLevel',
            'key_from' => 'account_id',
            'key_to' => 'account_id',
            'cascade_delete' => true,
        ),
        'account_fields' => array(
            'model_to' => 'Model_AccountFields',
            'key_from' => 'account_id',
            'key_to' => 'account_id',
            'cascade_delete' => true,
        ),
    );

}

model/accounts.php

class Model_AccountLevelGroup extends \Orm\Model 
{


    protected static $_table_name = 'account_level_group';
    protected static $_primary_key = array('level_group_id');

    // relations
    protected static $_has_many = array(
        'account_level' => array(
            'model_to' => 'Model_AccountLevel',
            'key_from' => 'level_group_id',
            'key_to' => 'level_group_id',
            'cascade_delete' => true,
        )
    );


}

model/accountlevelgroup.php

class Model_AccountLevel extends \Orm\Model 
{


    protected static $_table_name = 'account_level';
    protected static $_primary_key = array('level_id');

    // relations
    protected static $_belongs_to = array(
        'account_level_group' => array(
            'model_to' => 'Model_AccountLevelGroup',
            'key_from' => 'level_group_id',
            'key_to' => 'level_group_id',
        ),
        'accounts' => array(
            'model_to' => 'Model_Accounts',
            'key_from' => 'account_id',
            'key_to' => 'account_id',
        )
    );


}

model/accountlevel.php


Create user.

I use register method in accounts model to create user.

$data['account_username'] = 'unique_username';
$data['account_password'] = 'pass';
$data['account_email'] = 'no@email.tld';

$account = self::forge($data);
$account->account_level = new Model_AccountLevel();
$account->account_level->level_group_id = 3;
$account->save();
$account_id = $account->account_id;

and i got this error.

Assigned relationships must be an array or null, given relationship value for account_level is invalid.

How to insert related table? What wrong with my code?

  • 写回答

1条回答 默认 最新

  • douchu2823 2013-12-30 21:19
    关注

    Your account_level relation is a has_many, this means that when accessing it you need to access it like an array. The ORM expects the value of this to be an array so assigning the model directly to it is invalid.

    What you want to do is:

    $account->account_level[] = new Model_AccountLevel(['level_group_id' => 3]);
    

    (Using the array in the constructor because personally I find it's neater, but it's not required)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Python输入字符串转化为列表排序具体见图,严格按照输入
  • ¥20 XP系统在重新启动后进不去桌面,一直黑屏。
  • ¥15 opencv图像处理,需要四个处理结果图
  • ¥15 无线移动边缘计算系统中的系统模型
  • ¥15 深度学习中的画图问题
  • ¥15 java报错:使用mybatis plus查询一个只返回一条数据的sql,却报错返回了1000多条
  • ¥15 Python报错怎么解决
  • ¥15 simulink如何调用DLL文件
  • ¥15 关于用pyqt6的项目开发该怎么把前段后端和业务层分离
  • ¥30 线性代数的问题,我真的忘了线代的知识了