Php变量在Cakephp模型 [英] Php Variable in Cakephp Model
问题描述
我试图使用join与两个表通过使用有很多关系的CakePHP与条件我的模型代码在这里使用
i am trying to use join with two table by using has many relation of CakePHP with condition my model code are here which am using
public $userid = 3;
public $name = 'Course';
public $hasMany = array(
'Enrollcourse' => array(
'className' => 'Enrollcourse',
'foreignKey' => 'course_id',
'conditions' => array('Enrollcourse.student_id' => $this->userid),
'dependent' => true
)
);
或
public $userid = 3;
public $name = 'Course';
public $hasMany = array(
'Enrollcourse' => array(
'className' => 'Enrollcourse',
'foreignKey' => 'course_id',
'conditions' => array('Enrollcourse.student_id' => $userid),
'dependent' => true
)
);
这里是$ userid是变量,用于检查选定用户的数据,但无法得到这个&发生以下情况
here is $userid is Variable which are use as checking to reterive data of selected user but am unable to get this & following are occur
Error: parse error
File: E:\wamp\www\simpleApp\app\Model\course.php
Line: 10
任何帮助
推荐答案
您不能在类中声明变量时使用变量。这意味着使用 $ userid
将导致您看到的解析错误。
You cannot use variables in the declaration of a variable within a class. This means that use of $userid
will cause the parse error you are seeing.
克服这个问题的最好方法动态信息是替换/重载模型的构造函数:
The best way to overcome this for dynamic information is to replace/overload the constructor for the model:
public function __construct($id = false, $table = null, $ds = null) {
$this->hasMany = array(
'Enrollcourse' => array(
'className' => 'Enrollcourse',
'foreignKey' => 'course_id',
'conditions' => array('Enrollcourse.student_id' => $this->userid),
'dependent' => true
)
);
parent::__construct($id, $table, $ds);
}
这就克服了类变量声明中变量的使用。
This overcomes the use of a variable during a class variable declaration.
这篇关于Php变量在Cakephp模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!