Showing posts with label sql. Show all posts
Showing posts with label sql. Show all posts

Aug 19, 2015

CakePHP 2.* how to use custom complex sql Query with model

Let's suppose that we have MySQL database with table categories:

CREATE TABLE `categories` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `category` varchar(64) DEFAULT NULL,
  `description` varchar(64) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB;


Then, in CakePHP framework, you want to call a simple query as you did it with mysql_query in PHP with no framework.

For that you have the class Model::query()

First, you build the model. You don't want to use predefined Models for a table (like the model Categories for DB table Categories). Because you want to use perhaps this query for extracting data from two tables which are related.

So,  first you have to create the model. For this you have to create the file APP/Model/Categs.php



class Categ extends AppModel {

    public $useTable = false; // it is mandatory to have this line. Whithout this line, 
                              //                                    the Controller will search for table Categ; it doesn't matter that you have a sql query  

    public function getDataFromMyTable(){
        $d = $this->query("SELECT * FROM  categories");
        return $d;
    }

}


Second, you have to create the Controller. The file will be: APP/Controller/CategsController.php


class CostsController extends AppController {
    
    
    public function index(){
        $this->loadModel('Categ');
    
        $rezQuery = $this->Categ->getDataFromMyTable();
        $this->set('cs', $rezQuery);
    }
}
?>


And now we have to create the view. We create folder APP/View/Categs (if this folder doesn't exists). And then we create the file index.ctp into this folder. The name of the ctp file should be the  same as name of the function from Controller. In index.ctp we write:


Oct 31, 2012

New forum

Starting today, the coders has a new forum for their comunnication. Here we are:
coderstalk.co.cc

It is structurated by software domains: PHP, ASP, .NET, C#, Databases and HTML&CSS.
We are 4 people which will respond for every question.

The website is based on the SMF Simple Machines Forum.

Sep 18, 2012

Wordpress to regular website

Many clients ask me to build their websites with wordpress. So, now i found an interesting article about that:

"I get that for most of us entrepreneurs, especially when we're just getting started, we're bootstrapping and funds are tight.  That's what you need to jump in and use WordPress to build your website.  So, here's how to make a "regular" (non-blog) website using WordPress.  To see a larger version of any image below, just click on it.
Over the next few days I'll be posting about WordPress and how to get the most out of it.  But back to setting up your site…
First, you'll need to install WordPress on your hosting space.  This is not as scary as it sounds – really, it's not!  Most website hosts will even install it for free for you if you open a support ticket and ask.  Here's a video showing how to install WordPress using Fantastico (will open in a new window so you don't lose this post)."

Read more...

Mysql Restore

This handy command line snippets allow us to import and export databases or tables with mysql built in function. Mysqldump command can be configured to export databases, or just a table.


//Export Database
mysqldump -uUsername -p Database > db_backup.sql
 
//Export Multiple databases
mysqldump -uUsername -p --databases db_name1 [db_name2 ...] > db_backup.sql
 
//Export All databases
mysqldump -uUsername -p --all-databases > db_backup.sql
 
//Export a table
mysqldump -uUsername -p Database Tablename > table_backup.sql
 
//Export multiple tables
mysqldump -uUsername -p Database Table1 [Table2 ...]  > tables_backup.sql



Restore database or tables from mysqldump file
mysql -u username --p database_name < dump.sql