Monday, March 3, 2014

Re: 3.0 - Cake not connecting to MySql DB

You need to update your app template, specifically your app.php file as the arguments for configuring a database connection recently changed:

https://github.com/cakephp/app/blob/master/App/Config/app.default.php#L212


On Monday, March 3, 2014 4:49:53 PM UTC+1, Bayezid Alam wrote:
Dears,

DB Connection in Cake3

Located at /var/www/cake3/App/Config/app.php
/**
 * Connection information used by the ORM to connect
 * to your application's datastores.
 */
'Datasources' => [
'default' => [
'className' => 'Cake\\Database\\Driver\\Mysql',
'persistent' => false,
'host' => 'localhost',
'login' => 'loginName',
'password' => 'PassWord',
'database' => 'DatabaseName',
'prefix' => false,
'encoding' => 'utf8',
],

/**
* The test connection is used during the test suite.
*/
'test' => [
'className' => 'Cake\\Database\\Driver\\Mysql',
'persistent' => false,
'host' => 'localhost',
'login' => 'my_app',
'password' => 'secret',
'database' => 'test_myapp',
'prefix' => false,
'encoding' => 'utf8',
],
],

My Controller located at /var/www/cake3/App/Controller/PostsController.php
<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

namespace App\Controller;

use Cake\ORM\TableRegistry;

class PostsController extends AppController{
    public $helpers = array('Html', 'Form');
    
    public function index(){
       
        $posts = TableRegistry::get('Posts');
        
        $posts = $posts->find();
        
        
        $this->set('posts', $posts);
    }
}
?>

My Table located at /var/www/cake3/App/Model/Table/PostsTable.php

<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

namespace App\Model\Table;

use Cake\ORM\Table;

class PostsTable extends Table{
    public function initialize(array $config){
        $this->table('posts');
    }
    
}
?>


View located at /var/www/cake3/App/Template/Posts/index.ctp

<h1>Blog posts</h1>
<table>
    <tr>
        <th>Id</th>
        <th>Title</th>
        <th>Created</th>
    </tr>

    <!-- Here is where we loop through our $posts array, printing out post info -->
    <?php foreach ($posts as $post): ?>
    <tr>
        <td><?php echo $post['Post']['id']; ?></td>
        <td>
            <?php echo $this->Html->link($post['Post']['title'],array('controller' => 'posts', 'action' => 'view', $post['Post']['id'])); ?>
        </td>
        <td><?php echo $post['Post']['created']; ?></td>
    </tr>
    <?php endforeach; ?>
    <?php unset($post); ?>
</table>


It's showing only Header names, not bringing any data from Database. i Have three more records on database.

Please help me, am i missing any thing or not?


--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP
 
---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cake-php+unsubscribe@googlegroups.com.
To post to this group, send email to cake-php@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/groups/opt_out.

No comments: