Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ssh/2.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模型中关联的条件_Cakephp_Session Variables - Fatal编程技术网

会话变量作为CakePHP模型中关联的条件

会话变量作为CakePHP模型中关联的条件,cakephp,session-variables,Cakephp,Session Variables,我找了很多,但我无能为力。 我尝试使用sessionvariable作为模型中hasMany关联的条件。 像这样(在模型中) public$hasMany=array( “myClassname”=>数组( 'className'=>'myClassname', “foreignKey”=>“Foreigned” '条件'=>数组('myClassname.id'=>), ) ); 谁能给我一个clou吗? 谢谢这里有一个“clou”: $hasMany是一个数组,因此它不能有“动态”值,该值

我找了很多,但我无能为力。 我尝试使用sessionvariable作为模型中hasMany关联的条件。 像这样(在模型中)

public$hasMany=array(
“myClassname”=>数组(
'className'=>'myClassname',
“foreignKey”=>“Foreigned”
'条件'=>数组('myClassname.id'=>),
)
);
谁能给我一个clou吗? 谢谢

这里有一个“clou”:

$hasMany
是一个数组,因此它不能有“动态”值,该值会根据某些代码而变化

如果您无法执行其他操作(例如在查找中使用适当的
条件),则需要使用回调
Model::beforeFind()
来设置当前会话ID。

这里有一个“clou”:

$hasMany
是一个数组,因此它不能有“动态”值,该值会根据某些代码而变化


如果无法执行其他操作(例如在查找中使用适当的
条件),则需要使用回调
Model::beforeFind()
来设置当前会话ID。

将此条件放在查找条件之前的控制器中:

$this->ModelName->bindModel(array('hasMany' => array('myClassname' => array('className' => 'myClassname',
                                                                            'foreignKey' => 'foreignid',
                                                                            'conditions'    => array('myClassname.id' => $this->Session->read('SessionVar'))
                                            ))), false);

您还可以在beforeFind()回调中使用此动态关联关系。

将此条件放在控制器中的find条件之前:

$this->ModelName->bindModel(array('hasMany' => array('myClassname' => array('className' => 'myClassname',
                                                                            'foreignKey' => 'foreignid',
                                                                            'conditions'    => array('myClassname.id' => $this->Session->read('SessionVar'))
                                            ))), false);
您还可以在beforeFind()回调中使用此动态关联