且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在Yii中的活动记录的模型文件中添加新属性

更新时间:2023-12-01 12:50:04

您应该与父节点有关系.

You should have a relation to your parent node.

因此,在您的Node :: relations函数中,您应该具有类似以下内容:

So in your Node::relations function, you should have something like:

'parent' => array(self::BELONGS_TO, 'Node', 'parent_id'),

要从孩子那里获得此值,可以执行以下操作(假设您的孩子节点变量名为$ childNode):

To get this value from your child, you can do (assuming your child node variable is named $childNode):

echo $childNode->parent->name;

如果您真的想做...

If you really want to be able to do...

$childNode->parent_name;

...由于某种原因,您可以像这样在Node模型中创建一个属性:

... for some reason, you can create a property in your Node model like this:

public function getParent_name()
{
    if ($this->parent == null)
        return '';

    return $this->parent->name;
}

现在您可以致电...

Now you can call...

$childNode->parent_name;

OR

$childName->getParent_name();

获取父节点的名称.