resetScope still does not work.


(Stephane Belanger174) #1

Fresh upgrade to Yii 1.1.12 this morning.

ResetScope appears to not work in my case. Require Yii expertise please.

Deliverable model:


public function defaultScope()

{  

  return array(

    'condition'=>'project_id='.Yii::app()->user->projectId

    .' and tenant_id='.Yii::app()->user->tenantId

    );

}

Component:


.....

public function getDueDelivrables()

{

  return Deliverable::model()->resetScope()->findAll();

}

Returns result with conditions still in the sql statement. I get an error indicating that a value is required in projectId. Is this still a bug in 1.1.12? Or am i applying defaultScope/resetScope erroneously? Any help is greatly appreciated.


#2

Can you plase post the complete error message ideally with stack trace?


(Stephane Belanger174) #3

Thanks. I hope it is actually something wrong on my end.

Here is more information:

Database table:





CREATE TABLE `deliverable` (

  `id` int(11) NOT NULL AUTO_INCREMENT,

  `tenant_id` int(11) NOT NULL,

  `name` varchar(45) NOT NULL,

  `original_end_date` date NOT NULL,

  `current_end_date` date DEFAULT NULL,

  `actual_end_date` date DEFAULT NULL,

  `percent_complete` int(11) DEFAULT '0',

  `date_created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,

  `user_created` varchar(10) NOT NULL,

  `date_updated` timestamp NULL DEFAULT NULL,

  `user_updated` varchar(10) DEFAULT NULL,

  `project_id` int(11) NOT NULL,     

  `person_id` int(11) NOT NULL, 

  PRIMARY KEY (`id`),

  KEY `fk_deliverable_project1` (`project_id`),

  KEY `fk_deliverable_person1` (`person_id`),

  CONSTRAINT `fk_deliverable_person1` FOREIGN KEY (`person_id`) REFERENCES `person` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION,

  CONSTRAINT `fk_deliverable_project1` FOREIGN KEY (`project_id`) REFERENCES `project` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION

) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=latin1$$




Model:




..

	public function relations()

	{

		// NOTE: you may need to adjust the relation name and the related

		// class name for the relations automatically generated below.

		return array(

			'comments' => array(self::HAS_MANY, 'Comment', 'deliverable_id'),

			'commentCount' => array(self::STAT, 'Comment', 'deliverable_id'),		

			'person' => array(self::BELONGS_TO, 'Person', 'person_id'),

			'project' => array(self::BELONGS_TO, 'Project', 'project_id'),

		);

	}

.....

	public function defaultScope()

	{

		return array(

			'condition'=>'project_id='.Yii::App()->user->projectId 

			.' and tenant_id='.Yii::App()->user->tenantId

		);

	}




Code to retrieve data:


<?php


class getDueDeliverables extends CPortlet

{

	public $title='Due Deliverables';

 	    

    public function getDueDeliverables()

    {

        return Deliverable::model()->resetScope()->findAll();

    }

 

    protected function renderContent()

    {

        $this->render('getDueDeliverableView');

    }

}



Error message:

Stack (1): line 141 highlighted




C:\xampp\htdocs\yiiMain\framework\web\auth\CWebUser.php(141)


129 

130     /**

131      * PHP magic method.

132      * This method is overriden so that persistent states can be accessed like properties.

133      * @param string $name property name

134      * @return mixed property value

135      */

136     public function __get($name)

137     {

138         if($this->hasState($name))

139             return $this->getState($name);

140         else

141             return parent::__get($name);

142     }

143 

144     /**

145      * PHP magic method.

146      * This method is overriden so that persistent states can be set like properties.

147      * @param string $name property name

148      * @param mixed $value property value

149      */

150     public function __set($name,$value)

151     {

152         if($this->hasState($name))

153             $this->setState($name,$value);



line 155 highlighted




C:\xampp\htdocs\mypmapp\protected\models\Deliverable.php(155): CWebUser->__get("projectId")

150     }

151     

152     public function defaultScope()

153     {

154         return array(

155             'condition'=>'project_id='.Yii::App()->user->projectId 

156             .' and tenant_id='.Yii::App()->user->tenantId

157         );

158     }

159     

160     /**








#4

Well, that error message just tells you.

you can not use Yii::app()->user->projectId in default scope method because it is not existing. The reason why it is not existing is a problem of your code :wink:

defaultScope will be applied before you do resetScope() and therefore defaultScope() gets called and fails.


(Stephane Belanger174) #5

mmm…

i thought resetScope will disable defaultScopes? Am i mistaken?


#6

sure, but as defaultScope() is enabled when model is created it is called even when you reset it afterwards.

Will be clear when you not use chaining methods:


$model = Deliverable::model();

Will load defaulScope().


$model->resetScope();

Will then reset it then.


(Stephane Belanger174) #7

Thanks cebe for the reply. I have come a long way using yii but they are still things i don’t understand well i guess. I thought i could use resetScope to "disable " a defaultscope thus prevneting it from being loaded. Not after the model is loaded. So then what can i do to disable.a.defaultscope before loading a model?


#8

This is not possible, you need to adjust your defaulScope() method to only apply condition when projectId is availble.