Kohana 3,“未定义的索引” m2m 数据添加例外
我在官方论坛上发布了这个,但没有结果。
尝试保存数据时,我收到未定义索引
注册错误。
我的数据透视表模型:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
表注册
表菜单组
表页
Model_Enrollment
Model_Page
class Model_Page extends Model_FlyOrm {
}
Model_Menugroup
我还注意到我有一个列名带有 mysql 保留字“order”,我将其更改为“ord”,但没有任何积极结果。
Table enrollments
Table menugroups
Table pages
Model_Enrollment
Model_Page
class Model_Page extends Model_FlyOrm {
}
Model_Menugroup
I also noticed that I have one column name with mysql reserved word 'order', I changed it to 'ord' but with no positive result.
确保表以这种方式命名;
如果这不起作用,请尝试定义“model”、“foreign_key”和“foreign_key”。
Model_Menugroup
和Model_Page
中的“far_key”有很多定义,尽管规则是“如果您必须手动定义表名、外键或远键,则有些东西是在数据库设计中遗漏了(实际上,如果您必须定义任何应该自动定义的内容):)如果您仍然不知道错误在哪里,请在此处发布所有 3 个表创建查询和模型:
对其
进行测试,问题是您正在传递模型名称而不是“通过”的表名称,请尝试使用“注册”而不是“注册”:)
Make sure that tables are named this way;
If that doesn't get it to work, try defining 'model', 'foreign_key' & 'far_key' in
Model_Menugroup
andModel_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' :)
我的坏!
而不是:
我尝试添加枢轴!
我怎么可以这么盲目!
无论如何,非常感谢 Kemo 的帮助,并且很抱歉因为我的重大错误而浪费了您的时间。
My bad!
Instead of:
I try add pivot!
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.