How to cake bake in cakephp2.9 where if i bake the model i get the following error - cakephp-2.0

Error: Database connection "Mysql" is missing, or could not be created.
0 /opt/lampp/htdocs/projects/cakephp/lib/Cake/Model/ConnectionManager.php(105): DboSource->__construct(Array)
1 /opt/lampp/htdocs/projects/cakephp/lib/Cake/Console/Command/Task/ModelTask.php(952): ConnectionManager::getDataSource('default')
2 /opt/lampp/htdocs/projects/cakephp/lib/Cake/Console/Command/Task/ModelTask.php(889): ModelTask->getAllTables(NULL)
3 /opt/lampp/htdocs/projects/cakephp/lib/Cake/Console/Command/Task/ModelTask.php(979): ModelTask->listAll(NULL)
4 /opt/lampp/htdocs/projects/cakephp/lib/Cake/Console/Command/Task/ModelTask.php(203): ModelTask->getName()
5 /opt/lampp/htdocs/projects/cakephp/lib/Cake/Console/Command/Task/ModelTask.php(91): ModelTask->_interactive()
6 /opt/lampp/htdocs/projects/cakephp/lib/Cake/Console/Command/BakeShell.php(109): ModelTask->execute()
7 /opt/lampp/htdocs/projects/cakephp/lib/Cake/Console/Shell.php(461): BakeShell->main()
8 /opt/lampp/htdocs/projects/cakephp/lib/Cake/Console/ShellDispatcher.php(212): Shell->runCommand(NULL, Array)
9 /opt/lampp/htdocs/projects/cakephp/lib/Cake/Console/ShellDispatcher.php(66): ShellDispatcher->dispatch()
10 /opt/lampp/htdocs/projects/cakephp/app/Console/cake.php(47): ShellDispatcher::run(Array)
11 {main}
My database looks like:
<?php
class DATABASE_CONFIG {
public $default = array( 'datasource' => 'Database/Mysql', 'persistent' => false, 'host' => '127.0.0.1', 'login' => 'root', 'password' => '', 'database' => 'cake', );
}
?>

Related

Not able to fetch the data from other database in Drupal 7

