0

So i have this method inside of my:

JobsController.ctp:

<?php
namespace App\Controller;

use App\Controller\AppController;
use Cake\ORM\TableRegistry;

    /**
     * Jobs Controller
     *
     * @property \App\Model\Table\JobsTable $Jobs
     */
    class JobsController extends AppController
    {
        public $name = 'Jobs';

        public function add()
            {
                    //Some Vars assigning skipped, var job is empty
                    $this->set('job','Job');
                    $this->Job->create();
            }
    }

And I have this view with the form itself:

add.ctp:

<?= $this->Form->create($job); ?>
    <fieldset>
        <legend><?= __('Add Job Listing'); ?></legend>
        <?php 
            echo $this->Form->input('title');
            echo $this->Form->input('company_name');
            echo $this->Form->input('category_id',array(
                                    'type' => 'select',
                                    'options' => $categories,
                                    'empty' => 'Select Category'
                                    )); 
            echo $this->Form->input('type_id',array(
                                    'type' => 'select',
                                    'options' => $types,
                                    'empty' => 'Select Type'
                                    )); 
            echo $this->Form->input('description', array('type' => 'textarea'));
            echo $this->Form->input('city');
            echo $this->Form->input('contact_email');               
        ?>
    </fieldset>
<?php 
    echo $this->Form->button('Add');
    $this->Form->end(); 
?>

Also this table class:

JobsTable.php

<?php

namespace App\Model\Table;

use Cake\ORM\Table;

class JobsTable extends Table
{
    public function initialize(array $config)
    {
        $this->belongsTo('Types', [
            'foreignKey' => 'type_id',
            'joinType' => 'INNER',
        ]);
        $this->belongsTo('Categories', [
            'foreignKey' => 'category_id',
            'joinType' => 'INNER',
        ]);
    }

}

And when I submit it, it gives me next error:

Error: Call to a member function create() on boolean

No idea how to fix. I also have an entity

Job.php:

<?php
namespace App\Model\Entity;

use Cake\ORM\Entity;

/**
 * Job Entity.
 */
class Job extends Entity
{

    /**
     * Fields that can be mass assigned using newEntity() or patchEntity().
     *
     * @var array
     */
    protected $_accessible = array(
        'category_id' => true,
        'user_id' => true,
        'type_id' => true,
        'company_name' => true,
        'title' => true,
        'description' => true,
        'city' => true,
        'contact_email' => true,
        'category' => true,
        'user' => true,
        'type' => true,
    );
}

So how do I fix this error, that appears on form submit?

Error: Call to a member function create() on boolean

I guess I need to do something with $this->set('job'); ? but I'm not sure what exactly

2
  • syntax to set data is $this->set('key', 'value'); in your case $this->set('job','Job'); Commented May 11, 2015 at 18:14
  • Same error, it didn't help :( Commented May 11, 2015 at 18:19

1 Answer 1

2

By convention the default, auto-loadable table for a controller is based on the controller name without the trailing Controller, so for JobsController a table class named Jobs(Table) can be autoloaded.

In case the table class cannot be loaded (for example because it doesn't exist, or because the name doesn't match the one derived from the controller name), the magic getter that handles this will return false, a boolean, and this is where you are trying to call a method on, hence the error.

create() btw doesn't exist anymore, you should have a look at the ORM migration guide, and the docs in general to get a grasp on how things now work.

So either use $this->Jobs and make sure that you have a table class named JobsTable, or override the default model to use (Controller::_setModelClass()), or load the desired table manually (TableRegistry::get() or Controller::loadModel()).

See also

Sign up to request clarification or add additional context in comments.

8 Comments

I have tableclass, and it matches the database table.
yes, I've replaced $this->Job with $this->Jobs, and now it says that Unknown method "create", and as you said it doesn't exist anymore. So now i have to figure out how to replace it
@imran How would that be possible? The OP was invoking a method on a boolean, and the error message is very clear about that, also a stacktrace for the error can be found in the logs, any non-novice PHP programmer should be able figure what the error message means, and how to debug it. Once you'll access a non-existing method on an actual table class instance, you'll receive an error message saying that the method doesn't exist, that's nothing the CakePHP core needs to take care of, that's PHP/OOP 101.
Please, correct me if i'm wrong. Is the Modelless Form is the only way to process data received from the form in CakePHP 3.x?
@Tachi I don't really get what you are doing there, and why you think that you must use intermediate data processing classes, that's really something for a new question where you can explain your problem in detail and supply code examples.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.