Keeping your Yii Models Lean – Use Behaviors

Active Record models are fantastic for consolidating “black box” logic and keeping your models self-aware of their business logic, but what do you do when the business rules and object specific operations keep adding up?

Continue reading “Keeping your Yii Models Lean – Use Behaviors”

How to save multiple related models in Yii [Complete Solution]

Scenario:
You are in a situation where you have two related tables a Parent and a child table. You need to create a user experience in which user presses Save button only once, meaning by, user enters all the data in parent model fields, then enters data in all child model fields, optionally generates more child model fields and then presses the save button.
Continue reading “How to save multiple related models in Yii [Complete Solution]”

Table ‘DATEBASE_NAME.AuthAssignment’ doesn’t exist

If you have used Rights module for RBAC in your Yii application and you deploy it on Linux based server, you may encounter this error.

Problem:
If you have used Rights module for RBAC in your Yii application and you deploy it on Linux based server, you may encounter this error. Continue reading “Table ‘DATEBASE_NAME.AuthAssignment’ doesn’t exist”

How to separate front and admin panel in yii-framework

In my previous post I demonstrated you how to render different layout for different user role, now lets move a step forward and see how to create separate front and admin panel in a Yii based application.
Continue reading “How to separate front and admin panel in yii-framework”

nl2br equivalent JavaScript function

PHP provides a useful function nl2br that inserts HTML line breaks before all newlines \n in a string. Many times we want to utilize this functionality in JavaScript, although there is no nl2br  equivalent JavaScript function but we can create a custom function using regular expressions. Below is JavaScript equivalent for nl2br.

function nl2br(str, is_xhtml) {
	var breakTag = (is_xhtml || typeof is_xhtml === 'undefined') ? '<br />'	: '<br>';
	return (str + '').replace(/([^>\r\n]?)(\r\n|\n\r|\r|\n)/g, '$1' + breakTag	+ '$2');
}

mouseover with delay (using hoverIntent plugin)

hoverIntent is a plug-in that attempts to determine the user’s intent… like a crystal ball, only with mouse movement! It works like (and was derived from) jQuery’s built-in hover. However, instead of immediately calling the onMouseOver function, it waits until the user’s mouse slows down enough before making the call.
Continue reading “mouseover with delay (using hoverIntent plugin)”

Yii 1.1: Behaviors & Events

Behaviors & Events provide endless possibilities and unbelievable flexibility, but as current Yii documentation does not give more than a few examples, it might be difficult to fully understand their internals and requirements.

It should be noted that they do mostly the same thing. You can attach behaviors and event handlers to components to modify the components’ behavior.

Events

It is useful when you want to interrupt the normal application flow without extending base classes.

For example, enabling gzip compression on the output could be done via extending CWebApplication. But because there are entry points for event handlers, one can do this:

Yii::app()->onBeginRequest = create_function('$event', 'return ob_start("ob_gzhandler");');
Yii::app()->onEndRequest = create_function('$event', 'return ob_end_flush();');

You can create an event handler — which is simply a method in some class with a specific signature — and attach it to the event of an object. You can add as many event handlers as you wish, from as many objects as you wish. If the event handler is, effectively static, then you can create the object as you assign it:

$test_comp->onSomethingGoesOn = array(new SomeClass, 'eventHandler1');
$test_comp->onSomethingGoesOn = array(new SomeOtherClass, 'eventHandler2');
$test_comp->onSomethingGoesOn = array(new YetAnotherClass, 'eventHandler3');

As long as you have a handle on the object, then you can add an event handler to it.

At some point, you can then raise the event with something like one of these:

$test_comp->onSomethingGoesOn(new CEvent($this));
$test_comp->onSomethingGoesOn(new CEvent());

So, basically, it allows you to build a list of function calls that can later be executed, in the order they were added. It can save you passing around a lot of object refs and building conditional code, since you can still raise the event, even if it doesn’t do anything.

Behaviors

Behaviors are simply a way of adding methods to an object.

Take this scenario: You have 2 classes: MySuperClass1, MySuperClass2. There might be lots of methods from MySuperClass1 & 2 that you want in some new class, say MyBoringClass. Unfortunately, php does not allow for this:

class MyBoringClass extends MySuperClass1, MySuperClass2 {
}

This is where behaviors come in. Instead, you can go:

class MyBoringClass extends MySuperClass1 {
}

$classInstance = new MyBoringClass();
$classInstance->attachbehavior('uniqueName', new MySuperClass2);

Now $classInstance has all the methods from MySuperClass1 and MySuperClass2. Since MySuperClass2 is being used as a behavior, it has to extend CBehavior. The only caveat to this is an attached behavior cannot override any class methods of the component it is being attached to. If a method already exists, if it be from the original class or already added by a previously attached behavior, it will not be overwritten.

In an OO language like Ruby, it’s quite possible to start with a completely empty object and simply build its behavior as you go along. Yii provides this behavior with a little magic. The key is that the class you wish to add the behavior from must extend Cbehavior.

class SomeClass extends CBehavior
{
    public function add($x, $y) { return $x + $y; }
}

Then use with:

$test_comp = new TestComponent();
$test_comp->attachbehavior('blah', new SomeClass);
$test_comp->add(2, 5);

So, in this case, you are extending the functionality of an object with functionality of another object.

Originally posted at http://www.yiiframework.com/wiki/44/

Configure Authorize.net in Magento

Magento has various built-in payment methods, one of them is Authorize.net. To enable and configure Authorize.net as your payment processor, you should have API Login ID and Transaction Key. You can get these credentials from your Authorize.net account.
Continue reading “Configure Authorize.net in Magento”

Magento Admin: 404 Error Page not found.

mag-admin-404Let’s suppose you are logged in to Magento admin panel and you are installing a new extension that adds some new sections to Magento Configurations.
You activate your new extension and it adds some new tabs/sections but when you click on newly added tab you see a 404 error, don’t worry!

Solution :

Just logout from admin panel and login again. Cheers!!!