PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, March 11, 2022

[FIXED] How to manually execute SQL query in CakePHP without a model

 March 11, 2022     cakephp, cakephp-1.3     No comments   

Issue

I'm writing a quick throwaway method to import users from an old table to my new cake app. I've imported the old users table (old_users) into my cake app db. Basically I need to just do a select all form the old_users table, then loop through them and add them to the new users table using somehting like $newuser->create('old_username', 'old_password');

However, I didnt want to create a model etc for the temp table as this import will only be run once. So my question is - how can I do a basic select to get all the users from this table from a cake method within the users controller. I was thinking somehting like this:

public function admin_importOldUsers() {
        $db = $this->getDataSource();
        $db->fetchAll('SELECT * FROM old_users');
    }

But it failswith the error:

Call to undefined method UsersController::getDataSource()

I cant find much in the docs on how to query another db table (without a model) from within a controller....

Could anyone point me in the right direction?

Thanks in advance


Solution

To manually run a standard SQL query in CakePHP without a model, you can use the query method on any model that is available to the controller (it doesn't matter which model you use):

class MyController extends AppController
{
    public function index()
    {
        $result = $this->AnyModel->query("SELECT * FROM my_table");
    }
}

CakePHP 1.3 - Models



Answered By - BadHorsie
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing