# Unable to use ConnectionManager for inserting data

**URL:** <https://discourse.cakephp.org/t/unable-to-use-connectionmanager-for-inserting-data/10317>\
**Category:** Need Help\
**Tags:** release\
**Created:** [April 19, 2022, 3:15pm UTC](https://discourse.cakephp.org/t/unable-to-use-connectionmanager-for-inserting-data/10317 "2022-04-19T15:15:20Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![sachinak700](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/sachinak700/32/2474_2.png) [@sachinak700](https://discourse.cakephp.org/u/sachinak700)\
**Post date:** [April 19, 2022, 3:15pm UTC](https://discourse.cakephp.org/t/unable-to-use-connectionmanager-for-inserting-data/10317/1 "2022-04-19T15:15:20Z")

</div>

Once of the worst documentation.

Argument 1 passed to Cake\Database\Query::insert() must be of the type array, string given, called in /var/www/html/staging/src/Controller/Component/DatabaseComponent.php on line 141

---

<div class="post-metadata">

**Author:** ![Zuluru](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/zuluru/32/1230_2.png) [@Zuluru](https://discourse.cakephp.org/u/Zuluru)\
**Post date:** [April 19, 2022, 4:15pm UTC](https://discourse.cakephp.org/t/unable-to-use-connectionmanager-for-inserting-data/10317/2 "2022-04-19T16:15:00Z")

</div>

Please share the code you’re trying to use that causes this error.

---

<div class="post-metadata">

**Author:** ![sachinak700](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/sachinak700/32/2474_2.png) [@sachinak700](https://discourse.cakephp.org/u/sachinak700)\
**Post date:** [April 19, 2022, 5:40pm UTC](https://discourse.cakephp.org/t/unable-to-use-connectionmanager-for-inserting-data/10317/3 "2022-04-19T17:40:11Z")

</div>

@Zuluru Thankyou for your reply

$this-\>connection = ConnectionManager::get(‘default’);  
$insertSatet = $this-\>connection-\>insert($table, $insertData);  
return $insertSatet-\>lastInsertId($table);

this is my code

---

<div class="post-metadata">

**Author:** ![Zuluru](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/zuluru/32/1230_2.png) [@Zuluru](https://discourse.cakephp.org/u/Zuluru)\
**Post date:** [April 19, 2022, 6:20pm UTC](https://discourse.cakephp.org/t/unable-to-use-connectionmanager-for-inserting-data/10317/4 "2022-04-19T18:20:28Z")

</div>

What exactly is `$insertData`, and why are you using this instead of ORM methods that work with entities?

---

<div class="post-metadata">

**Author:** ![sachinak700](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/sachinak700/32/2474_2.png) [@sachinak700](https://discourse.cakephp.org/u/sachinak700)\
**Post date:** [April 19, 2022, 7:30pm UTC](https://discourse.cakephp.org/t/unable-to-use-connectionmanager-for-inserting-data/10317/5 "2022-04-19T19:30:35Z")

</div>

@Zuluru its an associative array with column name as key and value as column value. Initially it was working fine but today it broke. This same coding style is already implemented in two of my live projects and there it works fine.

eg: $insertData = [“id”=\>1,“name”=\>“test”];

---

<div class="post-metadata">

**Author:** ![Zuluru](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/zuluru/32/1230_2.png) [@Zuluru](https://discourse.cakephp.org/u/Zuluru)\
**Post date:** [April 19, 2022, 8:39pm UTC](https://discourse.cakephp.org/t/unable-to-use-connectionmanager-for-inserting-data/10317/6 "2022-04-19T20:39:11Z")

</div>

I’ve never used this particular interface (the ORM is so much better for 99% of applications), but it looks like this should work. I guess I’d check to make sure that right at the time you’re calling it, it really does have that, and not, for example, a string with a serialized or JSON-encoded version of what you expect.

---

<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:** [April 20, 2022, 7:52pm UTC](https://discourse.cakephp.org/t/unable-to-use-connectionmanager-for-inserting-data/10317/7 "2022-04-20T19:52:26Z")

</div>

This works for me without any problems:

```auto
/** @var \Cake\Database\Connection $connection */
$connection = \Cake\Datasource\ConnectionManager::get('default');
$lastID = $connection->insert('categories', [
    'name' => 'This is a Test',
    'lft' => 0,
    'rght' => 0
])->lastInsertId('categories');

```

---

<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:** [April 20, 2022, 8:00pm UTC](https://discourse.cakephp.org/t/unable-to-use-connectionmanager-for-inserting-data/10317/8 "2022-04-20T20:00:09Z")

</div>

The fact that your error literally says `Argument 1 passed to Cake\Database\Query::insert() must be of the type array, string given` means that you do something like

```auto
$connection = \Cake\Datasource\ConnectionManager::get('default');
$connection->insert('categories', 'somestring');

```

somewhere in your code or somewhere in your `$insertData` variable.

If you are 100% certain its not that array then you maybe are doing the same thing somewhere else in your code where you are currently not expecting it to throw that error.

Looking at the whole stacktrace will tell you where you come from to reproduce that error.
