【问题标题】:afterFind not working with recursive level 2afterFid 不使用递归级别 2
【发布时间】:2015-04-11 05:43:12
【问题描述】:

afterFind() 的问题。

当前代码:

 <?php
    public function afterFind($results, $primary = false)
    {
            foreach ($results as $key => $val)
            {
                if (isset($val['User']['country_code']) && isset($val['User']['mobile']))
                {
                    $results[$key]['User']['mobile'] = trim($val['User']['country_code']).trim($val['User']['mobile']);
                }
            }
            return $results;
    }
?>

它适用于$this-&gt;User-&gt;find(),但不适用于其他模型。 我有 3 个模型。 Room、Place 和 User。

<?php
    $this->Place->bindModel(array('belongsTo' => array('User')));
    $this->Room->bindModel(array('belongsTo' => array('Place')));
?>

当我尝试查找Room 数据时:

<?php
$data = $this->Room->find('first');
array(
    [Room] => array()
    [Place] => array(
        [User] => array(
            [mobile] => /* here after find not working it should content country code + mobile */
        )
    )
)
?>

【问题讨论】:

  • 不要使用任何递归。在 AppModel 中将其设置为 -1,然后在需要其他关联数据时使用 Containable Behavior。

标签: cakephp cakephp-2.0 cakephp-model


【解决方案1】:

绑定flyby时$reset可能有问题。 http://api.cakephp.org/2.1/source-class-Model.html#868-910

请将 $reset 设置为 false。

$this->Model->bindModel(array(...),false);

【讨论】:

    猜你喜欢
    • 2019-08-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-21
    • 2021-12-26
    • 1970-01-01
    相关资源
    最近更新 更多