I am trying to get the data from another database in Drupal 7. But I am not able to get the data. Here is the code what I have tried.
I have added another database information in settings.php
$databases['default']['default'] = array(
'driver' => 'mysql',
'database' => 'drupal_testing',
'username' => 'root',
'password' => '',
'host' => 'localhost',
'prefix' => '',
);
$databases['sakshi']['default'] = array(
'driver' => 'mysql',
'database' => 'test',
'username' => 'root',
'password' => '',
'host' => 'localhost',
'prefix' => '',
);
And added the code in page.tpl.php for testing the connection.
<div class="col-lg-3 col-md-3 col-sm-12 col-xs-12 padtop30">
<h4> DB Connection test </h4>
<?php
try{
echo "inside try block";
db_set_active('sakshi');
$results = db_query("select name from test.user_names where phone = 432323");
$records = $results->fetchAll();
foreach ($records as $record) {
echo $record;
}
}catch(\PDOException $ex){
echo "inside catch block";
echo $ex;
}finally{
echo "finally block is executed";
db_set_active('default');
}
?>
</div>
Now when i am loading the data (refreshing the page) i am getting the error like
'PDOException' with message 'SQLSTATE[42S02]: Base table or view not found: 1146 Table 'test.semaphore' doesn't exist' in E:\xampp5.6.20\htdocs\drupal\drupal-7.53\includes\database\database.inc:2227 Stack trace: #0 E:\xampp5.6.20\htdocs\drupal\drupal-7.53\includes\database\database.inc(2227): PDOStatement->execute(Array) #1 E:\xampp5.6.20\htdocs\drupal\drupal-7.53\includes\database\database.inc(697): DatabaseStatementBase->execute(Array, Array) #2 E:\xampp5.6.20\htdocs\drupal\drupal-7.53\includes\database\database.inc(2406): DatabaseConnection->query('SELECT expire, ...', Array, Array) #3 E:\xampp5.6.20\htdocs\drupal\drupal-7.53\includes\lock.inc(167): db_query('SELECT expire, ...', Array) #4 E:\xampp5.6.20\htdocs\drupal\drupal-7.53\includes\lock.inc(146): lock_may_be_available('rules_get_cache...') #5 E:\xampp5.6.20\htdocs\drupal\drupal-7.53\sites\all\modules\rules\rules.module(368): lock_acquire('rules_get_cache...', 60) #6 E:\xampp5.6.20\htdocs\drupal\drupal-7.53\sites\all\modules\rules\rules.module(1026): rules_get_cache('event_watchdog') #7 E:\xampp5.6.20\htdocs\drupal\drupal-7.53\sites\all\modules\rules\modules\events.inc(180): rules_invoke_event('watchdog', Array) #8 [internal function]: rules_watchdog(Array) #9 E:\xampp5.6.20\htdocs\drupal\drupal-7.53\includes\module.inc(926): call_user_func_array('rules_watchdog', Array) #10 E:\xampp5.6.20\htdocs\drupal\drupal-7.53\includes\bootstrap.inc(1997): module_invoke('rules', 'watchdog', Array) #11 E:\xampp5.6.20\htdocs\drupal\drupal-7.53\includes\errors.inc(210): watchdog('php', '%type: !message...', Array, 3) #12 E:\xampp5.6.20\htdocs\drupal\drupal-7.53\includes\errors.inc(75): _drupal_log_error(Array, true) #13 E:\xampp5.6.20\htdocs\drupal\drupal-7.53\includes\bootstrap.inc(2576): _drupal_error_handler_real(4096, 'Object of class...', 'E:\\xampp5.6.20\\...', 321, Array) #14 E:\xampp5.6.20\htdocs\drupal\drupal-7.53\sites\all\themes\my_theme\page.tpl.php(321): _drupal_error_handler(4096, 'Object of class...', 'E:\\xampp5.6.20\\...', 321, Array) #15 E:\xampp5.6.20\htdocs\drupal\drupal-7.53\includes\theme.inc(1526): include('E:\\xampp5.6.20\\...') #16 E:\xampp5.6.20\htdocs\drupal\drupal-7.53\includes\theme.inc(1208): theme_render_template('sites/all/theme...', Array) #17 E:\xampp5.6.20\htdocs\drupal\drupal-7.53\includes\common.inc(6045): theme('page', Array) #18 E:\xampp5.6.20\htdocs\drupal\drupal-7.53\includes\common.inc(5907): drupal_render(Array) #19 E:\xampp5.6.20\htdocs\drupal\drupal-7.53\includes\common.inc(2748): drupal_render_page('
I am new to Drupal. Can any one help me in this regard.
While according to the documentation, there is nothing wrong with the way you have set up your second connection, I have never been able to get it to work like that. The only way I have had it working is with the below, although I would agree there doesn't seem to be anything fundamentally different:
$databases = array (
'default' =>
array (
'default' =>
array (
'database' => 'drupal_db',
'username' => 'root',
'password' => 'password',
'host' => 'localhost',
'port' => '',
'driver' => 'mysql',
'prefix' => '',
),
),
'second_database' =>
array (
'default' =>
array (
'driver' => 'mysql',
'database' => 'second_db',
'username' => 'root',
'password' => 'password',
'host' => 'localhost',
'port' => '',
'prefix' => '',
),
),
);
Then you should be able to do db_set_active('second_database').
The "Base table or view not found: 1146 Table 'test.semaphore' doesn't exist'" message is usually when Drupal has tried to do something Drupally while still attached to your second database, so don't forget to do db_set_active() to return the connection back to the Drupal database. There is no need to add 'default' as default is the assumed default database.
You should do this directly after fetching the data you need. So in your example, it should come after $records = $results->fetchAll();
Or, if you only need this connection in one place/ infrequently, you could set it up on the fly. More info here
Hope this helps

