# fetchTable in Policy

**URL:** https://discourse.cakephp.org/t/fetchtable-in-policy/12388
**Category:** Need Help
**Created:** [January 18, 2025, 3:37pm UTC](https://discourse.cakephp.org/t/fetchtable-in-policy/12388 "2025-01-18T15:37:55Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![efjot](https://avatars.discourse-cdn.com/v4/letter/e/b38774/32.png) [@efjot](https://discourse.cakephp.org/u/efjot)
#### Post date: [January 18, 2025, 3:37pm UTC](https://discourse.cakephp.org/t/fetchtable-in-policy/12388/1 "2025-01-18T15:37:55Z")

</div>

Hi all,  
I am using policy from Authorization\IdentityInterface to restrict access to my objects.  
But for checking the access I need to fetch more information from database like  
$workerstable = $this-\>fetchTable(‘workers’);  
$worker = $workerstable-\>find()-\>where([‘user\_id’ =\> $user-\>id])-\>first();

But when I run application, I get an error

# Call to undefined method App\Policy\TicketPolicy::fetchTable()

My question is now, how do I get the fetchTable method in my Policy?  
do I have to use Cake\ORM\Table?

---

<div class="post-metadata">

### Author: ![jmcd73](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/jmcd73/32/481_2.png) [@jmcd73](https://discourse.cakephp.org/u/jmcd73)
#### Post date: [January 19, 2025, 9:56am UTC](https://discourse.cakephp.org/t/fetchtable-in-policy/12388/2 "2025-01-19T09:56:17Z")

</div>

You might try in your policy:

```auto
use Cake\ORM\Locator\LocatorAwareTrait;

class TicketPolicy {
         use LocatorAwareTrait;

         public function canX($user, $resource, $action)
         {
                 $workerstable = $this->fetchTable(‘workers’);
                 $worker = $workerstable->find()->where([‘user_id’ => $user->id])->first();
         }
}

```

---

<div class="post-metadata">

### Author: ![wasilij](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/wasilij/32/3211_2.png) [@wasilij](https://discourse.cakephp.org/u/wasilij)
#### Post date: [January 20, 2025, 11:24pm UTC](https://discourse.cakephp.org/t/fetchtable-in-policy/12388/3 "2025-01-20T23:24:03Z")

</div>

Look at this topic, maybe it is related to your problem …

> [@CakeDC/Auth: custom permissions for not logged in users](https://discourse.cakephp.org/t/cakedc-auth-custom-permissions-for-not-logged-in-users/12236):
>
> Hi everybody, I am using the CakeDC/Auth and Users plugin, that really does the job good slight_smile Thanks to the authors!!! But I struggle with one situation: I cannot define an allowed user function to evaluate the status (published, draft) of the Page entity for a not logged in user. When I open the not published page, the plugin redirects me to the login. I debugged the function \_matchPermission(…) of the Rbac class and found out that the code returns here a null value: if (!$user && …

---

<div class="post-metadata">

### Author: ![efjot](https://avatars.discourse-cdn.com/v4/letter/e/b38774/32.png) [@efjot](https://discourse.cakephp.org/u/efjot)
#### Post date: [January 22, 2025, 2:27pm UTC](https://discourse.cakephp.org/t/fetchtable-in-policy/12388/4 "2025-01-22T14:27:57Z")

</div>

yes thanks a lot. I was reading documentation and found a way the restrict the access on functions in an object.  
Even if you use the tables policy, you are able to handle the SQL statements too.  
Thanks a lot for your support
