# CAKE 5.0, controller and table class alias, component

**URL:** <https://discourse.cakephp.org/t/cake-5-0-controller-and-table-class-alias-component/11509>\
**Category:** Need Help\
**Created:** [September 11, 2023, 1:34pm UTC](https://discourse.cakephp.org/t/cake-5-0-controller-and-table-class-alias-component/11509 "2023-09-11T13:34:49Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![shadowx.jb](https://avatars.discourse-cdn.com/v4/letter/s/a88e4f/32.png) [@shadowx.jb](https://discourse.cakephp.org/u/shadowx.jb)\
**Post date:** [September 11, 2023, 1:34pm UTC](https://discourse.cakephp.org/t/cake-5-0-controller-and-table-class-alias-component/11509/1 "2023-09-11T13:34:49Z")

</div>

I upgrading to v5 and have problem… in controller call component:

```auto
$cart = $this->Cart->read('order');

```

and i get error:

```auto
Table class for alias `Cart` could not be found.

```

In version 4 its works… i define components in AppController.php

```auto
$this->loadComponent('Cart');

```

Thanks for the advice

---

<div class="post-metadata">

**Author:** ![KevinPfeifer](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/kevinpfeifer/32/3365_2.png) [@KevinPfeifer](https://discourse.cakephp.org/u/KevinPfeifer)\
**Post date:** [September 11, 2023, 2:06pm UTC](https://discourse.cakephp.org/t/cake-5-0-controller-and-table-class-alias-component/11509/2 "2023-09-11T14:06:16Z")

</div>

Can’t reproduce that.

What I did:

- `bin/cake bake component Cart`
- Check that `$this->loadComponent('Cart');` is present inside the `src/Controller/AppController.php` initialize method
- Add a method inside `src/Controller/Component/CartComponent.php`
- Call it inside another controller via `$this->Cart->test();`

Everything works as expected.

Also I am unable to find your specific error message

```auto
Table class for alias `Cart` could not be found.

```

in the source code.  
So please post the whole stacktrace of your error.

---

<div class="post-metadata">

**Author:** ![shadowx.jb](https://avatars.discourse-cdn.com/v4/letter/s/a88e4f/32.png) [@shadowx.jb](https://discourse.cakephp.org/u/shadowx.jb)\
**Post date:** [September 12, 2023, 5:03am UTC](https://discourse.cakephp.org/t/cake-5-0-controller-and-table-class-alias-component/11509/3 "2023-09-12T05:03:07Z")

</div>

I found where the problem is.  
I do have `CartController.php`

```auto
    public function initialize(): void
    {
        parent::initialize();

        $this->Users = $this->fetchTable('Users');
    }

```

So if I have the same name and component, it is probably looking for the name of the table in CartController.php

If I call the component in a controller other than CartController.php, it works.

 ![obrazek](https://canada1.discourse-cdn.com/flex029/uploads/cakephp/original/2X/4/414dae6798efd974b38e43ecda3d14e80a3113b4.jpeg)

if add `$this->Cart = $this->loadComponent('Cart');` to initialize method to `CartController.php` then its work…

```auto
    public function initialize(): void
    {
        parent::initialize();

        $this->Cart = $this->loadComponent('Cart');

        $this->Users = $this->fetchTable('Users');
    }

```
