[SOLVED] Error : cannot convert value of type `array` to string

Hello guys,

My name is Hamza, I’m a French student and a beginner in programmation !
So I’m doing a project, a personal one and it’s a kind of blog, where you can upload articles and those articles have comments and tags you know, basic. I’m stuck in a step. This step is that I want to select all my articles with entrering a tag in a search bar. I’ve done the search bar, I think that I had my articles but when I want to print then in my view I have one error : " Cannot convert value of type array to string" and I don’t understand.

EDITION : I already did the samething without the tag searching but I never seen this error…

There is my function where I take all my articles(it’s a screenshot) : http://prntscr.com/m10abg
And there is my view (it’s a screenshot too :slight_smile:) : http://prntscr.com/m10auy

Thank you for you help in advance and sorry for the spelling mistakes.

where(['Tags.text_tag' => $this->getRequest()->getData()])

This is going to expect a single value, not an array (assuming you’re using Cake 3.x). But getData is going to return an array.

Not sure that this is your problem, as you have shown the recherchearticle but not where that’s linked from, and you’ve said there’s a problem in your view but not specified which line it’s on.

You’re also more likely to get good responses if you include your code here (properly formatted) instead of through links to screen shots. I took the time to manually type out that one line of code, but many won’t bother; give them something they can copy-and-paste.

OK, thank you for advices, I’m going to check, and sorry I didn’t the “rules”, it’s my first time on this forum and more on forum oncerning programmation !

I’ll keep you informed ! Thank you

So I’ve checked and I think that my SQL request is good ! I did a dd on the variable and it looks well.
The problem is probably in my .ctp. Here is it :

Blockquote
<h1>Articles</h1>
<?php
echo $this->Form->create(null, [‘url’ => [‘controller’ => ‘Articles’, ‘action’ => ‘index’]]);
echo $this->Form->button(‘Fil d'actualité’);
echo $this->Form->end();
?>
<?php
echo $this->Form->create(null, [‘url’ => [‘controller’ => ‘Articles’, ‘action’ => ‘add’]]);
echo $this->Form->button(“Ajouter article”);
echo $this->Form->end();
?>
<?php
echo $this->Form->create(null, [‘url’ => [‘controller’ => ‘Articles’, ‘action’ => ‘recherchearticle’]]);
echo $this->Form->control(‘text_tag’, [‘type’ => ‘text’]);
echo $this->Form->button(‘Rechercher’);
echo $this->Form->end();
?>
<table>
<tr>
<th>Title</th>
<th>Author</th>
<th>Description</th>
<th>Image</th>
<th>Options</th>
</tr>
<?php
foreach ($articles as $article){ ?>
<tr>
<td><b><?=$this->Html->link(
$article->nameArticle,
[‘controller’ => ‘Articles’, ‘action’ => ‘view’, $article->id],
[‘class’ => ‘’, ‘title’ => $article->description]
)?></b></td>
<td><?= $article->nameAuthor ?></td>
<td><?= $article->description ?></td>
<td><?= $this->Html->image($article->image)?></td>
<td><?php
echo $this->Form->create(null, [‘url’ => [‘controller’ => ‘Articles’, ‘action’ => ‘delete’, $article->id]]);
echo $this->Form->button(‘Supprimer article’);
echo $this->Form->end();
echo $this->Form->create(null, [‘url’ => [‘controller’ => ‘Articles’, ‘action’ => ‘viewindexmodify’, $article->id]]);
echo $this->Form->button(‘Modifier article’);
echo $this->Form->end();
?></td>
</tr>
<?php } ?>
</table>

My error is just : “Cannot convert value of type array to string”, that’s all, no line, nothing.

The line number should be somewhere, if you have debugging turned on. Cake logs? DebugKit? Somewhere.

There is the error : http://prntscr.com/m164mh

So I checked many time my SQL request, when I don’t use any condition with where method, I can see all my articles but not filtred :

Blockquote
$articles = $this->Articles->find()->select([‘nameArticle’,‘nameAuthor’,‘description’,‘image’])->contain(‘Tags’)

And when I use the where method to filtre my articles I’ve the error : " Cannot convert value of type array to string” :

Blockquote
$text_tag = $this->getRequest()->getData();
$articles = $this->Articles->find()->select([‘nameArticle’,‘nameAuthor’,‘description’,‘image’])->contain(‘Tags’)->where([‘text_tag’ => $text_tag])

if it’s the where condition that doing bad, how can I filtre my articles without where method ?

As I said before, getData will return an array, and the form of where that you’re using won’t accept that. What exactly does your posted data look like? If it’s just a single tag, you need to extract that from the array before using it; if it’s multiple tags, you’ll likely want to use 'text_tag IN' => $text_tag.

I’m so sorry, I’m very stupid. When I did a dd, I did it like this

Blockquote
$param = $this->getRequest()->getData();
dd($param[‘text_tag’]);

but in my where i was sending just $param, I didn’ saved like this the name of the tag

Blockquote
$text_tag = $param[‘text_tag’];

Now it’s good ! All is working well ! Thank you very much !