Showing posts with label CodeIgniter. Show all posts
Showing posts with label CodeIgniter. Show all posts

Wednesday, January 29, 2014

How to update a JOINed tables using Codeigniter's Active Record?

Codeigniter active record doesn't allow to update a joined tables. 
After trying various method and searching the solution, I have found the following solution which does the exactly same thing i.e. update the multiple join tables. By using following method, you can update multiple table using codeigniter active record.
$this->db->set('a.firstname', 'Pekka');
$this->db->set('a.lastname', 'Kuronen');
$this->db->set('b.companyname', 'Suomi Oy');
$this->db->set('b.companyaddress', 'Mannerheimtie 123, Helsinki Suomi');

$this->db->where('a.id', 1);
$this->db->where('a.id = b.id');
$this->db->update('table as a, table2 as b');

Monday, August 19, 2013

CodeIgniter's Transactions

Transactions

CodeIgniter's database abstraction allows you to use transactions with databases that support transaction-safe table types. In MySQL, you'll need to be running InnoDB or BDB table types rather than the more common MyISAM. Most other database platforms support transactions natively.
If you are not familiar with transactions we recommend you find a good online resource to learn about them for your particular database. The information below assumes you have a basic understanding of transactions.

CodeIgniter's Approach to Transactions

CodeIgniter utilizes an approach to transactions that is very similar to the process used by the popular database class ADODB. We've chosen that approach because it greatly simplifies the process of running transactions. In most cases all that is required are two lines of code.
Traditionally, transactions have required a fair amount of work to implement since they demand that you to keep track of your queries and determine whether tocommit or rollback based on the success or failure of your queries. This is particularly cumbersome with nested queries. In contrast, we've implemented a smart transaction system that does all this for you automatically (you can also manage your transactions manually if you choose to, but there's really no benefit).

Running Transactions

To run your queries using transactions you will use the $this->db->trans_start() and $this->db->trans_complete() functions as follows:
$this->db->trans_start();
$this->db->query('AN SQL QUERY...');
$this->db->query('ANOTHER QUERY...');
$this->db->query('AND YET ANOTHER QUERY...');
$this->db->trans_complete();
You can run as many queries as you want between the start/complete functions and they will all be committed or rolled back based on success or failure of any given query.

Strict Mode

By default CodeIgniter runs all transactions in Strict Mode. When strict mode is enabled, if you are running multiple groups of transactions, if one group fails all groups will be rolled back. If strict mode is disabled, each group is treated independently, meaning a failure of one group will not affect any others.
Strict Mode can be disabled as follows:
$this->db->trans_strict(FALSE);

Managing Errors

If you have error reporting enabled in your config/database.php file you'll see a standard error message if the commit was unsuccessful. If debugging is turned off, you can manage your own errors like this:
$this->db->trans_start();
$this->db->query('AN SQL QUERY...');
$this->db->query('ANOTHER QUERY...');
$this->db->trans_complete();

if ($this->db->trans_status() === FALSE)
{
    // generate an error... or use the log_message() function to log your error
}

Enabling Transactions

Transactions are enabled automatically the moment you use $this->db->trans_start(). If you would like to disable transactions you can do so using $this->db->trans_off():
$this->db->trans_off()

$this->db->trans_start();
$this->db->query('AN SQL QUERY...');
$this->db->trans_complete();
When transactions are disabled, your queries will be auto-commited, just as they are when running queries without transactions.

Test Mode

You can optionally put the transaction system into "test mode", which will cause your queries to be rolled back -- even if the queries produce a valid result. To use test mode simply set the first parameter in the $this->db->trans_start() function to TRUE:
$this->db->trans_start(TRUE); // Query will be rolled back
$this->db->query('AN SQL QUERY...');
$this->db->trans_complete();

Running Transactions Manually

If you would like to run transactions manually you can do so as follows:
$this->db->trans_begin();

$this->db->query('AN SQL QUERY...');
$this->db->query('ANOTHER QUERY...');
$this->db->query('AND YET ANOTHER QUERY...');

if ($this->db->trans_status() === FALSE)
{
    $this->db->trans_rollback();
}
else
{
    $this->db->trans_commit();
}
Note: Make sure to use $this->db->trans_begin() when running manual transactions, NOT $this->db->trans_start().

Tuesday, February 26, 2013

Error Number: 1366 (Incorrect integer value:


When a custom field is created with Field Type: Text Input, Field Content: Integer, Required Field: No, and I create a new entry, while leaving this field blank, the error
A Database Error Occurred
Error Number: 1366
Incorrect integer value: ‘’ for column ‘field_id_10’ at row 1
is displayed.

solution:
Is your server running in MySQL “Strict” mode, comment that line as follows.
#sql-mode=“STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION”
After changing the my.ini file and restarting the mySQL server, the problem disappeared.
The Server Wizard should mention this as a warning/error, shouldn’t it?


Tuesday, January 8, 2013

MYSQL : "THE USER SPECIFIED AS A DEFINER ('ROOT'@'%') DOES NOT EXIST"

The source code for the stored procedures that you have been loading probably contain "DEFINER=root@'%'" as part of the definition - looking a bit like this:
create definer='root'@'%' procedure sp_test() begin end;
The problem here is that you do not have an account on your system for 'root'@'%'. This can be easily demonstrated. From the MySQL command line:
show grants for 'root'@'%';
I expect that this will come back with an error message:
ERROR 1141 (42000): There is no such grant defined for user 'root' on host '%'
The fix is to alter the source of your stored procedures, or to create the missing account:
grant all on *.* to 'root'@'%' identified by 'password' with grant option;
It is not generally a good idea to have such a high-powered account accessible from anywhere, but that is another story.

Tuesday, December 25, 2012

How to remove unnessesary blank lines in your code

In Dreamviewer
  1. Open the file
  2. Click CTRL + F
  3. Select "Current document" in "Find in" (You can also select the folder if you have multiple files)
  4. Search in "Source code"
  5. Tick "Use regular expression"
  6. Type "[\r\n]{2,}" (without quotes) in "Find"
  7. Type "\n" (without quotes) in "Replace"
  8. Press "Replace All"

Friday, September 23, 2011

Cannot find codeigniter spark path at sparks

1st issue:
sparks not installed successfully.
reinstall sparks


    Steps:
  1. Via the shell, navigate to the root of your CodeIgniter application
  2. Copy and paste the following into the command line:
php -r "$(curl -fsSL http://getsparks.org/go-sparks)"
  1. Press enter. If all goes well, move on to Get Sparks. If not, try the normal installation below.



2nd issue:
if you use public folder for index.php, CI application cann't find sparks.


Steps:
go to the site_root/application/core/MY_Loader.php

Default constructor

function __construct() { 
   if(!defined('SPARKPATH')) 
   { 
      define('SPARKPATH', 'sparks/'); 
   } 
   parent::__construct(); 
}


modify highlighted row to

define('SPARKPATH', '../sparks/');


Tuesday, August 30, 2011

CodeIgniter – No input file specified error – .htaccess – PHP5 – Apache 2

simply i used following .htaccess code


RewriteEngine on
RewriteCond $1 !^(index\.php|images|css|robots\.txt)
RewriteRule ^(.*)$ /index.php?/$1 [L]


difference of below code and codeigniter documentation code is "?" of third row. i highlighted it.