Kohana 3,“未定义的索引” m2m 数据添加例外

发布于 2024-08-31 18:48:48 字数 1567 浏览 5 评论 0原文

我在官方论坛上发布了这个,但没有结果。

尝试保存数据时,我收到未定义索引注册错误。

我的数据透视表模型:

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

Model_Page

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

Model_Menugroup

  protected $_has_many = array('menuitems' => array(), 'pages' => array('through' => 'enrollment'));
//Overriden save() method in Model_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));
          }
       }
    }
}

我做了:

  • 我通过将数据透视表模型中的表名称更改为单数来更正它们
  • ,我什至现在对数据透视表/模型=注册使用相同的名称。与教程中的相同。以防万一因此
  • 数据透视表的名称为“enrollment”并且有 2 列: page_id 、 menugroup_id
  • 我尝试在数据透视表中添加 pk ,但它没有改变任何内容
  • 我尝试添加/删除页面/菜单组和数据透视表之间的数据库关系(InnoDB )但运气不好
  • 我尝试将所有数据保存在控制器中,但结果相同:(

我仍然收到相同的错误:

未定义的索引:注册 在 ORM 行中: $columns = array($this->_has_many[$alias]['foreign_key'], $this->_has_many[$alias]['far_key']);

有人能告诉我吗,还有什么问题吗?我没有其他想法:(

亲切的问候

I posted this on official forum but with no result.

I am getting Undefined index enrollment error when trying to save data.

My pivot model:

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

Model_Page

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

Model_Menugroup

  protected $_has_many = array('menuitems' => array(), 'pages' => array('through' => 'enrollment'));
//Overriden save() method in Model_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 did:

  • 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:(

I am still getting the same error:

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

Could somebody tell me, what can be else wrong? I have no other ideas:(

Kind regards

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

番薯 2024-09-07 18:48:48

表注册

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 

表页

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

Model_Enrollment

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

Model_Page

class Model_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);
}

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

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

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

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);
              } else {
                  $page->delete();
              }
            }
    } else {
        $this->find($id);
        if ($this->_loaded) {
            if ($this->is_main) {
                $this->set_result($this->get_msg('pages.fail.main_page'), FALSE);
            } else {
                $this->delete();
            }
        } else {
            $this->set_result($this->get_msg('pages.fail.delete'), FALSE);
        }
    }
}

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

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;
}

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

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);
}

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

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

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

}

Model_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);
    }

    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));
                }
            }
        }
    }

    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;
               }
           }
           $this->is_global = 0;
        }

    }

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

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

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

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

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

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

我还注意到我有一个列名带有 mysql 保留字“order”,我将其更改为“ord”,但没有任何积极结果。

Table enrollments

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;

Table menugroups

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 

Table pages

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

Model_Enrollment

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

Model_Page

class Model_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);
}

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

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

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

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);
              } else {
                  $page->delete();
              }
            }
    } else {
        $this->find($id);
        if ($this->_loaded) {
            if ($this->is_main) {
                $this->set_result($this->get_msg('pages.fail.main_page'), FALSE);
            } else {
                $this->delete();
            }
        } else {
            $this->set_result($this->get_msg('pages.fail.delete'), FALSE);
        }
    }
}

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

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;
}

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

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);
}

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

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

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

}

Model_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);
    }

    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));
                }
            }
        }
    }

    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;
               }
           }
           $this->is_global = 0;
        }

    }

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

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

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

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

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

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

I also noticed that I have one column name with mysql reserved word 'order', I changed it to 'ord' but with no positive result.

浅语花开 2024-09-07 18:48:48

确保表以这种方式命名;

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

如果这不起作用,请尝试定义“model”、“foreign_key”和“foreign_key”。 Model_MenugroupModel_Page 中的“far_key”有很多定义,尽管规则是“如果您必须手动定义表名、外键或远键,则有些东西是在数据库设计中遗漏了(实际上,如果您必须定义任何应该自动定义的内容):)

如果您仍然不知道错误在哪里,请在此处发布所有 3 个表创建查询和模型:

对其

进行测试,问题是您正在传递模型名称而不是“通过”的表名称,请尝试使用“注册”而不是“注册”:)

Make sure that tables are named this way;

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

If that doesn't get it to work, try defining 'model', 'foreign_key' & 'far_key' in Model_Menugroup and Model_Page 'has many' definitions, although it's a rule that "if you have to define table name, foreign keys or far keys manually, something is missed in the db design (actually, if you have to define anything that's supposed to be defined automatically) :)

If you still don't figure out where's the mistake, post all 3 tables CREATE queries and models here.

EDIT:

tested it, the problem is that you're passing model name instead of table name for "through". Try with 'enrollments' instead of 'enrollment' :)

野の 2024-09-07 18:48:48

我的坏!

而不是:

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

我尝试添加枢轴!

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

我怎么可以这么盲目!

无论如何,非常感谢 Kemo 的帮助,并且很抱歉因为我的重大错误而浪费了您的时间。

My bad!

Instead of:

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

I try add pivot!

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

How can I be so blind!

Any way thank you very much Kemo for help and sorry for wasting your time by my big mistake.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文