Yii Framework Forum: calling an action of another controller - Yii Framework Forum

Jump to content

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

calling an action of another controller Rate Topic: -----

#1 User is offline   rohit 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 68
  • Joined: 08-July 09
  • Location:India

Posted 10 March 2010 - 04:13 AM

I have property controller which has create action,
also i have propertyImage controller which has create action!

now how do I call create action of propertyImage controller in property controller create action?
I have code similar to this..
this code is in property/CreateAction.php
Yii::import('application.controllers.PropertyImageController');
Yii::import('application.controllers.propertyImage.CreateAction');
$propertyImageController=new PropertyImageController('propertyImage');
$createImage=new CreateAction($propertyImageController,'create');
$createImage->run();


But this says "problem loading page"..

Regards..
0

#2 User is offline   Onman 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 175
  • Joined: 26-December 09
  • Location:The Netherlands

Posted 10 March 2010 - 05:27 AM

An action is not supposed to be an API which can be called from another action.
So if your property/create needs to do some more work after calling propertyimage/create, then you should consider some other approach.
If you just want the property/create and the propertyimage/create actions to perform exactly the some code, you may use the forward() function.
1

#3 User is offline   rohit 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 68
  • Joined: 08-July 09
  • Location:India

Posted 11 March 2010 - 11:09 PM

Thanks a lot Onman ...

forward() might be helpful. I didn't notice it at first!
But, my point is, when i'm creating a property, i want to create a new image related to it! Now, I don't want to totally give control to that controller! as render should be called from property controller and not from image controller.

Ok, situation is , when i'm creating a property, i'm checking if user email is existing, if not i'm adding to user table(i.e. new user created). Now, if i add user creation into property table, then its logically wrong isn't it? user creation should be handled by user controller itself. as there might be other events where i would like to handle user creation or updates etc. So to avoid duplicate code, i wanted to keep it in separate classes...

What is behavior? or event? Can that be used in any way, in such situations?
0

#4 User is offline   Onman 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 175
  • Joined: 26-December 09
  • Location:The Netherlands

Posted 12 March 2010 - 03:12 AM

You may want to create 1 (UserModel) or 2 (UserModel and PropertyImageModel) models.

On the model you can then create your api's (methods) which handle the task, e.g.
UserModel->createUser($email);
PropertyModel->createImage($imgName);
1

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