Yii Framework Forum: resetScope still does not work. - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

resetScope still does not work. Rate Topic: -----

#1 User is offline   filthy 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 68
  • Joined: 27-January 12

Posted 06 September 2012 - 11:02 AM

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.
0

#2 User is offline   CeBe 

  • Advanced Member
  • Yii
  • Group: Yii Dev Team
  • Posts: 474
  • Joined: 16-July 10
  • Location:Berlin. Germany

Posted 06 September 2012 - 06:35 PM

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

#3 User is offline   filthy 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 68
  • Joined: 27-January 12

Posted 06 September 2012 - 07:33 PM

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:

Quote

Property "CWebUser.projectId" is not defined.


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     /**




0

#4 User is offline   CeBe 

  • Advanced Member
  • Yii
  • Group: Yii Dev Team
  • Posts: 474
  • Joined: 16-July 10
  • Location:Berlin. Germany

Posted 06 September 2012 - 07:41 PM

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

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

#5 User is offline   filthy 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 68
  • Joined: 27-January 12

Posted 06 September 2012 - 07:47 PM

mmm...

i thought resetScope will disable defaultScopes? Am i mistaken?
0

#6 User is offline   CeBe 

  • Advanced Member
  • Yii
  • Group: Yii Dev Team
  • Posts: 474
  • Joined: 16-July 10
  • Location:Berlin. Germany

Posted 07 September 2012 - 02:47 AM

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.
0

#7 User is offline   filthy 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 68
  • Joined: 27-January 12

Posted 07 September 2012 - 07:00 AM

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?
0

#8 User is offline   CeBe 

  • Advanced Member
  • Yii
  • Group: Yii Dev Team
  • Posts: 474
  • Joined: 16-July 10
  • Location:Berlin. Germany

Posted 07 September 2012 - 09:03 AM

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

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users