good morning. I have two tables relations,
Articles hasMany Comments
MySQL Articles table
+---------+------------------+--------------------------+
| ID | Judul | Isi |
+---------+------------------+--------------------------+
| 1 | Penyakit Kucing | Penyakit ini disebabkan |
| | | bla...bla... |
|---------|------------------|--------------------------|
MySQL Comments table
+---------+--------------+------------------+-----------------------+
| ID | article_id | Judul | Komentar |
+---------+--------------+------------------+-----------------------+
| 1 | 1 | Penyakit kucing | apa penyebabnya |
| 2 | 1 | Penyakit kucing | bagaimana solusinya |
|---------|--------------|------------------|-----------------------+
inside TableMethod
Articles Table
<?php
class ArticlesTable extends Table
{
public function initialize(array $config)
{
$this->hasMany('Comments', [
'foreignKey' => 'article_id'
]);
}
}
?>
Comments Table
<?php
class CommentsTable extends Table
{
public function initialize(array $config)
{
$this->belongsTo('Articles', [
'foreignKey' => 'article_id'
]);
}
}
?>
I want to add comments called from
View.ctp in Articles, then get ID articles to save in CommentsTable
here’s CommentsController
<?php
class CommentsController extends AppController
{
public function add($id = null) //reference ID from View.ctp (ArticlesController)
{
$comment = $this->Comments->newEntity();
$judulByID = $this->Articles->get($id, ['contain' => []]); //shown error SQL builder to get ID from View.ctp (ArticlesController)
$isiByID = $this->Articles->get($id, ['contain' => []]);
$comment->article_ID = $this->Articles->get($id, ['contain' => []]);
$comment->Judul = $this->Articles->find()->select(['Judul' => $judulByID]);
$comment->Isi = $this->Articles->find()->select(['Isi' => $isiByID]);
$comment->Dibuat = (Time::now())->i18nFormat('dd-MM-yyyy HH:mm:ss');
if ($this->request->is('post')) {
$comment = $this->Comments->patchEntity($comment, $this->request->getData());
if ($this->Comments->save($comment)) {
$this->Flash->success(__('Komentar berhasil disimpan.'));
return $this->redirect(['action' => 'index']);
}
$this->Flash->error(__('Komentar gagal disimpan.'));
}
$articles = $this->Comments->Articles->find('list', ['limit' => 20]);
$users = $this->Comments->Users->find('list', ['limit' => 20]);
$this->set(compact('comment', 'articles', 'users'));
}
}
?>
The error messages shown below
Notice (1024): Undefined property: CommentsController::$Articles in C:\xampp\htdocs\klinikucing\src\Controller\CommentsController.php on line 32 [CORE\src\Controller\Controller.php, line 388]
Call to a member function get() on boolean Error
Documentation API
Error in: ROOT\src\Controller\CommentsController.php, line 32
I hopefull someone give fix the error, thanx for your help…