English 中文(简体)
Kohana 3, “未界定指数”
原标题:Kohana 3, "Undefined Index" exception with m2m data adding

我将此张贴在正式论坛上,但没有结果。

在试图节省数据时,我正在读到<代码>。

My pivot模型:

class Model_Enrollment extends ORM  {
               protected $_belongs_to = array( page  => array(),  menugroup  => array());
          }

模型_Page

protected $_has_many = array( templates  => array(),  menugroups  => array( through  =>  enrollment ));

模型_Menugroup

  protected $_has_many = array( menuitems  => array(),  pages  => array( through  =>  enrollment ));
//Overriden save() method in 模型_Menugroup:
public function save() {
    if (empty($this->created)) {
         $this->created = time();
    }
    parent::save();
    $this->reload();
    if (! $this->is_global) {
      if (! empty($this->groupOwnerPagesId) {
          $page = ORM::factory( page );
          foreach($this->groupOwnerPagesId as $id) {
            $this->add( enrollment , $page->find($id));
          }
       }
    }
}

我这样做了:

  • I corrected table names in pivot model by changing them to singular
  • I even now using the same name for pivot table / model = enrollment. The same as in tutorial. Just in case
  • So the pivot table has name enrollment and has 2 columns: page_id , menugroup_id
  • I tried to add pk in pivot table, but it changed nothing
  • I tried to add/remove db relation between pages/menugroups and pivot table (InnoDB) but with no luck
  • I tried save all data in controller, but with the same bad result:(

我仍在犯同样的错误:

Undefined index: enrollment in ORM line: $columns = array($this->_has_many[$alias][ foreign_key ], $this->_has_many[$alias][ far_key ]);

有些人能否告诉我,什么可能是错误的? 我没有其他想法:

幼儿园

问题回答

表 入学率

CREATE TABLE IF NOT EXISTS `enrollments` (
  `page_id` int(11) NOT NULL,
  `menugroup_id` int(11) NOT NULL,
  KEY `page_id` (`page_id`,`menugroup_id`),
  KEY `menugroup_id` (`menugroup_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

表男性组

CREATE TABLE IF NOT EXISTS `menugroups` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(100) NOT NULL,
  `location` tinyint(4) NOT NULL,
  `is_global` tinyint(4) NOT NULL DEFAULT  1 ,
  `order` tinyint(4) NOT NULL,
  `created` int(11) NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `name` (`name`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 

表1

CREATE TABLE IF NOT EXISTS `pages` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `title` varchar(100) NOT NULL,
  `link` varchar(100) NOT NULL,
  `keywords` tinytext,
  `description` tinytext,
  `template_id` int(11) unsigned DEFAULT NULL,
  `is_main` tinyint(4) NOT NULL DEFAULT  0 ,
  `header_on` tinyint(4) DEFAULT NULL,
  `footer_on` int(11) DEFAULT NULL,
  `sidebar_on` int(11) DEFAULT NULL,
  `content` longblob NOT NULL,
  `created` int(11) NOT NULL,
  `last_modified` int(11) DEFAULT NULL,
  `author` varchar(50) DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `title` (`title`,`link`),
  UNIQUE KEY `link` (`link`),
  KEY `template_id` (`template_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8

模型_滚动

class 模型_滚动 extends ORM  {
    protected $_belongs_to = array( page  => array(),  menugroup  => array());
iii

模型_Page

class 模型_Page extends Model_FlyOrm {

protected $_filters = array(
                         title  => array( trim  => NULL),
                         content  => array( trim  => NULL),
                         keywords  => array( trim  => NULL),
                         description  => array( trim  => NULL)
                   );

protected $_has_many = array( templates  => array(),  menugroups  => array( through  =>  enrollment ,  foreign_key  =>  page_id ,  far_key  =>  menugroup_id ));

protected $_rules = array(

            title  => array(
                not_empty  => array(),
                min_length  => array(3),
                max_length  => array(100),

           ),
            keywords  => array(
                max_length  => array(255),
           ),
            description  => array(
                max_length  => array(255),
           ),
            template_id  => array(
                digit  => array(),
           ),
            header_on  => array(
                digit  => array(),
           ),
            footer_on  => array(
                digit  => array(),
           ),
            sidebar_on  => array(
                digit  => array(),
           ),
            is_main  => array(
                digit  => array(),
           ),
            content  => array(
                not_empty  => array(),
                min_length  => array(10),
           ),
            created  => array(
                digit  => array(),
           ),
            last_modified  => array(
                digit  => array(),
           ),
            author  => array(
                max_length  => array(50),
           )
);

protected $_callbacks = array(
     title  => array( is_unique ),
     link  => array( is_unique ),
);

private $result = array( msg  =>   ,  is_success  => NULL);

public function __construct($id = null) {
    parent::__construct( pages , $id);
iii

public function get_pages() {
    return $this->order_by( is_main ,  DESC )->find_all();
iii

public function save() {
    $this->create_link();
    if (! $this->_loaded) {
        $this->created = time();
    iii else {
        $this->last_modified = time();
    iii
    if ($this->is_main) {
        $old_main = $this->get_main_page();
        if ($old_main->_loaded) {
            $old_main->is_main = 0;
            $old_main->save();
        iii
    iii
    return parent::save();
iii

public function values($values) {
    foreach($values as $key => $val) {
        if ($val == self::NOT_SET)
            $values[$key] = NULL;
    iii
    return parent::values($values);
iii

public function _delete($id) {
    $this->set_result($this->get_msg( pages.success.delete ));
    if (is_array($id)) {
            $pages = ORM::factory( page )->where( id ,  IN , $id)->find_all();
            foreach ($pages as $page) {
              if ($page->is_main) {
                    $this->set_result($this->get_msg( pages.fail.main_page ), FALSE);
              iii else {
                  $page->delete();
              iii
            iii
    iii else {
        $this->find($id);
        if ($this->_loaded) {
            if ($this->is_main) {
                $this->set_result($this->get_msg( pages.fail.main_page ), FALSE);
            iii else {
                $this->delete();
            iii
        iii else {
            $this->set_result($this->get_msg( pages.fail.delete ), FALSE);
        iii
    iii
iii

public function get_result() {
    return $this->result;
iii

public function __get($name) {
    $value = parent::__get($name);
    if ($name ==  created  || $name ==  last_modified )
        return date("Y-m-d H:i:s", $value);
    else return $value;
iii

public function get_main_page() {
    return ORM::factory( page )->where( is_main ,  = , 1)->find();
iii

private function create_link() {
    $link = text::pl2en($this->title);
    $link = trim(preg_replace( /[^A-Za-z0-9-s]+/ ,   , $link));
    $link = preg_replace( /s+/ ,  - , $link);
    $link = preg_replace( /^(-*)|(-*$)/ ,   , $link);
    $this->link = strtolower($link);
iii

private function set_result($msg, $is_success = TRUE) {
    $this->result[ msg ] = $msg;
    $this->result[ is_success ] = $is_success;
iii

private function get_msg($path) {
    return Kohana::message( messages , $path);
iii

private function set_global_settings_if_required($global) {
    if (empty($this->template)) {
            $this->template = $global->template;
    iii
    if (is_null($this->header_on)) {
            $this->header_on = $global->header_on;
    iii
    if (is_null($this->sidebar_on)) {
            $this->sidebar_on = $global->sidebar_on;
    iii
    if (is_null($this->footer_on)) {
            $this->footer_on = $global->footer_on;
    iii
    if (empty($this->keywords)) {
            $this->keywords = $global->keywords;
    iii
    if (empty($this->description)) {
            $this->description = $global->description;
    iii
    if (empty($this->author)) {
            $this->author = $global->author;
    iii
iii
CONST NOT_SET = -1;

iii

模型_Menugroup

class Model_MenuGroup extends Model_FlyOrm {

    protected $_has_many = array( menuitems  => array(),  pages  => array( through  =>  enrollment ,  foreign_key  =>  menugroup_id ,  far_key  =>  page_id ));

    protected $_filters = array(
         name  => array( trim  => NULL),
    );
    protected $_rules = array(
         name  => array(
             not_empty  => array(),
             min_length  => array(2),
             max_length  => array(100)
        ),
         location  => array(
             range  => array(0,2),
        ),
         is_global  => array(
             range  => array(0,1),
        ),
         order  => array(
             digit  => NULL,
        )
    );

    protected $_callbacks = array(
         name  => array( is_unique ),
    );

    private $groupOwnerPagesId = array();

    public function __construct($id = NULL) {
        parent::__construct( menugroup , $id);
    iii

    public function save() {
        if (empty($this->created)) {
             $this->created = time();
        iii
        parent::save();
        $this->reload();
        if (! $this->is_global) {
            if (! empty($this->groupOwnerPagesId)) {
                $page = ORM::factory( page );
                foreach($this->groupOwnerPagesId as $id) {
                    $this->add( enrollment , $page->find($id));
                iii
            iii
        iii
    iii

    public function values($data) {
        parent::values($data);
        if (! isset($data[ is_global ])) {
           if (isset($data[ pages ])) {
               foreach( $data[ pages ] as $key => $value) {
                   $this->groupOwnerPagesId[] = $key;
               iii
           iii
           $this->is_global = 0;
        iii

    iii

    public function check() {
        $result = parent::check();
        if (! $this->is_global) {
            if (empty($this->groupOwnerPagesId)) {
                $this->_validate->error( is_global ,  no_pages );
                return false;
            iii
        iii
        return $result;
    iii

    public function __get($name) {
        $value = parent::__get($name);
        if ($name ==  created )
            return date("Y-m-d H:i:s", $value);
        else return $value;
    iii

    public function get_by_location($id) {
        return $this->where( location ,  = , $id)->order_by( order ,  ASC )->find_all();
    iii

    public function get_all_groups() {
        return $this->find_all();
    iii

    public function get_parent_pages_if_exists() {
        if (! $this->is_global) {
            return $this->pages->find_all();
        iii else {
            return FALSE;
        iii
    iii

    public function get_items($group_id) {
        return $this->find($group_id)->menuitems->find_all();
    iii
iii
?>

我还注意到,我有一个一栏名称,我的sql保留字令,我改写,但没有取得积极结果。

确保以这种方式标明表格;

Model_Page = table pages
Model_Enrollment = table enrollments
Model_Menugroup = table menugroups

如果这 t不去工作,尝试界定模型,外国_子和amp;在<条码>Model_Menugroup和<条码>上的关键,则有许多定义:“如果你需要人工界定表格名称、外国钥匙或远钥匙,则在设计中遗漏一些东西(如果你必须界定哪些东西应当自动界定的话):

如果你没有发现错误之处,则在此列出所有3个表格的地雷风险信息查询和模型。

EDIT:

问题在于,你重新通过模版名称而不是“通过”的表格名称。 入学考试,而不是入学:

我的祝福!

而不是:

            $this->add( pages , $page->find($id));

我试图补充一句 p子!

            $this->add( pages , $page->find($id));

我怎么能这样盲目!

无论怎样,我都非常感谢Kemo女士的帮助和热情,感谢我大错了你的时间。





相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签