Isengo
Isengo

Reputation: 2073

CakePHP 3.x - add() function throws error

Having the table

adresses_users in my Database contains:

id | user_id | adress_id

Function add in my 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']);
    }

Getting this Error

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

I baked the controller and model - so I don´t get whats wrong.
I have a working app where I compared - seems legit :/

EDIT:

<?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;
    }
}

Before my Controller Class

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

Upvotes: 2

Views: 688

Answers (1)

Rohit Ailani
Rohit Ailani

Reputation: 910

try this

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

Upvotes: 1

Related Questions