2016-10-05 2 views
1

테이블을 갖는 발생CakePHP의 3.x를 - 추가() 함수는 오류

adresses_users 내 데이터베이스에가 포함

id | user_id | adress_id 

기능 내 AdressesUsersController.php

public function add() 
    { 
     $adressesUser = $this->AdressesUsers->newEntity(); 
     if ($this->request->is('post')) { 
      $adressesUser = $this->AdressesUsers->patchEntity($adressesUser, $this->request->data); 
      if ($this->AdressesUsers->save($adressesUser)) { 
       $this->Flash->success(__('The adresses user has been saved.')); 

       return $this->redirect(['action' => 'index']); 
      } else { 
       $this->Flash->error(__('The adresses user could not be saved. Please, try again.')); 
      } 
     } 
     $this->set(compact('adressesUser')); 
     $this->set('_serialize', ['adressesUser']); 
    } 
을에 추가

이 오류가 발생

Error: Call to a member function newEntity() on boolean File C:\xampp\htdocs\uh\src\Controller\AdressesUsersController.php

저는 컨트롤러와 모델을 구운 제품으로 무엇이 잘못 되었나요? 내가 비교 작업 응용 프로그램을
은 - 합법적 인 것 같다/

편집 :

<?php 
namespace App\Model\Table; 

use App\Model\Entity\AdressesUser; 
use Cake\ORM\Query; 
use Cake\ORM\RulesChecker; 
use Cake\ORM\Table; 
use Cake\Validation\Validator; 

/** 
* AdressesUsers Model 
* 
* @property \Cake\ORM\Association\BelongsTo $Users 
* @property \Cake\ORM\Association\BelongsTo $Adresses 
* 
* @method \App\Model\Entity\AdressesUser get($primaryKey, $options = []) 
* @method \App\Model\Entity\AdressesUser newEntity($data = null, array $options = []) 
* @method \App\Model\Entity\AdressesUser[] newEntities(array $data, array $options = []) 
* @method \App\Model\Entity\AdressesUser|bool save(\Cake\Datasource\EntityInterface $entity, $options = []) 
* @method \App\Model\Entity\AdressesUser patchEntity(\Cake\Datasource\EntityInterface $entity, array $data, array $options = []) 
* @method \App\Model\Entity\AdressesUser[] patchEntities($entities, array $data, array $options = []) 
* @method \App\Model\Entity\AdressesUser findOrCreate($search, callable $callback = null) 
*/ 
class AdressesUsersTable extends Table 
{ 

    /** 
    * Initialize method 
    * 
    * @param array $config The configuration for the Table. 
    * @return void 
    */ 
    public function initialize(array $config) 
    { 
     parent::initialize($config); 

     $this->table('adresses_users'); 
     $this->displayField('id'); 
     $this->primaryKey('id'); 

     $this->belongsTo('Users', [ 
      'foreignKey' => 'user_id', 
      'joinType' => 'INNER' 
     ]); 
     $this->belongsTo('Adresses', [ 
      'foreignKey' => 'adress_id', 
      'joinType' => 'INNER' 
     ]); 
    } 

    /** 
    * Default validation rules. 
    * 
    * @param \Cake\Validation\Validator $validator Validator instance. 
    * @return \Cake\Validation\Validator 
    */ 
    public function validationDefault(Validator $validator) 
    { 
     $validator 
      ->integer('id') 
      ->allowEmpty('id', 'create'); 


     return $validator; 
    } 

    /** 
    * Returns a rules checker object that will be used for validating 
    * application integrity. 
    * 
    * @param \Cake\ORM\RulesChecker $rules The rules object to be modified. 
    * @return \Cake\ORM\RulesChecker 
    */ 
    public function buildRules(RulesChecker $rules) 
    { 
     $rules->add($rules->existsIn(['user_id'], 'Users')); 
     $rules->add($rules->existsIn(['adress_id'], 'Adresses')); 

     return $rules; 
    } 
} 

전에 내 컨트롤러 클래스

/** 
* AdressesUsers Controller 
* 
* @property \App\Model\Table\AdressesUsersTable $AdressesUsers 
*/ 
+0

아마 시도 테이블 이름이 컨트롤러 이름과 일치하지 않습니다. 테이블 개체 정의? – arilia

+0

@RohitAilani 아니요, 엔티티 클래스 이름은 관례에 따라 다릅니다. – ndm

+0

예. 다른 모든 엔티티도 단수입니다. – Isengo

답변

1

public function add() 
    { 

     $this->loadModel('AdressesUsers'); 
     $adressesUser = $this->AdressesUsers->newEntity(); 
     if ($this->request->is('post')) { 

      if ($this->AdressesUsers->save($this->request->data)) { 
       $this->Flash->success(__('The adresses user has been saved.')); 

       return $this->redirect(['action' => 'index']); 
      } else { 
       $this->Flash->error(__('The adresses user could not be saved. Please, try again.')); 
      } 
     } 
     $this->set(compact('adressesUser')); 
     $this->set('_serialize', ['adressesUser']); 
    } 
+0

왜 모델을로드해야합니까? 어쨌든 내 컨트롤러에 포함시켜야합니까? o.O 나는 지금 사용자 정보와 주소 목록없이보기를 얻었습니다. – Isengo

+0

컨트롤러의 beforeFilter 기능에 있어야하며 그렇지 않은 경우 여기에 추가해야하며 오류로 모델이 누락되었음을 알 수 있습니다. 그래서, 나는 당신에게 이것을 제공했다. –