Hi I'm a novice in cakephp However, I'm trying to make my blog with cakephp 3.0 which is stable version. I got stuck not taken database show view.ctp
<?php
namespace App\Controller;
class ListjobsController extends AppController{
public function jobdetail(){
$jobdetail = $this->Listjobs->find('all');
$this->set( compact('jobdetail'));
}
}
<?php
namespace App\Model\Table;
use Cake\ORM\Table;
class ListjobsTable extends Table{
public function initialize(array $config){
$this->table('listjobs');
$this->displayField('title');
$this->primaryKey('id');
$this->belongsToMany('Users',[
'foreignKey' => 'user_id'
]);
}
}
<p><?= pr($jobdetail);exit; ?></p>
it does not appear that the current database information:
Cake\ORM\Query Object
(
[sql] => SELECT Listjobs.id AS `Listjobs__id`, Listjobs.user_id AS `Listjobs__user_id`, Listjobs.type_id AS `Listjobs__type_id`, Listjobs.cate_id AS `Listjobs__cate_id`, Listjobs.title AS `Listjobs__title`, Listjobs.location AS `Listjobs__location`, Listjobs.description AS `Listjobs__description`, Listjobs.skillsrequired AS `Listjobs__skillsrequired`, Listjobs.companyname AS `Listjobs__companyname`, Listjobs.website AS `Listjobs__website`, Listjobs.email AS `Listjobs__email`, Listjobs.password AS `Listjobs__password`, Listjobs.created AS `Listjobs__created`, Listjobs.modified AS `Listjobs__modified` FROM listjobs Listjobs
[params] => Array
(
)
[defaultTypes] => Array
(
[Listjobs.id] => integer
[id] => integer
[Listjobs.user_id] => integer
[user_id] => integer
[Listjobs.type_id] => integer
[type_id] => integer
[Listjobs.cate_id] => integer
[cate_id] => integer
[Listjobs.title] => string
[title] => string
[Listjobs.location] => string
[location] => string
[Listjobs.description] => text
[description] => text
[Listjobs.skillsrequired] => text
[skillsrequired] => text
[Listjobs.companyname] => string
[companyname] => string
[Listjobs.website] => string
[website] => string
[Listjobs.email] => string
[email] => string
[Listjobs.password] => string
[password] => string
[Listjobs.created] => datetime
[created] => datetime
[Listjobs.modified] => datetime
[modified] => datetime
)
[decorators] => 0
[executed] =>
[hydrate] => 1
[buffered] => 1
[formatters] => 0
[mapReducers] => 0
[contain] => Array
(
)
[matching] => Array
(
)
[extraOptions] => Array
(
)
[repository] => App\Model\Table\ListjobsTable Object
(
[registryAlias] => Listjobs
[table] => listjobs
[alias] => Listjobs
[entityClass] => \Cake\ORM\Entity
[associations] => Array
(
[0] => users
)
[behaviors] => Array
(
)
[defaultConnection] => default
[connectionName] => default
)
)
I can not understand why the data in the database does not appear. hope you can help me!!!
The find()
method always returns a Query
object, that is the reason you cannot see the results but only the SQL
that is going to be executed. If you are just starting with cake 3 there are a couple nice way in which you can see what the results of a query are:
debug($this->Table->find('all')->toArray());
That will give you an array of entities that were fetched from the database, the output may still be a bit confusing for you if you are not use to the idea of entities. SO here is another trick:
// In bootstrap.php
function dj($data)
{
debug(json_encode($data, JSON_PRETTY_PRINT));
}
And then:
dj($this->Table->find('all'));
do change in controller
namespace App\Controller;
class ListjobsController extends AppController{
public function jobdetail(){
$jobdetail = $this->Listjobs->find('all');
$this->set('listjobs', $jobdetail);
$this->set('_serialize', ['listjobs']);
}
}
now you can display data at view
Write in the appController.php
file:
public function initialize()
{
$this->loadModel('Listjobs');
}
This should solve your problem.