2009-11-06 51 views
2

如果我有一个人的模型first_namelast_name,我该如何创建并显示full_name?我想在我的编辑和视图视图(即“编辑弗兰克卢克”)和其他地方的顶部显示它。简单地将回波降至first_namelast_name不是干燥的。需要在蛋糕全名PHP

对不起,如果这是一个非常简单的问题,但没有任何工作。

谢谢 弗兰克卢克

编辑为清楚:好吧,我对人模型的功能。

function full_name() { 
    return $this->Person->first_name . ' ' . $this->Person->last_name; 
} 

在视图中,我称之为

echo $person['Person']['full_name'] 

这给了我,我有一个未定义的指标的通知。从视图中调用函数的正确方法是什么?我必须在控制器或其他地方执行吗?

回答

2

如果你想要的只是显示一个完整的名称,而不需要做任何数据库操作(比较,查找),我认为你应该只在视图中连接你的字段。

这将更符合MVC设计模式。在你的例子中,你只是想以不同的方式查看数据库中的信息。

由于连接操作很简单,因此可能不会通过将代码放置在单独的函数中来节省大量代码。我认为它最容易在视图文件中完成。

如果你想做更多花哨的事情(即改变大小,返回一个链接给用户)我建议创建一个元素,你用用户数据调用。

+0

这涵盖了需求。我继续这个元素。 – 2009-11-09 17:33:01

0

您可能需要从数据库中返回两个字段,并将它们连接成一个字符串变量,然后才能显示。

+0

这就是我一直在尝试的。我可以让这个功能很好。它叫它让我晾干。我应该更清楚。 – 2009-11-06 22:51:55

2

save()方法设置的数组仅返回数据库中的字段,它们不调用模型函数。要正确使用上面的函数(位于模型),你将需要添加以下内容:

控制器,在$操作方法:

$this->set('fullname', $this->Person->full_name(); 
// must have $this-Person->id set, or redefine the method to include $person_id 
视图

echo $fullname; 

基本上,您需要使用控制器从模型中收集数据,并将其分配给控制器。这与您之前的过程相同,您可以将查询中返回的数据从find()调用分配给变量,除非您从其他来源获取数据。

2

有多种方法可以做到这一点。一种方法是在模型类中使用afterFind函数。 参见:http://book.cakephp.org/view/681/afterFind

但是,这个函数不能很好地处理嵌套数据,相反,它并没有处理它! 因此,我使用一个afterFind功能在通过所述结果集行走app_model

function afterFind($results, $primary=false){ 
    $name = isset($this->alias) ? $this->alias : $this->name; 
    // see if the model wants to attach attributes 
    if (method_exists($this, '_attachAttributes')){ 
     // check if array is not multidimensional by checking the key 'id' 
     if (isset($results['id'])) { 
      $results = $this->_attachAttributes($results); 
     } else { 
      // we want each row to have an array of required attributes 
      for ($i = 0; $i < sizeof($results); $i++) { 
       // check if this is a model, or if it is an array of models 
       if (isset($results[$i][$name])){ 
        // this is the model we want, see if it's a single or array 
        if (isset($results[$i][$name][0])){ 
         // run on every model 
         for ($j = 0; $j < sizeof($results[$i][$name]); $j++) { 
          $results[$i][$name][$j] = $this->_attachAttributes($results[$i][$name][$j]); 
         } 
        } else { 
         $results[$i][$name] = $this->_attachAttributes($results[$i][$name]); 
        } 
       } else { 
        if (isset($results[$i]['id'])) { 
         $results[$i] = $this->_attachAttributes($results[$i]); 
        } 
       } 
      } 
     } 
    } 
    return $results; 
} 

然后我添加_attachAttributes函数模型中的类,例如用于在你的Person.php中

function _attachAttributes($data) { 
    if (isset($data['first_name']) && isset($data['last_name'])) { 
     $data['full_name'] = sprintf("%s %s %s", $data['first_name'], $data['last_name']); 
    } 
    return $data; 
} 

该方法可以处理嵌套modelData,例如,人有许多帖子,那么这个方法也可以在Post模型中附加属性。

该方法也记住,由于使用别名而不仅仅是名称(这是className),所以具有除className之外的其他名称的链接模型是固定的。

0

http://old.nabble.com/Problems-with-CONCAT-function-td22640199.html http://teknoid.wordpress.com/2008/09/29/dealing-with-calculated-fields-in-cakephps-find/

阅读的第一个,找出如何使用“字段”键即找到(“所有”,阵列(“字段” =>数组())到CONCAT传递给CakePHP的查询生成器。

第二个链接显示您如何合并,当您使用自定义字段回适当位置,在返回的结果是得到返回的数字指标。

这当然应该放在模型功能并从那里调用。