Hi,
I am using cakephp 3.3.15. I have a category table in which later on, as per need, I created an additional field ‘user_id’ to know which category is created by which user. A user can update his business data and can create the category on the fly if he needs to, like the business started serving in a new category.
My code like this
$serviceCategory = TableRegistry::get('ServiceCategory'); $new_service_category = $serviceCategory->newEntity(); $this->request->data['name'] = 'abc'; $this->request->data['sub_id'] = 12;//parent_cat $this->request->data['user_id'] = 28;// $new_service_data = $serviceCategory->patchEntity($new_service_category, $this->request->data); $save_data = $serviceCategory->save($new_service_data);
\Model\Table\ServiceCategoryTable.php
have these fundtions
public function initialize(array $config){
parent::initialize($config);
$this->table(DB_PREFIX.‘service_categories’);
$this->displayField(‘id’);
$this->primaryKey(‘id’);
}public function validationDefault(Validator $validator){
$validator
->notEmpty(‘user_id’, ‘Select a user.’);$validator ->notEmpty('name', 'Category Name is required field.'); return $validator; }
I dont have any entity file in "\Model\Entity". I also read about “accessible” and “accessibleFields” fields but don’t know how to use them and make “user_id” field accessible at run time?
Right now the inser query get generated like
INSERT INTO
hh_service_categories
(sub_id
,name
) VALUES (12, ‘abc’);
which must be like
INSERT INTO
hh_service_categories
(sub_id
,name
,user_id
) VALUES (12, ‘abc’, ‘28’);
Please help.