2010-05-06 32 views
1

我在官方論壇發佈了這個,但沒有結果。Kohana 3,「未定義索引」例外與m2m數據添加

我在嘗試保存數據時遇到Undefined index註冊錯誤。

我的透視模型:

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

我所做的:

  • 我通過將它們更改爲單數來修正表格名稱
  • 我甚至現在對於數據透視表/模型=登記使用了相同的名稱。和教程中一樣。萬一
  • 所以樞軸表有名稱「招生」,並有2列:PAGE_ID,menugroup_id
  • 我試圖在數據透視表添加PK,但它沒有
  • 改變了我嘗試添加/刪除DB關係頁/ menugroups和數據透視表(InnoDB的)
  • 之間,但沒有運氣我試着保存控制器的所有數據,但具有相同的不良後果:(

我仍然得到同樣的錯誤:

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

有人能告訴我,可能是還有什麼問題?我沒有別的想法:(

親切的問候

回答

0

確保表這樣命名;

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

如果沒有得到它的工作,嘗試定義「模式」,「 foreign_key'&'far_key'在Model_MenugroupModel_Page'有很多'的定義,雖然它是一個規則,「如果你必須手動定義表名,外鍵或遠端鍵,在db設計中錯過了某些東西(實際上,如果你有定義任何應該自動定義的東西):)

如果您仍然不知道錯誤在哪裏,請在此處發佈所有3個表CREATE查詢和模型。

編輯:

測試它,問題是,你的「穿越」傳遞模型名稱,而不是表名。與 '入學' 而不是 '招生' :)

+0

謝謝你的回答。我嘗試了你的建議,但仍然有相同的錯誤結果。我會在下面粘貼我的表格和模型。 – Marek 2010-05-07 07:51:15

0

表入學試

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; 

表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 

表頁面

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

類Model_Page延伸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保留字 '訂單' 的一個名,我把它改爲'ord',但沒有任何積極的結果。

+0

如果你有額外的信息,你應該把它放在你的問題,而不是張貼(因爲這篇文章沒有回答你的問題)。 – 2010-05-07 08:17:01

+0

編輯我的答案 – Kemo 2010-05-07 10:19:36

0

我的壞!

相反的:

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

我嘗試添加支點!

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

我該怎麼這麼盲目!

無論如何,非常感謝您Kemo的幫助和抱歉,因爲我的大錯誤而浪費您的時間。

+0

(?)你能改正你的答案嗎?你粘貼了兩個相同的代碼,並且如果它解決了你的問題,你也沒有將答案標記爲正確的答案。 – 2011-06-01 18:30:35