croogo error plugin search

I download a search plugin from http://www.cakedc.com/downloads. and I used cakephp/croogo 1.4.3.
I renamed the search plugin 'Search' and I download in app/Plugin/.
in my controller:
public $name = 'MovementsRouts';
public $components = array('Search.Prg');
public $presetVars = array(
array('field' => 'name', 'type' => 'value'),
array('field' => 'status', 'type' => 'value'),
);
public function admin_index_route() {
$this->set('title_for_layout', __('Movement Taxi route'));
$this->MovementsRout->recursive = 0;
$this->Prg->commonProcess();
$this->paginate = array(
'MovementsRout' => array(
'conditions' => $this->MovementsRout->parseCriteria($this->passedArgs),
//'conditions' => array('MovementsRout.type_mvt ='=>'route_taxi'),
//'fields'=>array('id','description','title','support_count','oppose_count','user_id','created'),
'limit' => 5,
//'paramType' => 'querystring'
));
$this->set('movementsRouts', $this->paginate());
}
in my model:public
$name = 'MovementsRouts';
public $components = array('Search.Prg');
public $presetVars = array(
array('field' => 'name', 'type' => 'value'),
array('field' => 'status', 'type' => 'value'),
);
in my view index_route.ctp:
<div><?php
echo $this->Form->create('MovementsRout', array(
'url' => array_merge(array('action' => 'index'), $this->params['pass'])
));
echo $this->Form->input('name', array('div' => false, 'empty' => true)); // empty creates blank option.
echo $this->Form->input('status', array('label' => 'Status', 'options' => $statuses));
echo $this->Form->submit(__('Search', true), array('div' => false));
echo $this->Form->end();
?>
</div>
and I add in config/bootstrap:
CakePlugin::load('Search');
ERROR DISPLAY : Fatal error: Class 'Hash' not found in C:\xampp\htdocs\wfs\app\Plugin\Search\Controller\Component\PrgComponent.php on line 85
IN line 85 in app\Plugin\Search\Controller\Component\PrgComponent.php:
$this->_defaults = **Hash**::merge($this->_defaults, array(
'commonProcess' => (array)Configure::read('Search.Prg.commonProcess'),
'presetForm' => (array)Configure::read('Search.Prg.presetForm'),
), $settings);
}
What is the problem?
There's a mismatch between the cakephp version that Croogo bundles and what the Search plugin expects. To use Search plugin in Croogo 1.4.3, you need to find out which CakePHP version it's using, and download the correct version of Search plugin for that specific version of CakePHP.
Croogo 1.4.3 is really old, 14 releases behind the current stable. A lot has been improved and version 1.5.x already supports the Search plugin by default. I suggest you upgrade to the latest stable.

Sending email works on test but not on production system

In my model I'm sending an email once the User is created:
Model/User.php:
<?php
App::uses('AppModel', 'Model');
App::uses('CakeEmail', 'Network/Email');
class User extends AppModel {
...
private function sendWelcomeMail($name, $email, $password) {
$Email = new CakeEmail('smtp');
$Email->viewVars(array('name' => $name, 'password' => $password));
$Email->template(('welcome'));
$Email->emailFormat('html');
$Email->from(array('info#staycomputer.de' => 'Stay Computer'));
$Email->to($email);
$Email->subject('Stay Serviceordersystem: Willkommen / Welcome');
$Email->send();
}
}
Config/email.php:
public $smtp = array(
'transport' => 'Smtp',
'from' => array('info#***.de' => 'Stay Computer'),
'host' => '***',
'port' => 25,
'timeout' => 30,
'username' => '***',
'password' => '***',
'client' => null,
'log' => false,
//'charset' => 'utf-8',
//'headerCharset' => 'utf-8',
);
It's working fine on my testing system but not on production system (1&1 webhosting):
Error: An Internal Error Has Occurred.
According to error.log:
Error: [SocketException] Connection timed out Request URL ...
and
15:52:36 Error: Fatal Error (256): [CakeException] Unknown status code #0 /homepage/30/d20974062/htdocs/StaywebDB/serviceordersystem/lib/Cake/Error/ExceptionRenderer.php(212) ...
There is only 1 difference:
In production system I use the built in re-writing function.
Switching to gmail worked like a charm. Thanks for the recommondations.

