0

I have two tables employees and user employees has field user_id relation between them us user hasOne employee and employee belongs to user, i have following code in my view file

<?php 
    echo $this->Form->input('employee.name');
    echo $this->Form->input('user.email');
    echo $this->Form->input('user.username');
    echo $this->Form->input('employee.employee_level');
?>

my EmployeeController code is

<?php 

    $employee = $this->Employees->newEntity($this->request->data);
    $user = $this->Employees->Users->newEntity($this->request->data);
        if ($this->request->is('post')) {

            $employee = $this->Employees->patchEntity($employee, $this->request->data, [
                'associated' => ['Users']
            ]);

            if($this->Employees->save($employee)) {

                $this->Flash->success(__('The employee has been saved.'));

            } else {
                $this->Flash->error(__('The employee could not be saved. Please, try again.'));
            }
        }
?>

Its saving the data if all thing going good and perfect but if there are some validation errors like username already exist or email already exist than its simple show the flash message that employee could not be saved please try again its not showing the model validation error message under the fields as shown in the attached image when i try to save data in single table enter image description here

Please tell me what is the problem in my view or controller file so that if any of two model has validation errors than its show error message under the related field

Sorry for my bad english Thanks

1 Answer 1

1

Try this for your form. You shouldn't need to provide the name of the top-level model.

echo $this->Form->input('name');
echo $this->Form->input('user.email');
echo $this->Form->input('user.username');
echo $this->Form->input('employee_level');

And in your controller, you never use $user, so no point in creating it, and since you're patching the employee entity with the request data, you don't need to initialize it the same way.

$employee = $this->Employees->newEntity();
if ($this->request->is('post')) {
    $employee = $this->Employees->patchEntity($employee, $this->request->data, [
        'associated' => ['Users']
    ]);
...
Sign up to request clarification or add additional context in comments.

6 Comments

Thank you so much its solve my problem you are awesome just mention this answer as approved answer i have one more question if you can help me what is the syntax in view for has many type fields like <?php echo $this->Form->input('image.0.image');?> , <?php echo $this->Form->input('image.1.image');?> and what is controller code for this
This is all covered in some detail in the manual.
Thanks a lot my friend, can you please tell me how to print detail of associated data value with paginate like employee table has user id and user has role_id now when i use the code $this->paginate($this->Employees) than i want to get name of role which relate to user table when i print he query and run in phpmyadmin its show the detail but when i use code for user $employee->has('user') ? $employee->user->email : "" than its print the email from user but how tio print the role table simillar to this thanks
You'll need to change the containment in the pagination so that the role is read, and then it'll be something like $employee->user->role->xxx. Again, this is all well covered in the manual. StackOverflow isn't here to do your research for you.
i have used the same vode but its always show that "Trying to get property of non-object " in my employee controller i have define this code $this->paginate = ['contain' => ['Users']]; but how to use roles for this so that result will show
|

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.