0

I have a Yii application with two separate models, Domain and Page. Domain has a unique field, so I set up a rule ('domain', 'unique') and also create an action that creates a new record if the field is new, otherwise uses the existing record, and returns the ID:

$domain_model=new Domain();
$domain_model->domain=$domain;
if(!$domain_model->save()){
    $atts = array('domain'=>$domain);
    $domain_model=Domain::model()->findByAttributes($atts);
}
return $domain_model->id;

This works fine, but within a Page controller I want to call this same function during a page insertion action. I can't figure out the best DRY method to do this with Yii. I can't access other model controllers, so I don't know where to put this function to make it accessible both within and outside of the Domain MVC.

tereško
  • 58,060
  • 25
  • 98
  • 150
Rhys
  • 1,581
  • 2
  • 14
  • 22
  • is _Page_ and _Domain_ related? or do you have the same field in _Page_ ? – bool.dev Jul 16 '12 at 12:20
  • @bool.dev: Page records have a domain ID (domain can have many pages) – Rhys Jul 16 '12 at 12:54
  • so during page insertion a domain can be specified(or selected)? – bool.dev Jul 16 '12 at 12:56
  • That's correct. Domains can be created from their own controller action, but this same process should be available during the page creation controller action (seeing a page CANNOT exist without a domain) – Rhys Jul 16 '12 at 13:07

2 Answers2

1

Define a behaviour that does this stuff, and attach to any class you want to use it.

Use of Component Behavior

A component supports the mixin pattern and can be attached with one or several behaviors. A behavior is an object whose methods can be 'inherited' by its attached component through the means of collecting functionality instead of specialization (i.e., normal class inheritance). A component can be attached with several behaviors and thus achieve 'multiple inheritance'.

Behavior classes must implement the IBehavior interface. Most behaviors can extend from the CBehavior base class. If a behavior needs to be attached to a model, it may also extend from CModelBehavior or CActiveRecordBehavior which implements additional features specifc for models.

To use a behavior, it must be attached to a component first by calling the behavior's attach() method. Then we can call a behavior method via the component:

// $name uniquely identifies the behavior in the component
$component->attachBehavior($name,$behavior);
// test() is a method of $behavior
$component->test();

An attached behavior can be accessed like a normal property of the component. For example, if a behavior named tree is attached to a component, we can obtain the reference to this behavior object using:

$behavior=$component->tree;
// equivalent to the following:
// $behavior=$component->asa('tree');

A behavior can be temporarily disabled so that its methods are not available via the component. For example,

$component->disableBehavior($name);
// the following statement will throw an exception
$component->test();
$component->enableBehavior($name);
// it works now
$component->test();

It is possible that two behaviors attached to the same component have methods of the same name. In this case, the method of the first attached behavior will take precedence.

When used together with events, behaviors are even more powerful. A behavior, when being attached to a component, can attach some of its methods to some events of the component. By doing so, the behavior gets a chance to observe or change the normal execution flow of the component.

A behavior's properties can also be accessed via the component it is attached to. The properties include both the public member variables and the properties defined via getters and/or setters of the behavior. For example, if a behavior has a property named xyz and the behavior is attached to a component $a. Then we can use the expression $a->xyz to access the behavior's property.

More reading:
http://www.yiiframework.com/wiki/44/behaviors-events
http://www.ramirezcobos.com/2010/11/19/how-to-create-a-yii-behavior/

Pentium10
  • 204,586
  • 122
  • 423
  • 502
  • Thank you for your help. I have looked into this, and although it does appear to be what I am after, I cannot make a working test using two separate controllers. Is this possible? The error is: `Object configuration must be an array containing a "class" element.` – Rhys Jul 21 '12 at 10:57
  • That error means that there is no `class` node in you configuration and can't identify which component to attach. – Pentium10 Jul 21 '12 at 11:02
0

I don't remember Yii exactly, but I believe there's a "beforeSave" callback that you can use for your Domain model. That way no matter what, when you save a domain model, it will always call the "Check if this id already exists before saving the model."

Then afterwards you'll simply do $domain->save();, followed by $domain->id, whether or not it previously existed.

Anther
  • 1,834
  • 12
  • 13
  • Thanks. I have looked into the beforeSave/Validate functions, they are quite useful but still don't help me, as the chunk of code I included would still have to be entered in two places through the application, wouldn't they? (within the Domain MVC and the Page MVC) – Rhys Jul 16 '12 at 12:56
  • You're right. So my next 2 suggestions are... create a static method that does what your previous function does within `Domain`, or have both of the controllers extends from the same class and simply call the method the exact same way from both the Domain controller and the Page controller. – Anther Jul 16 '12 at 13:34
  • 1
    You are asking around and suggesting static methods, but check out my answer with the behaviours class. That's the only and DRY way to have these functionality outside of model and usable in any controller. – Pentium10 Jul 16 '12 at 13:47