2017-11-19 10 views
0

私はcakephpの新機能です。私はすべての必要な手順を行って、まだCakePHPによるArticlecontroller.phpからadduserの機能のエラー:booleanでメンバー関数newEntity()を呼び出す

コード、データベース内のデータを保存するに支障持っていますUSERTABLEモデルの

public function adduser() 
{ 
    $user = $this->Users->newEntity(); 
    if ($this->request->is('post')) { 
     $user = $this->Users->patchEntity($user, $this->request->getData()); 

     // Hardcoding the user_id is temporary, and will be removed later 
     // when we build authentication out. 
     $user->user_id = 1; 

     if ($this->Users->save($user)) { 
      $this->Flash->success(__('Your article has been saved.')); 
      return $this->redirect(['action' => 'index']); 
     } 
     $this->Flash->error(__('Unable to add your article.')); 
    } 
    $this->set('article', $user); 
} 

コード:

<?php 
// src/Model/Table/ArticlesTable.php 
namespace App\Model\Table; 

use Cake\ORM\Table; 

class UsersTable extends Table 
{ 
    public function initialize(array $config) 
    { 
     $this->addBehavior('Timestamp'); 
    } 
} 

をデータベーステーブルを私のlocahostに: enter image description here

答えて

3

私はあなたがコントローラのユーザーモデルを読み込むのを忘れたと思います。最初の行の前にadduser()関数でこの行を追加することを修正する必要があります。それはこのように見えるはずです。

public function adduser() 
{ 
    $this->loadModel('Users'); 
    $user = $this->Users->newEntity(); 
... 

Cakephpのドキュメント。 https://book.cakephp.org/3.0/en/controllers.html#loading-additional-models

関連する問題