How to store array values in Yii model property

In some scenarios, we require to store multiple values in your class attribute of your Yii model. We usually use this for showing check boxes or multiple select in your Yii form.

Add new Rule for your field “arrayField” in your model:

Overwrite your model’s “savemethod and change the array value as serialized string to store this in your database table.

How can I avoid direct access to codeigniter controllers, rather than routes?

When we are defining all your URLs through the routes.php file, the direct access to your code-igniter class will make accessible duplicated pages for the website. Duplicated pages affect SEO in large extents, so better to avoid direct access to the controller functions.

Step 1: Create a controller function for 404 response page

Create a separate controller and a normal public function in the controller folder. The following code inside the function makes the 404 error response page.

Step 2: Modify your routes.php file

At this point, you already have a good idea about the config/routes.php file, otherwise please have a look at: Documentation for Codeigniter Routes. The routes.php will be similar to the below code after modification:

There are mainly two points in your routes files:

  1. Overwrite your default 404 error page with defined controller page:  $route['404_override'] = 'errors/show_404';
  2. After defining all your routes, avoid further URL access by pointing all other URLs to the 404 controller page written before:  $route['(:any)'] = 'errors/show_404';

So, pointing all requests to the 404 page after parsing all valid routes will help you to avoid direct access to the controller with controller name and its method names.

How you can change the server URL for GIT repository

Assume that your remote origin (repository server URL) is: git@repositoryserver.com/repository-name.git  and you want to replace it with git@newrepositoryserver.com/repository-name.git .

Command to check your current origins:

You will be able to see the origins for both fetch and push operations:

Command to change the GIT repository origin:

That’s it. You now successfully relocated your repository pointing to the new server.

How to restore the deleted local file from GIT repository

It is very simple to get the locally deleted file in a git repository. Make sure that you are not committed any changes.

Suppose you deleted the file: myfolder/my_deleted_file.txt  accidentally and you want to retrieve the file from the server.

Execute the following command:

That’s it. Your file is now restored to the folder.

How to copy file from an SSH server to the local computer

On Linux systems, it is very easy to copy the files from one computer to another with the secure copy protocol.

Suppose you are logged into the server with SSH and you want to copy the file: /home/user/dummy.txt from the current server folder to your local path: /var/dummy.txt

 

Do the following steps:

  1. Exit from the ssh connection.
  2. Make sure that you are logged into your local machine.
  3. Execute the following command with the remote computer’s username and host name:
  4. You will be prompted for the password to connect with the server. Type the password to execute the secure copy operation.
  5. Your file from the remote server is now copied successfully to the local system. Make sure that the file is copied at the location /var/dummy.txt.

Create a random alphanumeric string with mysql query

Why we need this?

We used to generate random strings for generating passwords or generating keys which are used for accessing sensitive information through a URL. When you want to generate a unique random key for a table, it is always better to generate the key in mysql rather than the server side scripting language. Generating random key in mysql procedure, avoid multiple key generation and checks the existence of generating a key, which reduces multiple mysql connection calls.

MySQL Custom Function to generate alphanumeric random string

Use the below code to create a custom function which generates a random key:

Note: The maximum length of random string that can be generated by the above function is 100. You can increase the limit by changing the function return type.

Using custom function in a procedure:

Implement the created custom function generate_random  :

 

How to add new action link column for Yii CGridView

It is very simple to create custom columns in CGridView. While defining the columns, you have to define an array with specific values for the custom column. To create a column which points to another controller with ID parameter, use following code:

To add header for the column, modify the same array with an additional parameter for the header.

The entire widget for CGridView in view file will look like this:

 

CodeIgniter Form Validation – How to get all error fields as an array

CodeIgniter by default is not providing you the error fields for a form as an array. To add this feature to your codeigniter form_valiation library, you have to extend the system library and overload as form_validation library.

Create the file: application/libraries/My_Form_validation.php file and add the following code:

In your controller use the following code to get your error fields as an array:

 

Tips to change your MySQL Procedure through command shell

Drop your procedure before modifications

There is no alter command like table alter in mysql for modifying your MySQL procedure code. So you have to drop your procedure and then create the same procedure for altering your procedure. To drop the procedure, execute the following line of code in your command shell:

Note: Mysql ALTER PROCEDURE can be used to change the characteristics of a stored procedure only, not the parameters or body of the procedure.

Change the delimiter

Normally body of the procedure consists of semicolons (;) for the end of the line (statement). To execute the procedure as a single statement, you have to change the delimiter for the console (command line interface). You can change the delimiter to ‘/’ by executing the code below:

Execute the CREATE PROCEDURE code

Now you can type/copy your procedure code. An example procedure code is given below for your reference:

Note the last line of the procedure code. Your procedure will get executed only when you add the changed delimiter  / . If there are no issues with your code, your procedure will add successfully to the server.

Change to delimiter back to normal

You already added the procedure to the database, but make sure that you changed your delimiter back to the normal. Execute the following code for changing the delimiter to the default semicolon.

 

How to make Yii form fields mandatory manually

When you generate the model and CRUD files, usually all rules in the model is generated automatically and you are not bothered about the rules. But if you want to manually make fields in a form mandatory, you have to write a simple rule in the model rules function – array. Suppose I am having username, email and password in my form and I want all fields to be mandatory. The rules function will be similar to the below code:

Note the last array in the rule, where the first element has all fields (separated by comma) in the form which are required. The second element in the array is the validation class, which is “required” in our case.