Tag Archives: php

Yii framework custom methods inside datatable row

I have been using the  edatatables extension from the yiiframework for a while now. I have noticed that you can do a ton of things with it. These things take very little effort. The extension is very powerful and was well done. Recently I ran into a situation that wasn’t apparent to me right away on how to implement a column with customized HTML. I wasn’t able to use the default column_name value since I needed to format it. The first thing I did was create a method inside the given model that would return the HTML. In the Yii framework models cannot call partials so I had to have the HTML within that models method. To me this looked out of place and felt dirty. Being a pragmatic programmer I decided to look into a better solution. I stumbled upon this solution while researching this. This solution did not seem to be common so I think this post could be beneficial to others.

The solution was to call an internal method within that controller and pass in the given data row. Through the internal function I am then able to render a partial and pass that HTML back to the actual row. When I called render partial in Yii I add the true parameter at the end so it doesn’t render it real time. Instead keeping it in a variable to return. This allowed me to remove the method from the model and move that HTML logic into the view layer. The code below show my implementation that involved adding the custom array to the columns definition and create an internal method to be called.


$columns = array( ‘column1’, ‘column2’, ‘column3’, array( ‘header’ =>         ‘Column 4’, ‘class’ => ‘EDataColumn’, ‘sortable’ => ‘false’, ‘value’ =>         array($this, ‘_latestPrices’), ‘type’ => ‘html’, ),
 );

protected function _partialViewBeingCalled($data, $row) {

$result = $this->renderPartial(’../shared/_myPartial’,array( ‘Data’ => $data, ), true);

return $result;

}

As you can see the solution is very simple. The hardest part was figuring out how the datatable would call the internal method while passing the data. After those updates I now have a clean solution that I can feel satisfied with.

PHP Yii and Ruby on Rails Active Record

I have been using the Yii Framework for over two years now and have been using the Ruby on Rails framework for around the same time period. I have done various projects in each and thought I would share my experience. The scope of this post will just be the differences between active records. It will not go into other details about the two frameworks. The purpose of this post is also not to debate which one is better. The purpose is to simply give the differences between the two and show the capabilities of each. This will only cover some features of each it will not include all of the features.

Ruby on Rails

Ruby on Rails has a powerful active record implementation. For example if you want to find something by it’s primary key. You would write something like Model.find(:id). This would then return an active record object with the properties of that single item. You can also add multiple conditions by simply by calling the where method. It would look similar to this Model.where(id: 10, text: ‘whatever’). This will then return a set of active record objects instead of just the single object the find would return. If you know there will only be one result you can simply do .first to get the result back.

Another part of active record is scopes. You can create scopes within a model and call them directly instead of having to write the where conditions over again. So if you need to write a query multiple times it’s a sign that you should create a scope instead. A scope is just defined in the model and can use lambda to pass in dynamic parameters.

Associations is a big part of Rails or any framework for that matter. In Rails associations are very simply and if it’s a simple relationship is dead simple. For example a post can have many comments. At the top of your post model you would add in a has_many :comments. As long as the keys match up that’s all you have to do. You can also do more advanced relationship mappings to.

PHP Yii

Yii has a powerful active record implementation also but uses a different implementation method. For example if you want to find a record by it’s primary key you can simply do ModelName::model()->findByPk(‘key’);. That will return a single active record object back. If you need to do more advanced queries you can do it in two ways. The first is to call the findByAttributes method. That would looks something like ModelName::model()->findByAttributes(array(‘a’ => ‘test’, ‘b’ => ‘whatever’));. That implementation will also just return a single active record object back if found. For advanced queries you can also create a CDbCriteria object with the criteria generated. When you look at the documentation you will see the various attributes you can set. After setting the object you can then simply call your query like this ModelName::model()->find(CDbCritieriaObject);.

Yii also has support for scopes. Similar to Rails Yii scopes are also defined in that specific model. A scope is Yii is basically a short cut to creating the CDbCriteria object. You will be passing in very similar parameters. It’s just as powerful as CDbCriteria also so you can do advanced queries without much of an issue.

Associations are also a big part of the Yii framework. They take a little more code than the Rails framework though. For example to do a has_many relationship you have to define it like this.


public function relations() {

return array('relationshipName' => array(

self::HAS_MANY, 'RelationshipModelName', 'RelationshipModelNamePk'

),

),

);

 

As you can see it takes a little bit more code to define them. These can be auto generated though through the Gii tool. So they are not as big of a deal to code up as 99% of the work is done if not 100%.

Those are some of the difference between the two of them. There are a lot more differences and this only covers the surface. The goal of this post is really to just share that each can do very similar things just in different ways. If you end goal is to get something up and running I feel that both frameworks can handle that. It comes down to a preference between Ruby and PHP.

Yii must have extensions

While developing a site using the Yii framework there are a couple of extensions that are must haves. These extensions will make your life easier and you won’t have to write your own code.

  • Edatatables – this extension allows you to create datatables using http://datatables.net/ jquery framework. It’s a wrapper around that framework and allows you to get datatables up and running fast. It also is very extensible allowing you to modify the source as needed.
  • minScript – min script is an extension that allows you to minify all of your assets through a one line change in the config.
  • recaptcha – allows you to put a captcha to prevent bots from spamming your site. This works great and is only a couple of lines to setup also.
  • restfulyii – this extensions allows you to easily create an API with authentication. You can name your routes whatever you want and have specific methods that get called.
  • yii-environment – this extensions will allow you to have environments setup for each phase of the development cycle. For example you have a development, test, staging and production environment. This allows you to set config params for each like database drivers or other environmental variables.

By using these extensions you will save yourself time and probably end up with more updates coming through than if you had built them yourself. It’s important when using any framework weather it be Rails or Yii that you use the extensions available even if they don’t do 100% what you want them to do. You can always modify the source in Yii and in Rails you can always open the class up and change whatever you need to change.