cakephp - Can't submit form using create(); -
so have 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 empty $this->set('job','job'); $this->job->create(); } }
and have view 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 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 submit it, gives me next error:
error: call member function create() on boolean
no idea how fix. have entity
job.php:
<?php namespace app\model\entity; use cake\orm\entity; /** * job entity. */ class job extends entity { /** * fields can 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 fix error, appears on form submit?
error: call member function create() on boolean
i guess need with $this->set('job');
? i'm not sure exactly
by convention default, auto-loadable table controller based on controller name without trailing controller
, jobscontroller
table class named jobs
(table
) can autoloaded.
in case table class cannot loaded (for example because doesn't exist, or because name doesn't match 1 derived controller name), magic getter handles return false
, boolean, , trying call method on, hence error.
create()
btw doesn't exist anymore, should have @ orm migration guide, , docs in general grasp on how things work.
so either use $this->jobs
, make sure have table class named jobstable
, or override default model use (controller::_setmodelclass()), or load desired table manually (tableregistry::get() or controller::loadmodel()).
see also
Comments
Post a Comment