Thursday, November 20, 2014

Cakephp 3.0 - How to apply condition at find list method

Dear All,

How do i apply condition at find('list') at Cakephp 3.0?

i have a Companies, Departments and Employees

Companies hasMany Department.
Companies hasMany Employees
Departments has Many Employees.

i want to add Employees from company & want to get departments list of that company.

My Add method:

public function add($companyId = null){
        
        if (!$companyId){
            throw new NotFoundException(__('A Company is required'));
        }
        
        $this->loadModel('Companies');
        
        $company = $this->Companies->get($companyId);        
             
        $employee = $this->Employees->newEntity($this->request->data);
        
        if ($this->request->is('post')){
            $employee->company_id = $company->id;
            if ($this->Employees->save($employee)){
                $this->Flash->success(__('The employee has been added successfully'));
                return $this->redirect(['action' => 'index']);
            } else {
                $this->Flash->error(__('Unable to add the employee, please try again'));
            }
        }
        
         
        $departments = $this->Companies->Departments->find('list'); // Here, Want to get only Departments lists of that company passed through $companyId 
        
        $this->set(compact('employee','departments'));
    }

Could you please help suggesting me on this regard?

Note: Sorry for my bad English

Thanks
Bayezid

--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP

---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cake-php+unsubscribe@googlegroups.com.
To post to this group, send email to cake-php@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/d/optout.

No comments: