Can't access $this->id in model's ActiveRecord

In the init() method of a User ActiveRecord, I have tried the following code in order to initialize the creation of a subsequent user folder under the images directory, according to the user’s primary key…

(the variable $image_path is set to

Yii::app()->getBasePath().DIRECTORY_SEPARATOR.’…’.DIRECTORY_SEPARATOR.‘images’.DIRECTORY_SEPARATOR.‘user’)




// this doesn't work

if (!file_exists(Yii::app()->getModule('user')->image_path.DIRECTORY_SEPARATOR.$this->id))

    mkdir (Yii::app()->getModule('user')->image_path.DIRECTORY_SEPARATOR.$this->id, 0755, true);



The problem is that mkdir() only creates the path up to ‘images/user/’, and that’s it. There is no primary key folder created, and I’m wondering why that is the case. FYI, I am modifying the yii-user extension for this, but it doesn’t make sense to me why I cannot create a folder using $this->id. Ideally, I would like this folder to be created at every creation of a new User ActiveRecord.

Thanks

What is the context of this piece of code? Can you paste the class signature?

In AR init(), $this->id would be empty. You should try to place that code snippet in afterFind() if you want to create the folder on first account access, or afterSave() on creation, where you also can check isNewRecord).

/Tommy

Awesome, your explanation of $this->id in init() makes sense. Your solution was a possibility I was considering, but I guess I was wondering if there was a "cleaner" way, though your solution looks like the best way possible. Thank you