SQL error 1064 in a hasMany through relationship

I've been trying to solve this for the past 3 days and nothing has came up. I've researched a lot and this errors usually come up when CakePHP can't find my model or I have some name wrong in my relationships. Well I tried looking at everything, but still couldn't find where the error is.
I have a User model, a Project model and a UserRole model which is the join table used by the hasMany through relationship.
File names:
user.php project.php user_role.php
Models:
class UserRole extends AppModel {
...
var $belongsTo = array(
'User' => array(
'className' => 'User',
'foreignKey' => 'user_id',
'conditions' => '',
'fields' => '',
'order' => ''
),
'Project' => array(
'className' => 'Project',
'foreignKey' => 'project_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
);
class User extends AppModel {
...
var $hasMany = array(
...
'UserRole' => array(
'className' => 'UserRole',
'foreignKey' => 'user_id',
'conditions' => '',
'fields' => '',
'order' => ''
),
);
class Project extends AppModel {
...
var $hasMany = array(
...
'UserRole' => array(
'className' => 'UserRole',
'foreignKey' => 'project_id',
'conditions' => '',
'fields' => '',
'order' => ''
),
);
When I try calling any method from UserRole from User, it give me the 1064 SQL error. Any hint on where could be the problem?
Things I've tried so far: checked if UserRole is being loaded, and it is. And I can call the UserRole functions from an element, they are working fine.
FUNCTIONS (get_related_clients is in Users and the other is in UserRole):
function get_related_clients ($id_user, $relationship_type) {
$id_names = $this->User->UserRole->get_id_users($id_user,$relationship_type);
...
}
function get_id_users ($id_agency = null,$type = null) {
$params_1 = array(
'conditions' => array('UserRole.user_id' => $id_agency)
);
$user_projects = $this->UserRole->find('all',$params_1);
$projects = array();
for ($i = 0; !empty($user_projects[$i]); $i++) {
$projects[] = $user_projects[$i]['UserRole']['project_id'];
}
$clients = array();
foreach ($projects as $project) { //pega o id de todos os usuarios que sao clientes da lista de projetos anteriores
$params_2 = array(
'conditions' => array('UserRole.project_id' => $project, 'UserRole.role' => $type)
);
$client_project = $this->UserRole->find('first',$params_2);
if ($id_agency != $client_project['UserRole']['user_id']) { $clients[] = $client_project['UserRole']['user_id']; } // voce nao pode ser cliente de voce mesmo
}
return $clients;
}
ERROR:
Warning (512): SQL Error: 1064: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'get_id_users' at line 1
DboSource::showQuery() - CORE/cake/libs/model/datasources/dbo_source.php, line 684
DboSource::execute() - CORE/cake/libs/model/datasources/dbo_source.php, line 266
DboSource::fetchAll() - CORE/cake/libs/model/datasources/dbo_source.php, line 410
DboSource::query() - CORE/cake/libs/model/datasources/dbo_source.php, line 364
Model::call__() - CORE/cake/libs/model/model.php, line 502
Overloadable::__call() - CORE/cake/libs/overloadable_php5.php, line 50
UserRole::get_id_users() - [internal], line ??
UsersController::get_related_clients() - APP/controllers/users_controller.php, line 71
Dispatcher::_invoke() - CORE/cake/dispatcher.php, line 204
Dispatcher::dispatch() - CORE/cake/dispatcher.php, line 171
Object::requestAction() - CORE/cake/libs/object.php, line 95
include - APP/views/elements/client_list.ctp, line 2
View::_render() - CORE/cake/libs/view/view.php, line 731
View::element() - CORE/cake/libs/view/view.php, line 392
include - APP/views/projects/index.ctp, line 45
View::_render() - CORE/cake/libs/view/view.php, line 731
UPDATE:
If I call the function through requestAction it works. :(
You're trying to call get_id_users function on UserRole model, while it appears to be in UserRoles controller. Hence the error.
Update:
This line:
UserRole::get_id_users() - [internal], line ??
in your log clearly shows that PHP did not find function get_id_users() in UserRole class (which should be model judging by its name, but since you've mistaken the names from the very start its hard to tell without actually looking at your files).
Conclusion: rebake your app (at least User and UserRole models and controllers), doing naming right this time.
The answer was: I can't call functions from another controller, unless it's declared with the model. :)

CakePHP: Uninitialized string offset: 0 in view

I am dealing with a wierd problem.
I have the Config model, together with these associations:
var $belongsTo = array(
'Language' => array(
'className' => 'Language',
'foreignKey' => 'default_language',
'conditions' => '',
'fields' => '',
'order' => ''
),
'Webmaster' => array(
'className' => 'User',
'foreignKey' => 'webmaster',
'conditions' => '',
'fields' => '',
'order' => ''
),
'Group' => array(
'className' => 'Group',
'foreignKey' => 'default_group',
'conditions' => '',
'fields' => '',
'order' => ''
),
);
In my ConfigsController I have the edit() action:
function edit($id = null) {
if (!$id && empty($this->data)) {
$this->Session->setFlash(__('Invalid config', true));
$this->redirect(array('action' => 'index'));
}
if (!empty($this->data)) {
if ($this->Config->save($this->data)) {
$this->Session->setFlash(__('The config has been saved', true));
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The config could not be saved. Please, try again.', true));
}
}
if (empty($this->data)) {
$this->data = $this->Config->read(null, $id);
}
$languages = $this->Config->Language->find('list');
$webmasters = $this->Config->Webmaster->find('list');
$groups = $this->Config->Group->find('list');
$this->set(compact('languages', 'groups', 'webmasters'));
}
And in my edit.ctp view, I have:
echo $this->Form->input('Language');
echo $this->Form->input('Webmaster');
echo $this->Form->input('Group');
Now, here is the problem: The group is correctly selected, but the other two (webmaster and language) are reseted on the first option, have no selected attribute, and as a "bonus", the language select displays this error: Notice (8): Uninitialized string offset: 0 [CORE/cake/libs/view/helper.php, line 859]
I struggled for almost 2 hours and couldn't find the isuue for this wierd problem. Could you please help me? Thank you!
EDIT
This is what the debugger shows:
Helper::__selectedArray() - CORE/cake/libs/view/helper.php, line 859
Helper::value() - CORE/cake/libs/view/helper.php, line 712
Helper::_initInputField() - CORE/cake/libs/view/helper.php, line 753
FormHelper::_initInputField() - CORE/cake/libs/view/helpers/form.php, line 2182
FormHelper::select() - CORE/cake/libs/view/helpers/form.php, line 1438
FormHelper::input() - CORE/cake/libs/view/helpers/form.php, line 882
include - APP/views/configs/edit.ctp, line 10
View::_render() - CORE/cake/libs/view/view.php, line 723
View::render() - CORE/cake/libs/view/view.php, line 419
Controller::render() - CORE/cake/libs/controller/controller.php, line 913
Dispatcher::_invoke() - CORE/cake/dispatcher.php, line 207
Dispatcher::dispatch() - CORE/cake/dispatcher.php, line 171
[main] - APP/webroot/index.php, line 83
And in the 'code' tab:
if (!empty($data)) {
foreach ($data as $var) {
$array[$var[$key]] = $var[$key];
You can use the following piece of code in your view instead:
echo $this->Form->input('default_language', array('options' => $languages));
echo $this->Form->input('webmaster', array('options' => $webmasters));
echo $this->Form->input('default_group', array('options' => $groups));
This would create a "select" drop down for each, and associate them correctly with the relevant fields.

Resources