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?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
M_A_K
  • 378
  • 3
  • 16
  • 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. – tigrang Aug 07 '12 at 06:32
  • 1
    You can try it by dynamic model associationship into your controller using `bindModel()` – Arun Jain Aug 07 '12 at 07:01
  • m beginner in cakephp can you explain ARun?? – M_A_K Aug 07 '12 at 07: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`). – jeremyharris Aug 07 '12 at 14:35

1 Answers1

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.

Predominant
  • 1,460
  • 12
  • 24
  • thanx Predominat its work can you explain be this process further i'll very thankufull to you because m beginner – M_A_K Aug 08 '12 at 05:01
  • 1
    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. – Predominant Aug 08 '12 at 05:17
  • can you tell me how can use seesion variable OR auth variables in this section $this->Session->read('User.role')?????? – M_A_K Aug 08 '12 at 05:23
  • Might be best to ask a new question if you want to ask about other stuff ;) – Predominant Aug 08 '12 at 05:28