0

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
        )
    );

OR

   public $userid = 3;
        public $name = 'Course';
        public $hasMany = array(
            'Enrollcourse' => array(
                'className'     => 'Enrollcourse',
                'foreignKey'    => 'course_id',
                'conditions'    => array('Enrollcourse.student_id' => $userid),
                'dependent'     => true
            )
        );

here is $userid is a variable which is used to check to retrieve 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

Any help?

4
  • 2
    You'll have to do that in your constructor. But I don't think you're going about the right way. Not sure what you're doing. Commented Aug 7, 2012 at 6:32
  • 1
    You can try it by dynamic model associationship into your controller using bindModel() Commented Aug 7, 2012 at 7:01
  • m beginner in cakephp can you explain ARun?? Commented Aug 7, 2012 at 7:20
  • It's not CakePHP that's the problem, @M_A_K, it's basic PHP. You cannot reference variables ($this->userid, $userid) in class variables ($hasMany). Commented Aug 7, 2012 at 14:35

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

4 Comments

thanx Predominat its work can you explain be this process further i'll very thankufull to you because m beginner
When you declared a variable on a class, such as the hasMany in your initial example, you were attempting to use another variable as part of its declaration. In PHP, you cannot include a variable in the declaration of another variable, as the scope is indeterminate at the point in which the variable is initialised. To overcome this, we can setup the __constructor method, which is called every time you create an instance of the class. This has the same effect as declaring the variable on the class, but avoids the limitations :) I hope that explains it better.
can you tell me how can use seesion variable OR auth variables in this section $this->Session->read('User.role')??????
Might be best to ask a new question if you want to ask about other stuff ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.