Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/247.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
CakePHP控制器函数中的返回值_Php_Cakephp - Fatal编程技术网

CakePHP控制器函数中的返回值

CakePHP控制器函数中的返回值,php,cakephp,Php,Cakephp,我试图在控制器中创建一个递归函数,用于测试是否已经创建了模型。我的模型本身有一个hasMany/belongsTo关系(因此每个模型都有一个父id)。看起来像这样: Site.php: public $hasMany = array( 'Children' => array( 'className' => 'Site', 'foreignKey' => 'parent_id', 'dependent' => true

我试图在控制器中创建一个递归函数,用于测试是否已经创建了模型。我的模型本身有一个hasMany/belongsTo关系(因此每个模型都有一个父id)。看起来像这样:

Site.php:

public $hasMany = array(
    'Children' => array(
        'className' => 'Site',
        'foreignKey' => 'parent_id',
        'dependent' => true
    )
);

public $belongsTo = array(
    'Parent' => array(
        'className' => 'Site',
        'foreignKey' => 'parent_id'
    )
);
public function checkForExistingLink($site_id, $href) {
    $conditions = array(
        'Site.parent_id' => $site_id,
        'Site.url' => $href
    );

    if($this->Site->hasAny($conditions)) {
        debug('duplicate found');
        debug($href);
        debug($site_id);
        return $site_id;
    } else {
        $parent_site = $this->Site->findById($site_id);
        if($parent_site['Site']['parent_id'] == null) {
            debug('no duplicate found');
            debug($href);
            return 0;
        } else {
            debug('recall function');
            $this->checkForExistingLink($parent_site['Site']['parent_id'], $href);
        }
    }
}
现在,每次用户访问一个新站点时,我的控制器都会为找到的每个新链接(链接是
标记中的任何href)创建新的模型。这很好,但我不希望它在我的数据库中为已经添加到此站点的链接创建重复的行,所以我要做的是对照父id检查该行是否已经存在

为此,我需要不断检查每个模型的父\u id,直到其中一个模型没有父\u id(它为null,这是根域)。我在我的站点控制器内使用以下功能:

SitesController.php:

public $hasMany = array(
    'Children' => array(
        'className' => 'Site',
        'foreignKey' => 'parent_id',
        'dependent' => true
    )
);

public $belongsTo = array(
    'Parent' => array(
        'className' => 'Site',
        'foreignKey' => 'parent_id'
    )
);
public function checkForExistingLink($site_id, $href) {
    $conditions = array(
        'Site.parent_id' => $site_id,
        'Site.url' => $href
    );

    if($this->Site->hasAny($conditions)) {
        debug('duplicate found');
        debug($href);
        debug($site_id);
        return $site_id;
    } else {
        $parent_site = $this->Site->findById($site_id);
        if($parent_site['Site']['parent_id'] == null) {
            debug('no duplicate found');
            debug($href);
            return 0;
        } else {
            debug('recall function');
            $this->checkForExistingLink($parent_site['Site']['parent_id'], $href);
        }
    }
}
然后在我的view方法中调用此函数并将结果存储到变量:

$check_id = $this->checkForExistingLink($id, $href);
它似乎可以通过结果进行解析(所有调试都被正确命中),但当我调试
$check\id
时,它返回
null
。我似乎不明白为什么


如果有什么不清楚的地方,请询问,我将尝试更详细地解释这个问题。

这有几个问题:1。控制器中的函数应该对应于一个动作——站点上的一个页面。因此,在控制器本身中编写递归函数对我来说似乎不是一个好主意。将其放在组件中。2.您通常不能也不应该从视图访问控制器层中的函数。或者把它放在一个助手中,或者因为它看起来需要模型,所以在控制器中调用它,然后使用$this->set('results',$results);将结果发送到视图。最后,我想指出的是,在结束大括号之前的函数最后一行中,您没有返回
$this->checkForExistingLink…
的结果,这可能就是您最终使用null的原因。