嵌套集删除学说
我有一些模型正在使用 Doctrine 嵌套集功能。 我想添加从树中删除元素的功能,因为这是我的应用程序所必需的。我尝试使用文档中的片段,但该代码出现了一个非常奇怪的错误。
YAML 在这里: http://pastie.org/820978
我正在我的 Menu 类中尝试使用此代码女巫扩展了生成的抽象类 BaseMenu 和 BaseMenu 扩展了 Doctrine_Record :)
无论如何我的代码:
public function getMenuItem($id)
{
return Doctrine::getTable('Menu')->find($id);
}
public function delete($id)
{
$item = $this->getMenuItem($id);
//echo get_class($item); will return Menu so object exists !?
$item->getNode()->delete();
}
我得到了一个错误:
致命错误:调用成员函数 非对象上的 getNode()
我刚刚注意到 get_class($item) 正在引发一场战争(所以这可能是这种奇怪行为的原因):
警告:get_class() 需要参数 1 是对象,布尔值给出...
但是我需要一个解决方案,欢迎所有提示...
I have some models witch are using Doctrine nestedset feature.
I want to add delete functionaly of elements from tree since that is required in my application. I was trying with snippets from documentation but I am getting a very strange error with that code.
YAML is here: http://pastie.org/820978
And I am trying with this code in my Menu class witch extends generated abstract class BaseMenu and BaseMenu extends Doctrine_Record :)
Anyway my code:
public function getMenuItem($id)
{
return Doctrine::getTable('Menu')->find($id);
}
public function delete($id)
{
$item = $this->getMenuItem($id);
//echo get_class($item); will return Menu so object exists !?
$item->getNode()->delete();
}
And I get this an error:
Fatal error: Call to a member function
getNode() on a non-object
And I just noticed that get_class($item) is trowing a warring (so that probabbly is reason for this strange behavior):
Warning: get_class() expects parameter
1 to be object, boolean given in...
However I need a solution for this and all hints are welcome...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
getNode() 返回 Doctrine_Node,而不是 Doctrine_Record。
Doctrine_Record 可以被删除,但 Doctrine_Node 不能被删除——因为它无论如何都不是持久的。
正确的逻辑很简单:
另外,不要将模型中的方法命名为“删除”!这将覆盖 Doctrine_Record 的 delete() 方法,这会让你疯狂地尝试调试它。
getNode() returns a Doctrine_Node, not a Doctrine_Record.
A Doctrine_Record can be deleted, but a Doctrine_Node cannot be deleted -- because it is not persistent anyway.
The correct logic would simply be:
Also, don't name a method in your model 'delete'!! This will override Doctrine_Record's delete() method, which will drive you crazy trying to debug it.
我个人不喜欢使用 Doctrine::getTable("table_name") 因为它不会使代码变得非常干燥。如果由于某种原因“table_name”发生变化,您将不得不在很多地方更改它。
我在 Zend Framework 应用程序中使用了 Doctrine,因此我的典型使用模式包括实例化模块中每个模型的受保护实例。
使用该模式,我会在我的控制器中执行此操作
如果您真的想保持功能相似,我会使用类似的东西
I personally don't like using Doctrine::getTable("table_name") because it doesn't make the code very dry. If for some reason "table_name" ever changes, you'll have to change it in alot of places.
I used Doctrine in Zend Framework apps, so my typical pattern of use involves instantiating a protected instance of every model in my module.
Using that pattern, I would just do this in my controller
If you really want to keep your functions similar, I would use something like this
答案就在你的问题中: $item 不是对象(我猜它的值是 false,但你可以使用 var_dump($item)),因为数据库中没有具有此类 id 的行(我也猜你的 $id 为空)
Answer is in your question: $item is not object (i guess it's value is false, but you can use var_dump($item)), because there is no row with such id in DB (also I guess your $id is null)