【问题标题】:CakePHP 3.x - add() function throws errorCakePHP 3.x - add() 函数抛出错误
【发布时间】:2016-10-05 09:38:53
【问题描述】:

有桌子

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']);
    }

收到此错误

错误:在布尔文件 C:\xampp\htdocs\uh\src\Controller\AdressesUsersController.php 上调用成员函数 newEntity()

我烘焙了控制器和模型 - 所以我没有弄错。
我有一个可以运行的应用程序进行比较 - 看起来是合法的:/

编辑:

<?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
 */

【问题讨论】:

  • 可能是表名和控制器名不匹配,请问可以发一下表对象定义吗?
  • @RohitAilani 不,实体类名称按照惯例是单数。
  • 是的。所有其他实体也是单数。
  • @RohitAilani,不,实体是单一的。他还说,代码是烘焙的。也许清除缓存会有所帮助?
  • false 将由魔术 getter 返回,以防属性名称与$this-&gt;modelClass 的(类部分)不匹配。 github.com/cakephp/cakephp/blob/3.3.5/src/Controller/…

标签: php cakephp entity


【解决方案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']);
    }

【讨论】:

  • 为什么我必须加载模型?那不应该包含在我的控制器中吗? o.O 我现在看到了——虽然没有用户和地址列表。
  • 它应该在控制器的 beforeFilter 函数中,或者如果它不存在,那么您需要在此处添加它,因为您的错误表明您缺少模型。所以,我为你提供了这个。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-11-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多