Yii Framework Forum: Add customize functions inside model or inside controller - Yii Framework Forum

Jump to content

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

Add customize functions inside model or inside controller Rate Topic: -----

#1 User is offline   Maclein 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 65
  • Joined: 25-April 12

Posted 22 May 2012 - 02:58 AM

I want to add a customize function inside model or inside controller. Then what I have to do? Please I am stuck at this point.

The scenario is like that

I have a model imr
class Imr extends CActiveRecord{
    ....
    ....
}


I need to add my custom method related to this Imr model like this

class Imr extends CActiveRecord{
    ....
    ....
    public function imrDetails(){
        ....
        ....
    }
}



but when I am trying to call this function like this
if (isset($_POST['imr_id'])){
   $imr = new Imr;
   $imr->imr_id = $_POST['imr_id'];
			
   $imr->imrDetails();
}


Giving me this error ( Imr and its behaviors do not have a method or closure named "imrDetails" ).

Please help me to solve it.
Please help me :(
0

#2 User is offline   bennouna 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,166
  • Joined: 05-January 12
  • Location:Morocco

Posted 22 May 2012 - 03:42 AM

What's your imrDetails method doing?

Also, you may want to read this wiki: Understanding Virtual Attributes and get/set methods
0

#3 User is offline   Maclein 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 65
  • Joined: 25-April 12

Posted 22 May 2012 - 04:34 AM

View Postbennouna, on 22 May 2012 - 03:42 AM, said:

What's your imrDetails method doing?

Also, you may want to read this wiki: Understanding Virtual Attributes and get/set methods



Its returning an array object.... I have to refine my search and then the results need to be processed. Then the processed result is returned through an array..
0

#4 User is offline   bennouna 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,166
  • Joined: 05-January 12
  • Location:Morocco

Posted 22 May 2012 - 05:01 AM

So why don't you use a getter in your model:
public function getImrDetails() {
    …
    return array(1, 2, 3);
}

Then you can use:
var_dump $imr->imrDetails();

0

#5 User is offline   yugene 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 513
  • Joined: 08-August 09

Posted 22 May 2012 - 11:41 PM

View Postbennouna, on 22 May 2012 - 05:01 AM, said:

Then you can use:
var_dump $imr->imrDetails();



Please take a notice of a typo here. You should use

$imr->imrDetails;

or
$imr->getImrDetails();

to get virtual attribute value.

As for your initial question - your function definition and call looks quite normal to me. Seems like there's a typo in your code which is not visible to us.
1

#6 User is offline   bennouna 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,166
  • Joined: 05-January 12
  • Location:Morocco

Posted 23 May 2012 - 03:19 AM

Thanks yugene for spotting the typo.
0

#7 User is offline   Maclein 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 65
  • Joined: 25-April 12

Posted 27 May 2012 - 12:26 AM

View Postbennouna, on 23 May 2012 - 03:19 AM, said:

Thanks yugene for spotting the typo.


That was really help full. Thanks everyone. I found one more thing, that I want to share with everyone. Its much easier and flexible as well if we create a component file there and inside it put our code.

But one thing I would like to ask you guys, will it be okay if we add some functions to the models? Will it affect the whole traditional coding structure in Yii?

And thanks again for your replies. :)
0

#8 User is offline   yugene 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 513
  • Joined: 08-August 09

Posted 27 May 2012 - 01:13 AM

View Postdipayan, on 27 May 2012 - 12:26 AM, said:

But one thing I would like to ask you guys, will it be okay if we add some functions to the models? Will it affect the whole traditional coding structure in Yii?

Do you mean methods?Model is just a class, with its specific intention, so why not to add a method to this class if this method relevant to the model's intention?

Thus yes, you can and should add methods to any of your classes if it is a correct place for this definite method: follows MVC structure in our case, follows whole application design, improves the code.

If you mean functions - Yii is OOP framework, so using functions isn't the best choice from design point of view.
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